import pygame,sys import random,math from pygame.locals import * pygame.init() FPS=60 fpsClock = pygame.time.Clock() playerImage = pygame.image.load('player.png') playerStretchedImage = pygame.transform.scale(playerImage, (40, 40)) playerRect=playerImage.get_rect() foodImage = pygame.image.load('cherry.png') font = pygame.font.SysFont(None,48) baddie=pygame.image.load('baddie.png') x=10 y=10 a,b=0,0 movex,movey=0,0 foodCounter = 0 NEWFOOD = 15 BADDIEMINSIZE = 10 BADDIEMAXSIZE = 40 BADDIEMINSPEED = 1 BADDIEMAXSPEED = 8 ADDNEWBADDIERATE = 6 foods = [] for i in range(20): foods.append(pygame.Rect(random.randint(0, 500 - 20), random.randint(0, 400 - 20), 20, 20)) white=(255,255,255) black=(0,0,0) windowSurface=pygame.display.set_mode((500,400),0,32) pygame.display.set_caption('move') def waitForPlayerToPressKey(): while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: # Pressing ESC quits. pygame.quit() sys.exit() return def drawText(text, font, surface, x, y): textobj = font.render(text, 1, black) textrect = textobj.get_rect() textrect.topleft = (x, y) surface.blit(textobj, textrect) topscore=0 while True: baddies=[] score=0 baddieAddCounter=0 while True: windowSurface.blit(playerStretchedImage,(x,y)) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() x, y = pygame.mouse.get_pos() pygame.mouse.set_visible(False) x -= playerImage.get_width() / 2 y -= playerImage.get_height() / 2 player = pygame.Rect(x, y, 40, 40) windowSurface.fill(white) foodCounter += 1 if foodCounter >= NEWFOOD: foodCounter = 0 foods.append(pygame.Rect(random.randint(0, 500- 20), random.randint(0, 400 - 20), 20, 20)) windowSurface.blit(playerStretchedImage,(x,y)) for food in foods[:]: if player.colliderect(food): foods.remove(food) score+=1 player = pygame.Rect(player.left, player.top, player.width + 2, player.height + 2) playerStretchedImage = pygame.transform.scale(playerImage, (player.width, player.height)) drawText('Score: %s' % (score), font, windowSurface, 10, 0) drawText('Top Score: %s' % (topscore), font, windowSurface, 10, 40) for food in foods: windowSurface.blit(foodImage, food) pygame.display.update() baddieAddCounter+=1 if baddieAddCounter == ADDNEWBADDIERATE: baddieAddCounter = 0 baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE) newBaddie = {'rect': pygame.Rect(random.randint(0, 500 - baddieSize), 0 - baddieSize, baddieSize,baddieSize), 'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED), 'surface': pygame.transform.scale(baddie, (baddieSize, baddieSize)), } baddies.append(newBaddie) for b in baddies: b['rect'].move_ip(0, b['speed']) for b in baddies[:]: if b['rect'].top > 400: baddies.remove(b) for b in baddies: windowSurface.blit(b['surface'], b['rect']) pygame.display.update() for b in baddies: if playerRect.colliderect(b['rect']): if score > topscore: topscore = score break break fpsClock.tick(FPS) drawText('GAME OVER', font, windowSurface, (500 / 3), (400 / 3)) drawText('Press a key to play again.', font, windowSurface, (500 / 3) - 80, (400 / 3) + 50) pygame.display.update() fpsClock.tick(FPS) waitForPlayerToPressKey()
相关分类