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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| import pygame import sys import time import random difficulty = 25 frame_size_x = 720 frame_size_y = 480 check_errors = pygame.init() pygame.display.set_caption('Lets Play') game_window = pygame.display.set_mode((frame_size_x, frame_size_y)) black = pygame.Color(0, 0, 0) white = pygame.Color(255, 255, 255) red = pygame.Color(255, 0, 0) green = pygame.Color(0, 255, 0) blue = pygame.Color(0, 0, 255) fps_controller = pygame.time.Clock() snake_pos = [ 100, 50] snake_body = [ [ 100, 50], [ 90, 50], [ 80, 50]] f_body = [ [ 40, 29], [ 40, 30], ... ... ... [ 624, 38]] food_pos = [ random.randrange(1, frame_size_x // 10) * 10, random.randrange(1, frame_size_y // 10) * 10] food_spawn = True direction = 'RIGHT' change_to = direction score = 0
def game_over(): OOOO000000O000OO0 = pygame.font.SysFont('times new roman', 90) O000O0OO0OO0O0O0O = OOOO000000O000OO0.render('YOU DIED', True, red) OOO00OO00O0O00O00 = O000O0OO0OO0O0O0O.get_rect() OOO00OO00O0O00O00.midtop = (frame_size_x / 2, frame_size_y / 4) game_window.fill(black) game_window.blit(O000O0OO0OO0O0O0O, OOO00OO00O0O00O00) show_score(0, red, 'times', 20) pygame.display.flip() time.sleep(3) pygame.quit() sys.exit()
def show_score(OO00000O0O000OO0O, O0O0O0OOOO000O000, O0O0000O00OO00OO0, O0O000OOOOOO00O0O): O00O000000OO0O0OO = pygame.font.SysFont(O0O0000O00OO00OO0, O0O000OOOOOO00O0O) O0O0O0O0O0O00O00O = O00O000000OO0O0OO.render('Score : ' + str(score), True, O0O0O0OOOO000O000) O000O0OOOO000OO0O = O0O0O0O0O0O00O00O.get_rect() if OO00000O0O000OO0O == 1: O000O0OOOO000OO0O.midtop = (frame_size_x / 10, 15) else: O000O0OOOO000OO0O.midtop = (frame_size_x / 2, frame_size_y / 1.25) game_window.blit(O0O0O0O0O0O00O00O, O000O0OOOO000OO0O)
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP or event.key == ord('w'): change_to = 'UP' if event.key == pygame.K_DOWN or event.key == ord('s'): change_to = 'DOWN' if event.key == pygame.K_LEFT or event.key == ord('a'): change_to = 'LEFT' if event.key == pygame.K_RIGHT or event.key == ord('d'): change_to = 'RIGHT' if event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) if change_to == 'UP' and direction != 'DOWN': direction = 'UP' if change_to == 'DOWN' and direction != 'UP': direction = 'DOWN' if change_to == 'LEFT' and direction != 'RIGHT': direction = 'LEFT' if change_to == 'RIGHT' and direction != 'LEFT': direction = 'RIGHT' if direction == 'UP': snake_pos[1] -= 10 if direction == 'DOWN': snake_pos[1] += 10 if direction == 'LEFT': snake_pos[0] -= 10 if direction == 'RIGHT': snake_pos[0] += 10 snake_body.insert(0, list(snake_pos)) if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]: score += 1 food_spawn = False else: snake_body.pop() if score == 1000: game_window.fill(black) for pos in f_body: pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 1, 1)) pygame.display.flip() time.sleep(10) elif not food_spawn: food_pos = [ random.randrange(1, frame_size_x // 10) * 10, random.randrange(1, frame_size_y // 10) * 10] food_spawn = True game_window.fill(black) for pos in snake_body: pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10)) pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10)) if snake_pos[0] < 0 or snake_pos[0] > frame_size_x - 10: game_over() if snake_pos[1] < 0 or snake_pos[1] > frame_size_y - 10: game_over() for block in snake_body[1:]: if snake_pos[0] == block[0] and snake_pos[1] == block[1]: game_over() show_score(1, white, 'consolas', 20) pygame.display.update() fps_controller.tick(difficulty) continue
|