Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRicardo Flores <ricardo.flores@usmp.pe>2010-09-22 20:28:45 (GMT)
committer Ricardo Flores <ricardo.flores@usmp.pe>2010-09-22 20:28:45 (GMT)
commit55f8e05352e27f9f5f84877d07f5f452720009a8 (patch)
tree5f14112be8a6d325ba221996d88dc53c10b566c0
parent3f9f13c51d71066c3a26c4123804908d18db9269 (diff)
This version check collsion and uses masks
-rw-r--r--runMathBunnyFunc.py256
1 files changed, 96 insertions, 160 deletions
diff --git a/runMathBunnyFunc.py b/runMathBunnyFunc.py
index 07b0bd3..8c12486 100644
--- a/runMathBunnyFunc.py
+++ b/runMathBunnyFunc.py
@@ -1,11 +1,11 @@
##==================================================================##
-## MATH BUNNY ##
+## MATH BUNNY ##
##==================================================================##
## Based on PyGame Game Asteroides ##
## ##
## Version: 0.1 (20-08-2010) ##
## Desarrollado por: Ricardo Flores S. ##
-## Class: Constants.py ##
+## Class: Constants.py ##
## Programa desarrollado bajo licencia Creative Commons ##
##==================================================================##
@@ -42,19 +42,73 @@ CARROTMAXSIZE = 150
#CARROT VELOCITY
CARROTMINVEL= 4
-CARROTMAXVEL = 8
+CARROTMAXVEL = 6
#CARROT VELOCITY FRECUENCY
CARROTVELFREC = 25
#BUNNY MOVING VELOCITY
-BUNNYMOVEVEL = 10
+BUNNYMOVEVEL = 15
score=0
+
+class MBSprite(pygame.sprite.Sprite):
+ def __init__(self, image):
+ self.image = image
+ #Images.__init__(self)
+ self.mask = pygame.mask.from_surface(self.image) # pixelmask
+ #self.mask.invert()
+ self.rect=image.get_rect()
+
+class Carrots(MBSprite) :
+ def __init__(self, image):
+ carrotSize = random.randint(CARROTMINSIZE, CARROTMAXSIZE)
+ image=pygame.transform.scale(image, (carrotSize, carrotSize))
+ MBSprite.__init__(self,image)
+
+
+ self.rect = pygame.Rect(random.randint(0, WINDOWWIDTH-carrotSize), 0 - carrotSize, carrotSize, carrotSize)
+ self.velocity = random.randint(CARROTMINVEL, CARROTMAXVEL)
+ self.number =random.randint(max(result-5,1), min(99,result+5))
+
+
+
+
+
+
+# Initialize Pygame, window and mouse pointer
+pygame.init()
+mainClock = pygame.time.Clock()
+surface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
+# Window Title
+pygame.display.set_caption(_('Math Bunny'))
+# Mouse Visibility
+pygame.mouse.set_visible(False)
+
+
+# Sounds
+gameOverSound = pygame.mixer.Sound('./media/sounds/gameover.wav')
+#pygame.mixer.music.load('./media/sounds/background.mid')
+
+# Images
+bunnyImage = pygame.image.load('./media/images/bunny.gif')
+carrotImage = pygame.image.load('./media/images/Carrots.png')
+bunnyImageF=MBSprite(bunnyImage)
+carrotImageF=MBSprite(carrotImage)
+
+rectBunny = bunnyImageF.rect
+#rectBunny = bunnyImage.get_rect()thBunnyFunc.py", line 148, in bunnyEatsCarrot
+#rectCarrot = carrotImage.get_rect()
+
+
##=========================##
##Declaracion de metodos ##
##=========================##
+
+
+
+
#Close Game Method
def closeGame():
pygame.quit()
@@ -85,18 +139,20 @@ def waitPlayerPressKey():
if event.type == JOYBUTTONDOWN:
return
-
#Bunny Eats a Carrot
-def bunnyEatsCarrot(rectBunny, carrots):
+def bunnyEatsCarrot(bunnyImage, carrots):
for b in carrots:
- if rectBunny.colliderect(b['rect']):
+ #if bunnyImageF.rect.colliderect(b.rect):
+ print 'offset '+ str(carrotImageF.rect[0]-bunnyImageF.rect[0])+', '+str(carrotImageF.rect[1]-bunnyImageF.rect[1])
+ print 'Carrot: ' + str(carrotImageF.rect)
+ if pygame.sprite.collide_mask(bunnyImageF,b):
return True
return False
def checkResult(result,carrots):
global score
for b in carrots:
- if result==b['number']:
+ if result==b.number:
score=score+20
return True
if score>0:
@@ -113,17 +169,17 @@ def drawText(text, font, surface, x, y, color):
-# Initialize Pygame, window and mouse pointer
-pygame.init()
-mainClock = pygame.time.Clock()
-surface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
-# Window Title
-pygame.display.set_caption(_('Math Bunny'))
-# Mouse Visibility
-pygame.mouse.set_visible(False)
+#Fonts
+font = pygame.font.SysFont(None, 80)
+#font = pygame.font.Font("./mediadef drawText(text, font, surface, x, y, color):/fonts/space age.ttf", 50)
+#Max Points
+maxScore = 0
+result=0
+
+
'''
JoyStick Functions Disabled
#Initialize Joystick
@@ -145,35 +201,18 @@ except pygame.error:
'''
-# Sounds
-gameOverSound = pygame.mixer.Sound('./media/sounds/gameover.wav')
-#pygame.mixer.music.load('./media/sounds/background.mid')
-
-# Images
-bunnyImage = pygame.image.load('./media/images/bunny.gif')
-bunnyImage.get_alpha()
-rectBunny = bunnyImage.get_rect()
-carrotImage = pygame.image.load('./media/images/Carrots.png')
-carrotImage.get_alpha()
-
-#Fonts
-font = pygame.font.SysFont(None, 80)
-#font = pygame.font.Font("./mediadef drawText(text, font, surface, x, y, color):/fonts/space age.ttf", 50)
-
-
# Mostrar la pantalla de Inicio
drawText(_('MATH BUNNY'), font, surface, (WINDOWWIDTH / 2.75), (WINDOWHEIGHT / 2.5), TITLECOLOR)
drawText(_('PRESS ANY KEY'), font, surface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 2.5) + 80, TITLECOLOR)
pygame.display.update()
waitPlayerPressKey()
-
-#Max Points
-maxScore = 0
-result=0
##============================##
## Game start ##
##============================##
+
+
+
while True:
screen = pygame.display.set_mode( (WINDOWWIDTH,WINDOWHEIGHT) )
background = load_image('./media/images/farm.jpg');
@@ -195,9 +234,9 @@ while True:
result=number1+number2
- while True: # Score loop
+ while True: # Scobre loop
#score += 1 # increase score
-
+
for event in pygame.event.get():
if event.type == QUIT:
closeGame()
@@ -234,7 +273,7 @@ while True:
carrotsQuick = False
score= 0
if event.key == K_ESCAPE:
- closeGame()
+ closeGame()
if event.key == K_LEFT or event.key == ord('a') or event.key == K_KP4 or event.key== K_KP7:
moveLeft = False
@@ -251,125 +290,16 @@ while True:
#rectBunny.move_ip(event.pos[0] - rectBunny.centerx, 0)
- #JOYTICK EVENTS
-
- if event.type == JOYAXISMOTION:
- x1 , y1, x2, y2 = j.get_axis(0), j.get_axis(1), j.get_axis(2), j.get_axis(3)
- print ('x1 e y1 : ' + str(x1) +' , '+ str(y1))
- print ('x2 e y2 : ' + str(x2) +' , ' + str(y2))
-
-
- #Axis movement on JOYSTICK
- if(j.get_axis(0)<0):
- moveRight = False
- moveLeft = True
- if(j.get_axis(0)>0):
- moveLeft = False
- moveRight = True
- if(j.get_axis(1)>0):
- moveUp = False
- moveDown = True
- if(j.get_axis(1)<0):
- moveDown = False
- moveUp = True
-
-
- if(j.get_axis(0)<0 and j.get_axis(1)<0):
- moveRight = False
- moveLeft = True
- moveDown = False
- moveUp = True
- if(j.get_axis(0)<0 and j.get_axis(1)>0):
- moveRight = False
- moveLeft = True
- moveDown = True
- moveUp = False
- if(j.get_axis(0)>0 and j.get_axis(1)<0):
- moveLeft = False
- moveRight = True
- moveDown = False
- moveUp = True
- if(j.get_axis(0)>0 and j.get_axis(1)>0):
- moveLeft = False
- moveRight = True
- moveDown = True
- moveUp = False
-
- if(j.get_axis(0)==0):
- moveRight = False
- moveLeft = False
- if(j.get_axis(1)==0):
- moveDown = False
- moveUp = False
- if(x1==0 or y1==0):
- moveRight = False
- moveLeft = False
- moveDown = False
- moveUp = False
-
-
-
- #Joystick Directional Keys movement
- elif event.type == JOYHATMOTION:
- print ('hat motion')
- print(str(j.get_hat(0)))
- if(j.get_hat(0)==(-1,0)):
- moveRight = False
- moveLeft = True
- if(j.get_hat(0)==(0,1)):
- moveDown = False
- moveUp = True
- if(j.get_hat(0)==(1,0)):
- moveLeft = False
- moveRight = True
- if(j.get_hat(0)==(0,-1)):
- moveUp = False
- moveDown = True
- if(j.get_hat(0)==(-1,1)):
- moveUp = True
- moveLeft = True
- if(j.get_hat(0)==(1,1)):
- moveRight = True
- moveUp = True
- if(j.get_hat(0)==(1,-1)):
- moveRight = True
- moveDown = True
- if(j.get_hat(0)==(-1,-1)):
- moveLeft = True
- moveDown = True
- if(j.get_hat(0)==(0,0)):
- moveUp = False
- moveDown = False
- moveRight = False
- moveLeft = False
-
- #Joystick ball movement
- elif event.type == pygame.locals.JOYBALLMOTION:
- print ('Joystick ball movement')
- #JOYSTICKBUTTONDOWN event
- elif event.type == pygame.locals.JOYBUTTONDOWN:
- print ('JOYSTICKBUTTONDOWN event')
- print(' boton 0 state: '+str(j.get_button(0)))
- #Evento de Joystick de boton suelto
- elif event.type == pygame.locals.JOYBUTTONUP:
- print ('JOYBUTTONUP event')
-
-
-
# New carrots on the screen
if not carrotsBack and not carrotsSlow and not carrotsQuick:
newCarrotCounter += 1
if newCarrotCounter == CARROTVELFREC:
newCarrotCounter = 0
- carrotSize = random.randint(CARROTMINSIZE, CARROTMAXSIZE)
- newCarrot = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-carrotSize), 0 - carrotSize, carrotSize, carrotSize),
- 'velocity': random.randint(CARROTMINVEL, CARROTMAXVEL),
- 'surface':pygame.transform.scale(carrotImage, (carrotSize, carrotSize)),
- 'number':random.randint(max(result-5,1), min(99,result+5)),
- }
-
+# carrotSize = random.randint(CARROTMINSIZE, CARROTMAXSIZE)
+
+ newCarrot=Carrots(carrotImage)
carrots.append(newCarrot)
-
+
# Realiza el movimiento del jugador.
if moveLeft and rectBunny.left > 0:
rectBunny.move_ip(-1 * BUNNYMOVEVEL, 0)
@@ -386,17 +316,17 @@ while True:
# Move carrots down
for b in carrots:
if not carrotsBack and not carrotsSlow and not carrotsQuick:
- b['rect'].move_ip(0, b['velocity'])
+ b.rect.move_ip(0, b.velocity)
elif carrotsBack:
- b['rect'].move_ip(0, -5)
+ b.rect.move_ip(0, -5)
elif carrotsSlow:
- b['rect'].move_ip(0, 1)
+ b.rect.move_ip(0, 1)
elif carrotsQuick:
- b['rect'].move_ip(0, 5)
+ b.rect.move_ip(0, 5)
# Delete carrots when you can't see it on the screen
for b in carrots[:]:
- if b['rect'].top > WINDOWHEIGHT:
+ if b.rect.top > WINDOWHEIGHT:
carrots.remove(b)
# Draw background on game
@@ -409,8 +339,10 @@ while True:
# Dibuja a cada enemigo
for b in carrots:
- surface.blit(b['surface'], b['rect'])
- drawText(str(b['number']), font, b['surface'], 35, 50, (255,255,255))
+
+ surface.blit(b.image, b.rect)
+
+ drawText(str(b.number), font, b.image, 45, 55, (255,255,255))
# Show score and maximum score
@@ -421,7 +353,11 @@ while True:
pygame.display.update()
# Verifica si es que algun enemigo ha colisionado con el jugador.
- if bunnyEatsCarrot(rectBunny, carrots):
+# if bunnyEatsCarrot(rectBunny, carrots):
+ bunnyImageF.rect=rectBunny
+
+
+ if bunnyEatsCarrot(bunnyImage, carrots):
if checkResult(result,carrots):
drawText(_('CORRECT ANSWER!'), font, surface, (WINDOWWIDTH / 3.5), (WINDOWHEIGHT / 2.25), TEXTCOLOR)
drawText(_('PRESS ANY KEY'), font, surface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 2.25) + 50, TEXTCOLOR)