1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| def main(): pygame.init() screen_size = (SCREEN_X,SCREEN_Y) screen = pygame.display.set_mode(screen_size) pygame.display.set_caption('Snake') clock = pygame.time.Clock() scores = 0 isdead = False snake = Snake() food = Food() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: snake.changedirection(event.key) if event.key == pygame.K_SPACE and isdead: return main() screen.fill((255,255,255)) if not isdead: scores+=1 snake.move() for rect in snake.body: pygame.draw.rect(screen,(20,220,39),rect,0) isdead = snake.isdead() if isdead: show_text(screen,(100,200),'YOU DEAD!',(227,29,18),False,100) show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30) if food.rect == snake.body[0]: scores+=50 food.remove() snake.addnode() food.set() pygame.draw.rect(screen,(136,0,21),food.rect,0) show_text(screen,(50,500),'Scores: '+str(scores),(223,223,223)) pygame.display.update() clock.tick(10) if __name__ == '__main__': main()
|