×
Namespaces

Variants
Actions

Archived:Collision detection events with PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.


Article Metadata
Code Example
Source file: Media:CornRush.zip
Article
Created: marcelcaraciolo (26 Mar 2009)
Last edited: hamishwillee (31 May 2013)


PySymbian is a simple and powerful language. Despite not providing native API's for game development, as the existing one in PyGame , we can easily create some code to PySymbian support it. The example below serves just as a base to the development of the Sprite class existing in MIDP 2.0 of JavaME. To detect collision between two objects, it uses the method collidesWith(). As same as in Java ME, this method returns True when a collision has ocurred and accepts an image or another sprite to check collision.

The creation of a simple framework for games would help simplifying the development of applications with PySymbian.

The example of code is based on the article in portuguese hosted in the Wiki Nokia Developer Criando animações em PySymbian by Felipe Andrade .

Contents

First Steps

If you're beginning in PySymbian, I'd recommend you to read some articles and tutorials for introduction of the platform.

Example

In case of this be your first time with PySymbian, download the PySymbian runtime and script shell.

After the installation, create a directory named 'Python' at your memory card and copy the code below in a new file with .py termination.

The image must be either copied to the recent created 'Python' folder at the memory card.

Cornrush.png

import appuifw
import e32
from graphics import Image
import graphics
 
 
## Sprite
#Similar to the Sprite class of the game package available at Java ME MIDP 2.0
#This piece of code was created in only 30 minutes, and it wasn't tested exhaustively,
#so may have some bugs in your implementation. Any improvement is welcomed.
 
class Sprite:
 
## Class Constructor
# @param self The object pointer.
def __init__(self,aImg,aFrWidth,aFrHeight):
self._iImg = aImg
self._iFrWidth = aFrWidth
self._iFrHeight = aFrHeight
self._iCurrentFrame = 1
self._iIndFrame = 0
self._iWidth, self._iHeight = self._iImg.size
self._iMaxWidthFrames = self._iWidth / self._iFrWidth
self._iMaxHeightFrames = self._iHeight / self._iFrHeight
self._iFrameSequence = [0]
self._iPosX = 0
self._iPosY = 0
self._iOrientation = None
self._iFlipOrientation = None
#end __init__
 
##Set the frame Sequence
# @param self The object pointer.
# @param aSeq Frame sequence.
def set_frame_sequence(self,aSeq):
self._iIndFrame = 0
self._iFrameSequence = aSeq
self.iCurrentFrame = self._iFrameSequence[self._iIndFrame]
#end set_frame_sequence
 
##Sets the position of the sprite in the screen
# @param self The object pointer.
# @param aPos The Layer position.
def set_position(self,aPos):
self._iPosX, self._iPosY = aPos
#end set_position
 
##Gets the current position
# @param self The object pointer.
# @return the current position
def getPosition(self):
return self._iPosX,self._iPosY
#end getPosition
 
##Gets the frame Size
# @param self The object pointer.
# @return the frame Size.
def getFrameSize(self):
return self._iFrWidth, self._iFrHeight
#end getFrameSize
 
###Gets the Sprite Size
# @param self The object pointer.
# @return the Sprite Size.
def getSize(self):
return self._iWidth, self._iHeight
#end getSize
 
##Sets the next frame.
#@param self The object pointer.
def next_frame(self):
self._iIndFrame += 1
if self._iIndFrame == len(self._iFrameSequence):
self._iIndFrame = 0
self._iCurrentFrame = self._iFrameSequence[self._iIndFrame]
#end next_frame
 
##Set the current frame
# @param self The object pointer.
# @param aFrame frame.
def set_frame(self, aFrame):
self._iCurrentFrame = self._iFrameSequence[aFrame]
#end set_frame
 
##Set the Sprite Orientation
# @param self The object pointer.
# @param aOrientation the sprite rotation orientation.
# @param aFlipOrientation the sprite flip orientation.
def set_transform(self,aOrientation,aFlipOrientation):
self._iImg = self._iImg.transpose(aOrientation)
self._iImg = self._iImg.transpose(aFlipOrientation)
#end set_transform
 
##Moves the sprite at the screen
# @param self The object pointer.
# @param aPosX X coord.
# @param aPosY Y coord
def move(self,aPosX,aPosY):
self._iPosX, self._iPosY = (self._iPosX + aPosX, self._iPosY + aPosY)
#end move
 
##Checks for collision between itself and another object.
# @param self The object pointer.
# @param aObject the another object.
def collidesWith(self,aObject):
objectLeft,objectTop = aObject.getPosition()
objectRight,objectBottom = (objectLeft + aObject.getFrameSize()[0], \
objectTop + aObject.getFrameSize()[1])
selfLeft,selfTop = (self._iPosX, self._iPosY)
selfRight,selfBottom = (selfLeft + self._iFrWidth, selfTop + self._iFrHeight)
if not ((selfLeft >= objectRight) or (selfTop >= objectBottom) \
or (selfRight <= objectLeft) or (selfBottom <= objectTop)):
return True
return False
#end collidesWith
 
##Draws the object at the screen in accordance with its current frame and current position.
#@param self The object pointer.
#@param aGraphics Canvas.
def paint(self,aGraphics):
aGraphics.blit(self._iImg,target=(self._iPosX,self._iPosY), \
source=((self._iCurrentFrame-1) * self._iFrWidth, 0, \
self._iCurrentFrame * self._iFrWidth, self._iCurrentFrame * self._iFrHeight))
# end paint
 
##Sprite Animation
#Responsable for creating the objects at the screen
class SpriteAnimation:
##Class Constructor
#@param self The object pointer.
def __init__(self):
 
#Defines the body of application
self._iCanvas = appuifw.Canvas()
appuifw.app.body = self._iCanvas
 
#Defines the screen resolution
appuifw.app.orientation = 'portrait'
appuifw.app.screen = 'full'
 
#Sets the Height and Width of canvas
self._cWidth, self._cHeight = self._iCanvas.size
 
#Sprite CornRush Up
self._imgCornRush = Image.open("e:\\Python\\cornrush.png")
self._spCornRushUp = Sprite(self._imgCornRush, 66, 56)
self._spCornRushUp.set_position((self._cWidth/2 - self._spCornRushUp.getSize()[0]/2, \
self._spCornRushUp.getSize()[1]))
self._spCornRushUp.set_frame_sequence([1,2])
self._spCornRushUp.set_transform(graphics.ROTATE_180, graphics.FLIP_LEFT_RIGHT)
 
#Sprite CornRush Down
self._spCornRushDown = Sprite(self._imgCornRush,66,56)
self._spCornRushDown.set_position((self._cWidth/2 - self._spCornRushDown.getSize()[0]/2, \
self._cHeight-self._spCornRushDown.getSize()[1]))
self._spCornRushDown.set_frame_sequence([1,2])
 
#Defines the exit key handler
appuifw.app.exit_key_handler = self.quit
 
#Sets the app status
self._iRunning = True
self._iCollision = 0
 
#defines a white background
self._iCanvas.rectangle((0,0,240,320), outline = 0xFFFFFF, width=240)
 
#Creates the app main loop
self.paint()
# end __init__
 
#Redraws the objects at the screen.
# @param self The object pointer.
def paint(self):
while self._iRunning:
 
self._iCanvas.clear()
self._spCornRushUp.paint(self._iCanvas)
self._spCornRushDown.paint(self._iCanvas)
 
self._spCornRushUp.next_frame()
self._spCornRushDown.next_frame()
 
self._spCornRushDown.move(0,-1)
self._spCornRushUp.move(0,1)
 
#A delay to show at the screen that the collision has ocurred.
if self._iCollision == 2:
self._iRunning = False
 
#Checks the collision detection
self.detectionCollision()
 
#Process pendent events
e32.ao_yield()
 
#update rate
e32.ao_sleep(0.1)
# end paint
 
##Collision Checker
def detectionCollision(self):
if self._spCornRushUp.collidesWith(self._spCornRushDown):
self._spCornRushUp.set_frame_sequence([3])
self._spCornRushDown.set_frame_sequence([3]);
self._iCollision += 1
#end detectionCollision
 
##Exit Handler
# @param self The object pointer.
def quit(self):
self._iRunning = False
#end quit
 
 
animation = SpriteAnimation()


Screenshots

CornRushNoCollision.jpg        CornRushCollision.jpg

Source Code

Donwload source code of this example: File:CornRush.zip


Autor

marcelcaraciolo -- 09:12, 26 March 2009 (EEST)


--nirpsis 10:27, 1 September 2009 (UTC)

This page was last modified on 31 May 2013, at 04:07.
46 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×