From 1221a093713ca4d654255ab07f45ece07f69926a Mon Sep 17 00:00:00 2001 From: Mateu Batle Date: Fri, 19 Nov 2010 12:16:54 +0000 Subject: restructure game2 fixed several bugs --- diff --git a/game2/game2.py b/game2/game2.py index d1089dc..08f1015 100755 --- a/game2/game2.py +++ b/game2/game2.py @@ -89,10 +89,6 @@ musicSound = pygame.mixer.Sound('./media/sounds/menumusic.ogg') font = pygame.font.SysFont(None, 80) -gameOverSound = None -correctAnswerSound = None -musicSound = None - imageList = [] imageList2 = [] @@ -131,6 +127,7 @@ class Game2(usmpgames.ApplicationState): global bunnyImageF, rectBunny bunnyImageF=MBSprite(bunnyImage) rectBunny = bunnyImageF.rect + self.new_operation = True def entering_state(self, fromStack): @@ -145,6 +142,7 @@ class Game2(usmpgames.ApplicationState): global score, a score = 0 a = 0 + self.new_operation = True def exiting_state(self, fromStack): global score @@ -154,295 +152,291 @@ class Game2(usmpgames.ApplicationState): color = (0, 0, 0, 0), pos = (660, 260), rectsize = (380, 390) ); - global musicSound + global gameOverSound, correctAnswerSound, musicSound + gameOverSound.stop() musicSound.stop() + correctAnswerSound.stop() + pygame.mixer.music.stop() usmpgames.ApplicationState.exiting_state(self, fromStack) - def loop(self, ms): + def input(self, ms): + global carrotsBack, carrotsSlow, carrotsQuick, moveRight, moveLeft, score, rectBunny, moveUp + + for event in pygame.event.get(): + if event.type == QUIT: + self.set_running( False ) + # KEYDOWN EVENTS + if event.type == KEYDOWN: + if event.key == ord('z'): + carrotsBack = True + if event.key == ord('x'): + carrotsSlow = True + if event.key == ord('c'): + carrotsQuick = True + if event.key == K_LEFT or event.key == ord('a') or event.key == K_KP4 or event.key== K_KP7: + moveRight = False + moveLeft = True + if event.key == K_RIGHT or event.key == ord('d') or event.key == K_KP6 or event.key== K_KP1: + moveLeft = False + moveRight = True + #Up and Down moves disabled + #if event.key == K_UP or event.key == ord('w') or event.key == K_KP8 or event.key== K_KP9: + #moveDown = False + #moveUp = True while(i=100): + rectBunny.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 250) + moveLeft = moveRight = moveUp = moveDown = False + #Game Cheat + carrotsBack = carrotsSlow = carrotsQuick = False + newCarrotCounter = 0 + #pygame.mixer.music.play(-1, 0.0) + musicSound.play(-1) + + global number1 + global number2 + global GAMEMODE + if(GAMEMODE==1): + #print('n: '+str(n)) + #random.randint(max(result-4,1), min(99,result+4)) number1 = random.randint(max(n-5,0),n) + 1 number2 = random.randint(max(n-5,0),n) - result=number1+number2 - - elif(GAMEMODE==2): - #print('n: '+str(n)) - #random.randint(max(result-4,1), min(99,result+4)) - number1 = random.randint(max(n-15,0),n) + 1 - number2 = random.randint(max(n-15,0),n) - z=min(number1,number2) - number1=max(number1,number2) - number2=z - while (number1==number2): + #result=number1+number2 + result=100 + while (result>=100): + number1 = random.randint(max(n-5,0),n) + 1 + number2 = random.randint(max(n-5,0),n) + result=number1+number2 + + elif(GAMEMODE==2): + #print('n: '+str(n)) + #random.randint(max(result-4,1), min(99,result+4)) number1 = random.randint(max(n-15,0),n) + 1 number2 = random.randint(max(n-15,0),n) z=min(number1,number2) number1=max(number1,number2) - number2=z + number2=z + while (number1==number2): + number1 = random.randint(max(n-15,0),n) + 1 + number2 = random.randint(max(n-15,0),n) + z=min(number1,number2) + number1=max(number1,number2) + number2=z + result=number1-number2 result=number1-number2 - result=number1-number2 - elif(GAMEMODE==3): - #print('n: '+str(n)) - #random.randint(max(result-4,1), min(99,result+4)) - number1 = random.randint(max(n-5,0),n+1)+1 - number2 = random.randint(max(n-5,0),n+1)+1 - while (number1>10 or number2 >10): + elif(GAMEMODE==3): + #print('n: '+str(n)) + #random.randint(max(result-4,1), min(99,result+4)) number1 = random.randint(max(n-5,0),n+1)+1 number2 = random.randint(max(n-5,0),n+1)+1 - result=number1*number2 - result=number1*number2 - elif(GAMEMODE==4): - #print('n: '+str(n)) - #random.randint(max(result-4,1), min(99,result+4)) - number1 = random.randint(1,9) - #number1=9 - #number2 = random.randint(max(0,n-1),n)+1 - #result=number1+result2 - result=number1 - putImagesToList() - - - #print ('number1: '+str(number1)+' number2: '+str(number2)+' result: '+str(result)) - while self.running(): # Score loop - #score += 1 # increase score - - for event in pygame.event.get(): - if event.type == QUIT: - self.set_running( False ) - # KEYDOWN EVENTS - if event.type == KEYDOWN: - if event.key == ord('z'): - carrotsBack = True - if event.key == ord('x'): - carrotsSlow = True - if event.key == ord('c'): - carrotsQuick = True - if event.key == K_LEFT or event.key == ord('a') or event.key == K_KP4 or event.key== K_KP7: - moveRight = False - moveLeft = True - if event.key == K_RIGHT or event.key == ord('d') or event.key == K_KP6 or event.key== K_KP1: - moveLeft = False - moveRight = True - #Up and Down moves disabled - #if event.key == K_UP or event.key == ord('w') or event.key == K_KP8 or event.key== K_KP9: - #moveDown = False - #moveUp = True while(i10 or number2 >10): + number1 = random.randint(max(n-5,0),n+1)+1 + number2 = random.randint(max(n-5,0),n+1)+1 + result=number1*number2 + result=number1*number2 + elif(GAMEMODE==4): + #print('n: '+str(n)) + #random.randint(max(result-4,1), min(99,result+4)) + number1 = random.randint(1,9) + #number1=9 + #number2 = random.randint(max(0,n-1),n)+1 + #result=number1+result2 + result=number1 + putImagesToList() + + + # Add 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) + a=random.randint(0, 1) + if(a==0): + newCarrot=Carrots(fruit1Image,result) + else: + newCarrot=Carrots(fruit2Image,result) + carrots.append(newCarrot) + + # Moves player + if moveLeft and rectBunny.left > 0: + rectBunny.move_ip(-1 * BUNNYMOVEVEL, 0) + if moveRight and rectBunny.right < WINDOWWIDTH: + rectBunny.move_ip(BUNNYMOVEVEL, 0) + if moveUp and rectBunny.top > 0: + rectBunny.move_ip(0, -1 * BUNNYMOVEVEL) + if moveDown and rectBunny.bottom < WINDOWHEIGHT: + rectBunny.move_ip(0, BUNNYMOVEVEL) + + # Puts Bunny on the mouse pointer + pygame.mouse.set_pos(rectBunny.centerx, rectBunny.centery) + + # Move carrots down + for b in carrots: if not carrotsBack and not carrotsSlow and not carrotsQuick: - newCarrotCounter += 1 - if newCarrotCounter == CARROTVELFREC: - newCarrotCounter = 0 - #carrotSize = random.randint(CARROTMINSIZE, CARROTMAXSIZE) - a=random.randint(0, 1) - if(a==0): - newCarrot=Carrots(fruit1Image,result) - else: - newCarrot=Carrots(fruit2Image,result) - carrots.append(newCarrot) - - # Moves player - if moveLeft and rectBunny.left > 0: - rectBunny.move_ip(-1 * BUNNYMOVEVEL, 0) - if moveRight and rectBunny.right < WINDOWWIDTH: - rectBunny.move_ip(BUNNYMOVEVEL, 0) - if moveUp and rectBunny.top > 0: - rectBunny.move_ip(0, -1 * BUNNYMOVEVEL) - if moveDown and rectBunny.bottom < WINDOWHEIGHT: - rectBunny.move_ip(0, BUNNYMOVEVEL) - - # Puts Bunny on the mouse pointer - pygame.mouse.set_pos(rectBunny.centerx, rectBunny.centery) - - # Move carrots down - for b in carrots: - if not carrotsBack and not carrotsSlow and not carrotsQuick: - b.rect.move_ip(0, b.velocity) - elif carrotsBack: - b.rect.move_ip(0, -5) - elif carrotsSlow: - b.rect.move_ip(0, 1) - elif carrotsQuick: - 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-150: - carrots.remove(b) - - # Draw background on game - background = Application.get_resource_background ('./media/images/yard.jpg') - self.screen().blit(background, (0,0) ) - #surface.fill(BACKGROUNDCOLOR) + b.rect.move_ip(0, b.velocity) + elif carrotsBack: + b.rect.move_ip(0, -5) + elif carrotsSlow: + b.rect.move_ip(0, 1) + elif carrotsQuick: + 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-150: + carrots.remove(b) + + # Draw background on game + background = Application.get_resource_background ('./media/images/yard.jpg') + self.screen().blit(background, (0,0) ) + #surface.fill(BACKGROUNDCOLOR) - # Draw enemies - for b in carrots: - surface.blit(b.image, b.rect) - drawText(str(b.number), font, b.image, 15, 25, (255,255,255)) + # Draw enemies + for b in carrots: + surface.blit(b.image, b.rect) + drawText(str(b.number), font, b.image, 15, 25, (255,255,255)) - # Show score and maximum score - global maxScore - global score - drawText(_('PUNTAJE: %s') % (score), font, surface, 700, 0, TEXTCOLOR) - drawText(_('MAXIMO: %s') % (maxScore), font, surface, 700, 60, TEXTCOLOR) + # Show score and maximum score + global maxScore + global score + drawText(_('PUNTAJE: %s') % (score), font, surface, 700, 0, TEXTCOLOR) + drawText(_('MAXIMO: %s') % (maxScore), font, surface, 700, 60, TEXTCOLOR) - if(GAMEMODE==1): - #This draw the operation - drawText(_('SUMAR: %d + %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) + if(GAMEMODE==1): + #This draw the operation + drawText(_('SUMAR: %d + %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) - elif(GAMEMODE==2): - #This draw the operation - drawText(_('RESTAR: %d - %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) + elif(GAMEMODE==2): + #This draw the operation + drawText(_('RESTAR: %d - %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) - elif(GAMEMODE==3): - #This draw the operation - drawText(_('MULTIPLICAR: %d x %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) + elif(GAMEMODE==3): + #This draw the operation + drawText(_('MULTIPLICAR: %d x %d') % (number1,number2), font, surface, 10, 0, TEXTCOLOR) - elif(GAMEMODE==4): - #This draw the operation - drawText(_('CONTAR '), font, surface, 10, 0, TEXTCOLOR) - medC=0 - i=0 - while(i maxScore: - maxScore = score # Puts new score - #print ('bunnyEatsCarrot(after): MaxScore'+str(maxScore)+' score: '+str(maxScore)) + elif(GAMEMODE==4): + #This draw the operation + drawText(_('CONTAR '), font, surface, 10, 0, TEXTCOLOR) + medC=0 + i=0 + while(i maxScore: + maxScore = score # Puts new score + #print ('bunnyEatsCarrot(after): MaxScore'+str(maxScore)+' score: '+str(maxScore)) + pygame.display.update() + waitPlayerPressKey(self) else: - # Draw playerpygame.time.wait(2000) rectangle - - surface.blit(bunnyImage, rectBunny) - - pygame.display.update() - - mainClock.tick(FPS) - - pygame.mixer.music.stop() - - pygame.display.update() - waitPlayerPressKey(self) - #correctAnswerSound.stop() - gameOverSound.stop() - + surface.blit(sad, rectBunny) + #surface.blit(sad, rectBunny) + score=returnScore() + drawText(_('RESPUESTA INCORRECTA!'), font, surface, (WINDOWWIDTH / 4), (WINDOWHEIGHT / 2.25), TEXTCOLOR) + drawText(_('RESPUESTA CORRECTA: '+str(result)), font, surface, (WINDOWWIDTH / 3.8), (WINDOWHEIGHT / 2.25)+50, TEXTCOLOR) + + drawText(_('PULSA UNA TECLA'), font, surface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 2.25) + 100, TEXTCOLOR) + musicSound.stop() + gameOverSound.play() + pygame.display.update() + waitPlayerPressKey(self) + #print ('bunnyEatsCarrot(after): MaxScore'+str(maxScore)+' score: '+str(maxScore)) + else: + # Draw playerpygame.time.wait(2000) rectangle + surface.blit(bunnyImage, rectBunny) ######################################## methods #Time Limit -TIMELIMIT=240.0 -#TIMELIMIT=30.0 +#TIMELIMIT=240.0 +TIMELIMIT=10.0 #print ('time: '+str(TIMESTART)) def compareTime(playTime, gamestate): @@ -452,7 +446,9 @@ def compareTime(playTime, gamestate): if(c>TIMELIMIT): #print ('Time Limit!') #gamestate.set_running( False ) - gamestate.go_to_next_state() + gamestate.go_to_next_state() + return True + return False #Loading Image Method def load_image(fileName): @@ -465,10 +461,10 @@ def load_image(fileName): #Wait Player to Press Key Method def waitPlayerPressKey(gamestate): + pygame.time.wait(300) + pygame.event.clear() while True: for event in pygame.event.get(): - pygame.time.wait(300) - pygame.event.clear() if event.type == QUIT: gamestate.set_running( False ) if event.type == KEYDOWN: @@ -534,7 +530,7 @@ def returnScore(): return a -#Draw text on the screenine +#Draw text on the screen def drawText(text, font, surface, x, y, color): text = font.render(text, 1, color) rectText = text.get_rect() -- cgit v0.9.1