Wednesday, July 15, 2009

Brick-breaker game in Python - Part 1


Introduction

In this article, I would be explaining how to create a Brick-Breaker game in Python for S60. The Brick-Breaker game is an arcade game. In this game, the player moves a paddle on the screen and bounces a ball. The objective of the game is to destroy bricks which are at the top.

Development tools

1. Text editor - notepad is sufficient.

2. Python for S60 - preferred version: PyS60 1.4.5 stable release

3. Python for S60 script shell - preferred version: PyS60 1.4.5 stable release

The PyS60 tools can be downloaded from Sourceforge resources.

The following modules are used when developing this application:

  • appuifw module
  • sysinfo module
  • graphics module
  • e32 module
  • key_codes module
  • random module
  • math module
  • time module

Part 1

In this article (Part-1) we would discuss the different slider functions in detail.

Initializing slider parameter

Before discussing, any functions in detail - let us define some of the slider parameters.

#SLIDER PARAMETERS########
SliderX1=120
SliderY1=305
SliderX2=160
SliderY2=315
SliderSpeed=22

SliderColor=RED
TotalScreenWidth=230

draw_screen()

By using this function, We create a black screen in canvas. The definition of the draw_screen() is as follows:

def draw_screen():
appuifw.app.screen = 'full'
global canvas
canvas = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit

global img
img = graphics.Image.new(canvas.size)
img.clear(BLACK)
handle_redraw(None)

draw_slider()

By using this function, we create a slider in canvas using keyboard shortcuts. The defination of the draw_screen() is as follows:

def draw_slider():

global canvas
#Keyboard shortcuts
canvas.bind(key_codes.EKeyLeftArrow,lambda: leftkey())
canvas.bind(key_codes.EKeyRightArrow,lambda: rightkey())
appuifw.app.body = canvas
appuifw.app.exit_key_handler = quit
img.rectangle((SliderX1,SliderY1,SliderX2,SliderY2),fill=SliderColor)
handle_redraw(None)

leftkey()

The leftkey() is called from the draw_slider() function. By using this function, we can move the slider to left side by pressing left key. Thus, the code of leftkey() function is as follows:

def leftkey():
if (SliderX1>0):


img.rectangle((SliderX1, SliderY1, SliderX2, SliderY2),fill=BLACK)
global img, canvas,SliderX1, SliderY1, SliderX2, SliderY2
SliderX1=SliderX1-SliderSpeed
SliderX2=SliderX2-SliderSpeed
img.rectangle((SliderX1, SliderY1, SliderX2, SliderY2),fill=SliderColor)
#appuifw.note(u"Left arrow was pressed")
handle_redraw(None)

rightkey()

The rightkey() is called from the draw_slider() function. By using this function, we can move the slider to right side by pressing right key. Thus the code of rightkey() function is as follows:

def rightkey():
if (SliderX2<230):

img.rectangle((SliderX1, SliderY1, SliderX2, SliderY2),fill=BLACK)
global img, canvas,SliderX1, SliderY1, SliderX2, SliderY2
SliderX1=SliderX1+SliderSpeed
SliderX2=SliderX2+SliderSpeed
img.rectangle((SliderX1, SliderY1, SliderX2, SliderY2),fill=SliderColor)
#appuifw.note(u"Left arrow was pressed")
handle_redraw(None)


That finishes the slider functions (Part 1) for the Brick-Breaker Game.

The next article in the series : Part 2 for brick-Breaker, would cover Brick functions for this game.

Screenshots These screenshots demonstrate the Slider and its movable position on canvas. The screenshots are relevant only to Part 1.



No comments:

Post a Comment