i have gone over every inch of this code i can not find out what is causing it to go into a loop. once i press key to start it plays the start noise and then it goes to the end screen and says press key to play again and i press it and it goes to start the game and plays the soung and one tick of the game and says game over.
import pygame, random, sys
from pygame.locals import *
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BGC = (0, 0, 0)
FPS = 80
PLAYERMOVERATE = 5
countBy = 10
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
def playerHasHitShoot(playerRect, shoot):
if playerRect.colliderect(shoot):
return True
return False
def baddieHasHitShoot(baddieRect, shoot):
if baddieRect.colliderect(shoot):
return True
return False
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def fire(shoot):
shootRect.topleft = ((playerRect / 2), 101)
shootRect.move_ip(5, 1)
pygame.init()
pygame.font.init()
font = pygame.font.SysFont(None,48)
mainClock = pygame.time.Clock()
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Mothers Day')
pygame.mouse.set_visible(True)
pygame.display.set_icon(pygame.image.load('gameicon.gif'))
gameStartSound = pygame.mixer.Sound('gamestart.wav')
gameOverSound = pygame.mixer.Sound('gamestart.wav')
pygame.mixer.music.load('background.wav')
playerImage = pygame.image.load('starship.bmp')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('enemy.bmp')
baddieRect = baddieImage.get_rect()
shootImage = pygame.image.load('shoot.bmp')
shootRect = shootImage.get_rect()
drawText('Star Trek', font, screen, (WINDOWWIDTH / 2.5), (WINDOWHEIGHT / 3))
drawText('Press a key to start.', font, screen, (WINDOWWIDTH / 3.75) - 30, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
topScore = 10000
while True:
score = 10000
playerRect.topleft = (0, 100)
baddieRect.topright = (600, 100)
moveUp = moveDown = False
pygame.mixer.music.play(-1, 0.0)
while True:
score -= countBy
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_UP:
moveDown = False
moveUp = True
if event.key == ord(' '):
fire
if event.key == K_DOWN:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == ord(' '):
fire
if event.key == K_ESCAPE:
terminate()
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if moveUp and playerRect.top > 0:
playerRect.move_ip(0, -1 * PLAYERMOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0, PLAYERMOVERATE)
drawText('Score: %s' % (score), font, screen, 10, 0)
drawText('Top Score: %s' % (topScore), font, screen, 10, 40)
screen.blit(playerImage, playerRect)
screen.blit(shootImage, shootRect)
screen.blit(baddieImage, baddieRect)
pygame.display.update()
if playerHasHitShoot(playerRect, baddieRect):
score = 0
break
if baddieHasHitShoot(baddieRect, shoot):
if score > topScore:
score = topscore
break
mainClock.tick(FPS)
sb = pygame.image.load('sb.png')
screen.blit(sb, (600, 600))
screen.fill(BGC)
pygame.mixer.music.stop()
gameOverSound.play()
drawText('GAME OVER', font, screen, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to play again.', font, screen, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
gameOverSound.stop()
That
break
will be hit every frame, and immediately go to the 'Game Over' section at the end. I'm guessing you only intended this to happen ifplayerHasHitShoot
returned true? If so:It's less clear what the following bit (that's currently never running) is meant to do, but you should check and make sure all your conditional paths are correct: