|
1 | | -z |
| 1 | +## 导入相关模块 |
| 2 | +import random |
| 3 | +import pygame |
| 4 | +import sys |
| 5 | + |
| 6 | +from pygame.locals import * |
| 7 | + |
| 8 | + |
| 9 | +snake_speed = 15 #贪吃蛇的速度 |
| 10 | +windows_width = 800 |
| 11 | +windows_height = 600 #游戏窗口的大小 |
| 12 | +cell_size = 20 #贪吃蛇身体方块大小,注意身体大小必须能被窗口长宽整除 |
| 13 | + |
| 14 | +''' #初始化区 |
| 15 | +由于我们的贪吃蛇是有大小尺寸的, 因此地图的实际尺寸是相对于贪吃蛇的大小尺寸而言的 |
| 16 | +''' |
| 17 | +map_width = int(windows_width / cell_size) |
| 18 | +map_height = int(windows_height / cell_size) |
| 19 | + |
| 20 | +# 颜色定义 |
| 21 | +white = (255, 255, 255) |
| 22 | +black = (0, 0, 0) |
| 23 | +gray = (230, 230, 230) |
| 24 | +dark_gray = (40, 40, 40) |
| 25 | +DARKGreen = (0, 155, 0) |
| 26 | +Green = (0, 255, 0) |
| 27 | +Red = (255, 0, 0) |
| 28 | +blue = (0, 0, 255) |
| 29 | +dark_blue =(0,0, 139) |
| 30 | + |
| 31 | + |
| 32 | +BG_COLOR = black #游戏背景颜色 |
| 33 | + |
| 34 | +# 定义方向 |
| 35 | +UP = 1 |
| 36 | +DOWN = 2 |
| 37 | +LEFT = 3 |
| 38 | +RIGHT = 4 |
| 39 | + |
| 40 | +HEAD = 0 #贪吃蛇头部下标 |
| 41 | +#主函数 |
| 42 | +def main(): |
| 43 | + pygame.init() # 模块初始化 |
| 44 | + snake_speed_clock = pygame.time.Clock() # 创建Pygame时钟对象 |
| 45 | + screen = pygame.display.set_mode((windows_width, windows_height)) # |
| 46 | + screen.fill(white) |
| 47 | + |
| 48 | + pygame.display.set_caption("Python 贪吃蛇小游戏") #设置标题 |
| 49 | + show_start_info(screen) #欢迎信息 |
| 50 | + while True: |
| 51 | + running_game(screen, snake_speed_clock) |
| 52 | + show_gameover_info(screen) |
| 53 | +#游戏运行主体 |
| 54 | +def running_game(screen,snake_speed_clock): |
| 55 | + startx = random.randint(3, map_width - 8) #开始位置 |
| 56 | + starty = random.randint(3, map_height - 8) |
| 57 | + snake_coords = [{'x': startx, 'y': starty}, #初始贪吃蛇 |
| 58 | + {'x': startx - 1, 'y': starty}, |
| 59 | + {'x': startx - 2, 'y': starty}] |
| 60 | + |
| 61 | + direction = RIGHT # 开始时向右移动 |
| 62 | + |
| 63 | + food = get_random_location() #实物随机位置 |
| 64 | + |
| 65 | + while True: |
| 66 | + for event in pygame.event.get(): |
| 67 | + if event.type == QUIT: |
| 68 | + terminate() |
| 69 | + elif event.type == KEYDOWN: |
| 70 | + if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT: |
| 71 | + direction = LEFT |
| 72 | + elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT: |
| 73 | + direction = RIGHT |
| 74 | + elif (event.key == K_UP or event.key == K_w) and direction != DOWN: |
| 75 | + direction = UP |
| 76 | + elif (event.key == K_DOWN or event.key == K_s) and direction != UP: |
| 77 | + direction = DOWN |
| 78 | + elif event.key == K_ESCAPE: |
| 79 | + terminate() |
| 80 | + |
| 81 | + move_snake(direction, snake_coords) #移动蛇 |
| 82 | + |
| 83 | + ret = snake_is_alive(snake_coords) |
| 84 | + if not ret: |
| 85 | + break #蛇跪了. 游戏结束 |
| 86 | + snake_is_eat_food(snake_coords, food) #判断蛇是否吃到食物 |
| 87 | + |
| 88 | + screen.fill(BG_COLOR) |
| 89 | + #draw_grid(screen) |
| 90 | + draw_snake(screen, snake_coords) |
| 91 | + draw_food(screen, food) |
| 92 | + draw_score(screen, len(snake_coords) - 3) |
| 93 | + pygame.display.update() |
| 94 | + snake_speed_clock.tick(snake_speed) #控制fps |
| 95 | +#将食物画出来 |
| 96 | +def draw_food(screen, food): |
| 97 | + x = food['x'] * cell_size |
| 98 | + y = food['y'] * cell_size |
| 99 | + appleRect = pygame.Rect(x, y, cell_size, cell_size) |
| 100 | + pygame.draw.rect(screen, Red, appleRect) |
| 101 | +#将贪吃蛇画出来 |
| 102 | +def draw_snake(screen, snake_coords): |
| 103 | + for coord in snake_coords: |
| 104 | + x = coord['x'] * cell_size |
| 105 | + y = coord['y'] * cell_size |
| 106 | + wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size) |
| 107 | + pygame.draw.rect(screen, dark_blue, wormSegmentRect) |
| 108 | + wormInnerSegmentRect = pygame.Rect( #蛇身子里面的第二层亮绿色 |
| 109 | + x + 4, y + 4, cell_size - 8, cell_size - 8) |
| 110 | + pygame.draw.rect(screen, blue, wormInnerSegmentRect) |
| 111 | +#移动贪吃蛇 |
| 112 | +def move_snake(direction, snake_coords): |
| 113 | + if direction == UP: |
| 114 | + newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1} |
| 115 | + elif direction == DOWN: |
| 116 | + newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1} |
| 117 | + elif direction == LEFT: |
| 118 | + newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']} |
| 119 | + elif direction == RIGHT: |
| 120 | + newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']} |
| 121 | + |
| 122 | + snake_coords.insert(0, newHead) |
| 123 | +#判断蛇死了没 |
| 124 | +def snake_is_alive(snake_coords): |
| 125 | + tag = True |
| 126 | + if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \ |
| 127 | + snake_coords[HEAD]['y'] == map_height: |
| 128 | + tag = False # 蛇碰壁啦 |
| 129 | + for snake_body in snake_coords[1:]: |
| 130 | + if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']: |
| 131 | + tag = False # 蛇碰到自己身体啦 |
| 132 | + return tag |
| 133 | +#判断贪吃蛇是否吃到食物 |
| 134 | +def snake_is_eat_food(snake_coords, food): #如果是列表或字典,那么函数内修改参数内容,就会影响到函数体外的对象。 |
| 135 | + if snake_coords[HEAD]['x'] == food['x'] and snake_coords[HEAD]['y'] == food['y']: |
| 136 | + food['x'] = random.randint(0, map_width - 1) |
| 137 | + food['y'] = random.randint(0, map_height - 1) # 实物位置重新设置 |
| 138 | + else: |
| 139 | + del snake_coords[-1] # 如果没有吃到实物, 就向前移动, 那么尾部一格删掉 |
| 140 | +#食物随机生成 |
| 141 | +def get_random_location(): |
| 142 | + return {'x': random.randint(0, map_width - 1), 'y': random.randint(0, map_height - 1)} |
| 143 | +#开始信息显示 |
| 144 | +def show_start_info(screen): |
| 145 | + font = pygame.font.Font('myfont.ttf', 40) |
| 146 | + tip = font.render('按任意键开始游戏~~~', True, (65, 105, 225)) |
| 147 | + gamestart = pygame.image.load('gamestart.png') |
| 148 | + screen.blit(gamestart, (140, 30)) |
| 149 | + screen.blit(tip, (240, 550)) |
| 150 | + pygame.display.update() |
| 151 | + |
| 152 | + while True: #键盘监听事件 |
| 153 | + for event in pygame.event.get(): # event handling loop |
| 154 | + if event.type == QUIT: |
| 155 | + terminate() #终止程序 |
| 156 | + elif event.type == KEYDOWN: |
| 157 | + if (event.key == K_ESCAPE): #终止程序 |
| 158 | + terminate() #终止程序 |
| 159 | + else: |
| 160 | + return #结束此函数, 开始游戏 |
| 161 | +#游戏结束信息显示 |
| 162 | +def show_gameover_info(screen): |
| 163 | + font = pygame.font.Font('myfont.ttf', 40) |
| 164 | + tip = font.render('按Q或者ESC退出游戏, 按任意键重新开始游戏~', True, (65, 105, 225)) |
| 165 | + gamestart = pygame.image.load('gameover.png') |
| 166 | + screen.blit(gamestart, (60, 0)) |
| 167 | + screen.blit(tip, (80, 300)) |
| 168 | + pygame.display.update() |
| 169 | + |
| 170 | + while True: #键盘监听事件 |
| 171 | + for event in pygame.event.get(): # event handling loop |
| 172 | + if event.type == QUIT: |
| 173 | + terminate() #终止程序 |
| 174 | + elif event.type == KEYDOWN: |
| 175 | + if event.key == K_ESCAPE or event.key == K_q: #终止程序 |
| 176 | + terminate() #终止程序 |
| 177 | + else: |
| 178 | + return #结束此函数, 重新开始游戏 |
| 179 | +#画成绩 |
| 180 | +def draw_score(screen,score): |
| 181 | + font = pygame.font.Font('myfont.ttf', 30) |
| 182 | + scoreSurf = font.render('得分: %s' % score, True, Green) |
| 183 | + scoreRect = scoreSurf.get_rect() |
| 184 | + scoreRect.topleft = (windows_width - 120, 10) |
| 185 | + screen.blit(scoreSurf, scoreRect) |
| 186 | +#程序终止 |
| 187 | +def terminate(): |
| 188 | + pygame.quit() |
| 189 | + sys.exit() |
| 190 | +main() |
0 commit comments