Tuesday, July 21, 2009

How to develop brick-breaker game in Python - Part 3

Introduction

Finally we have the last article in the series of "How to develop brick-breaker game in Python. If you have followed the previous articles in this series, then after finishing this one, you should be able to create your own Brick-breaker or similar game using PyS60.

In this article- Part 3, we would learn about the code for the moving ball, intializing the bricks parameter, slider parameter and sequence of calling functions. Let's discuss them in detail.


Part 3

Code for moving the ball
Here we have a little code for moving the ball in canvas.
This ball is moving automatically.

Thus the code for moving the ball could be as follows:


# import module
import e32,random
import time
import appuifw
import graphics
import math
BLACK=(0,0,0)
GREEN=(0,255,0)
RED=(255,0,0)
running=1
#define exit function
def quit():
global running
running=0

appuifw.app.exit_key_handler=quit
#Set the screen to large
appuifw.app.screen='large'
#Create an instance of Canvas and set it as the application's body
canvas=appuifw.Canvas()
appuifw.app.body=canvas
j,k=canvas.size
#define circle coordinate
x1vel=10
y1vel=10
x1=150
y1=300
speed=15
while running:
global x1,y1,x1vel,y1vel
if((x1<0)>j-15)):
x1vel=x1vel*-1
if ((y1<0)>k-15)):
y1vel=y1vel*-1
x1=x1+x1vel
y1=y1+y1vel
x2=x1+speed
y2=y1+speed
#draw circle
canvas.ellipse((x1,y1,x2,y2),outline=None,fill=RED)
# Sleep for 0.1
e32.ao_sleep(0.1)
#clear canvas
canvas.clear(BLACK)
e32.ao_yield()

Sequence of calling functions

The defined functions need to be called in a proper sequence. sequence of calling functions are as follows:

draw_screen()
draw_slider()
draw_bricks()
erase_bricks(12)
app_lock = e32.Ao_lock()
app_lock.wait()
Screenshots














We have concluded the series of articles related on "How to develop Brick-Breaker game. If you will follow this series, you would be able to create a Brick-Breaker game in Python for S60.

0 comments:

Post a Comment