Adding a play again option to python game -
i'm working on making game programming class using python. don't know how give option player again when lose, or quit game. using python 2.7. code game:
import pygame, sys, time, random pygame.locals import * # set pygame pygame.init() mainclock = pygame.time.clock() # set window windowwidth = 1000 windowheight = 500 windowsurface = pygame.display.set_mode((windowwidth, windowheight), 0, 32) pygame.display.set_caption('dankest memes') # set colors pink = (223, 61, 163) textcolor = (223, 61, 163) def waitforplayertopresskey(): while true: event in pygame.event.get(): if event.type == quit: terminate() if event.type == keydown: if event.key == k_escape: # pressing escape quits terminate() return def terminate(): while true: event in pygame.event.get(): if event.type == quit: terminate() if event.type == keydown: if event.key == k_escape: # pressing escape quits terminate() return 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) font = pygame.font.sysfont(none, 48) textcolor = (255,250,250) score=4 # set fonts font = pygame.font.sysfont(none, 48) myscore=4 # set block data structure file = 'score.txt' player = pygame.rect(300, 100, 40, 40) playerimage = pygame.image.load('player.png') playerstretchedimage = pygame.transform.scale(playerimage, (40, 40)) foodimage = pygame.image.load('meme.png') foods = [] in range(20): foods.append(pygame.rect(random.randint(0, windowwidth - 20), random.randint(0, windowheight - 20), 20, 20)) foodcounter = 0 newfood = 40 baddie = pygame.rect(300, 100, 40, 40) baddieimage = pygame.image.load('baddie.png') baddiestretchedimage = pygame.transform.scale(baddieimage, (40, 40)) baddies = [] in range(20): baddies.append(pygame.rect(random.randint(0, windowwidth - 20), random.randint(0, windowheight - 20), 20, 20)) baddiecounter = 0 newbaddie = 120 # set keyboard variables moveleft = false moveright = false moveup = false movedown = false movespeed = 6 # set music pickupsound = pygame.mixer.sound('pickup.wav') pygame.mixer.music.load('background.mp3') pygame.mixer.music.play(-1, 0.0) musicplaying = true # show "start" screen drawtext('dankest memes', font, windowsurface, (windowwidth / 3), (windowheight / 3)) drawtext('press key start.', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 50) drawtext('move wasd or arrow keys.', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 100) drawtext('collect green pepes points.', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 150) drawtext('avoid chickens.', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 200) pygame.display.update() waitforplayertopresskey() # run game loop while true: # check quit event event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if event.type == keydown: # change keyboard variables if event.key == k_left or event.key == ord('a'): moveright = false moveleft = true if event.key == k_right or event.key == ord('d'): moveleft = false moveright = true if event.key == k_up or event.key == ord('w'): movedown = false moveup = true if event.key == k_down or event.key == ord('s'): moveup = false movedown = true if event.type == keyup: if event.key == k_escape: pygame.quit() sys.exit() if event.key == k_left or event.key == ord('a'): moveleft = false if event.key == k_right or event.key == ord('d'): moveright = false 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 event.key == ord('x'): player.top = random.randint(0, windowheight - player.height) player.left = random.randint(0, windowwidth - player.width) if event.key == ord('m'): if musicplaying: pygame.mixer.music.stop() else: pygame.mixer.music.play(-1, 0.0) musicplaying = not musicplaying if event.type == mousebuttonup: foods.append(pygame.rect(event.pos[0] - 10, event.pos[1] - 10, 20, 20)) foodcounter += 1 if foodcounter >= newfood: # add new food foodcounter = 0 foods.append(pygame.rect(random.randint(0, windowwidth - 20), random.randint(0, windowheight - 20), 20, 20)) baddiecounter += 1 if baddiecounter >= newbaddie: baddiecounter = 0 baddies.append(pygame.rect(random.randint(0, windowwidth - 20), random.randint(0, windowheight - 20), 20, 20)) # draw pink background onto surface windowsurface.fill(pink) # move player if movedown , player.bottom < windowheight: player.top += movespeed if moveup , player.top > 0: player.top -= movespeed if moveleft , player.left > 0: player.left -= movespeed if moveright , player.right < windowwidth: player.right += movespeed drawtext('score: %s' % (score), font, windowsurface, 10,0) # draw block onto surface windowsurface.blit(playerstretchedimage, player) # check if block has intersected food squares. food in foods[:]: if player.colliderect(food): foods.remove(food) score+=2 if musicplaying: pickupsound.play() baddie in baddies[:]: if player.colliderect(baddie): baddies.remove(baddie) score-=4 if musicplaying: pickupsound.play() if score < 0: drawtext('you have lost', font, windowsurface, (windowwidth / 3), (windowheight / 3)) drawtext('press escape quit game', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 50) drawtext('press other key play again', font, windowsurface, (windowwidth / 3) - 50, (windowheight / 3) + 100) pygame.display.update() waitforplayertopresskey() # draw food food in foods: windowsurface.blit(foodimage, food) # draw baddie baddie in baddies: windowsurface.blit(baddieimage, baddie) # draw window onto screen pygame.display.update() mainclock.tick(40) if event.key == pygame.k_escape: pygame.quit() sys.exit()
the game simple. player moves around, collects food points , avoids baddies don't lose points. when score drops below 0, player loses. managed , end game screen, can't make play again or quit game after point.
first of, 1 suggestion. people more if show them you've tried.
anyhow.
what wrap entire game loop in function. so:
def game_loop(): while true: # check quit event event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() ....
next when ever game ends(i'm assuming have condition makes end), call game on screen function. in game on screen function, first fill window color, blit text screen wan show user, such "press key play again". check if key pressed , if call game loop function. need check if user trying close window.
then call game on screen function so:
#pseudo code if game_has_ended == true: game_over_screen()
Comments
Post a Comment