程序运行截图:


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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | import pygame as pg from random import randint import sys from pygame.locals import * FPS = 6 # 画面帧数,代表蛇的移动速率 window_width = 600 window_height = 500 cellsize = 20 cell_width = int(window_width / cellsize) cell_height = int(window_height / cellsize) BGcolor = (0, 0, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) apple_color = (255, 0, 0) snake_color = (0, 150, 0) GREEN = (0, 255, 0) WHITE = (255, 255, 255) DARKGRAY = (40, 40, 40) UP = "up" DOWN = "down" LEFT = "left" RIGHT = "right" HEAD = 0 def main(): # 有函数 global FPSclock, window, BASICFONT pg.init() FPSclock = pg.time.Clock() window = pg.display.set_mode((window_width, window_height)) BASICFONT = pg.font.Font("freesansbold.ttf", 18) pg.display.set_caption("贪吃蛇") showStartScreen() while True: runGame() showGameOverScreen() def runGame(): # 运行游戏函数 startx = randint(5, cell_width - 6) starty = randint(5, cell_height - 6) snakeCoords = [{"x": startx, "y": starty}, {"x": startx - 1, "y": starty}, {"x": startx - 2, "y": starty}] direction = RIGHT apple = getRandomLocation() while True: for event in pg.event.get(): if event.type == QUIT: terminate() elif event.type == KEYDOWN: if event.key == K_LEFT and direction != RIGHT: direction = LEFT elif event.key == K_RIGHT and direction != LEFT: direction = RIGHT elif event.key == K_UP and direction != DOWN: direction = UP elif event.key == K_DOWN and direction != UP: direction = DOWN elif event.key == K_ESCAPE: terminate() if snakeCoords[HEAD]["x"] == -1 or snakeCoords[HEAD]["x"] == cell_width or snakeCoords[HEAD]["y"] == -1 or \ snakeCoords[HEAD]["y"] == cell_height: return for snakeBody in snakeCoords[1:]: if snakeBody["x"] == snakeCoords[HEAD]["x"] and snakeBody["y"] == snakeCoords[HEAD]["y"]: return if snakeCoords[HEAD]["x"] == apple["x"] and snakeCoords[HEAD]["y"] == apple["y"]: apple = getRandomLocation() else: del snakeCoords[-1] if direction == UP: newHead = {"x": snakeCoords[HEAD]["x"], "y": snakeCoords[HEAD]["y"] - 1} elif direction == DOWN: newHead = {"x": snakeCoords[HEAD]["x"], "y": snakeCoords[HEAD]["y"] + 1} elif direction == LEFT: newHead = {"x": snakeCoords[HEAD]["x"] - 1, "y": snakeCoords[HEAD]["y"]} elif direction == RIGHT: newHead = {"x": snakeCoords[HEAD]["x"] + 1, "y": snakeCoords[HEAD]["y"]} snakeCoords.insert(0, newHead) window.fill(BGcolor) drawGrid() drawSnake(snakeCoords) drawApple(apple) drawScore(len(snakeCoords) - 3) pg.display.update() FPSclock.tick(FPS) def drawPressKeyMsg(): # 游戏开始提示信息 pressKeySurf = BASICFONT.render("press a key to play", True, BLUE) pressKeyRect = pressKeySurf.get_rect() pressKeyRect.topleft = (window_width - 200, window_height - 30) window.blit(pressKeySurf, pressKeyRect) def checkForKeyPress(): # 检查是否触发按键 if len(pg.event.get(QUIT)) > 0: terminate() keyUpEvents = pg.event.get(KEYUP) if len(keyUpEvents) == 0: return None if keyUpEvents[0].key == K_ESCAPE: terminate() return keyUpEvents[0].key def showStartScreen(): # 开始画面 window.fill(BGcolor) titleFont = pg.font.Font("freesansbold.ttf", 100) titleSurf = titleFont.render("snake!", True, RED) titleRect = titleSurf.get_rect() titleRect.center = (window_width / 2, window_height / 2) window.blit(titleSurf, titleRect) drawPressKeyMsg() pg.display.update() while True: if checkForKeyPress(): pg.event.get() return def terminate(): # 退出 pg.quit() sys.exit() def getRandomLocation(): # 出现位置 return {"x": randint(0, cell_width - 1), "y": randint(0, cell_height - 1)} def showGameOverScreen(): # 游戏结束 gameOverFont = pg.font.Font("freesansbold.tff", 150) gameSurf = gameOverFont.render("Game", True, WHITE) overSurf = gameOverFont.render("over", True, WHITE) gameRect = gameSurf.get_rect() overRect = overSurf.get_rect() gameRect.midtop = (window_width / 2, 10) overRect.midtop = (window_width / 2, gameRect.height10 + 25) window.blit(gameSurf, gameRect) window.blit(overSurf, overRect) drawPressKeyMsg() pg.display.update() pg.time.wait(500) checkForKeyPress() while True: if checkForKeyPress(): pg.event.get() return def drawScore(score): # 显示分数 scoreSurf = BASICFONT.render("Score:%s" % (score), True, WHITE) scoreRect = scoreSurf.get_rect() scoreRect.topleft = (window_width - 120, 10) window.blit(scoreSurf, scoreRect) def drawSnake(snakeCoords): # 画蛇 for coord in snakeCoords: x = coord["x"] * cellsize y = coord["y"] * cellsize snakeSegmentRect = pg.Rect(x, y, cellsize, cellsize) pg.draw.rect(window, snake_color, snakeSegmentRect) snakeInnerSegmentRect = pg.Rect(x + 4, y + 4, cellsize - 8, cellsize - 8) pg.draw.rect(window, GREEN, snakeInnerSegmentRect) def drawApple(coord): x = coord["x"] * cellsize y = coord["y"] * cellsize appleRect = pg.Rect(x, y, cellsize, cellsize) pg.draw.rect(window, apple_color, appleRect) def drawGrid(): # 画方格 for x in range(0, window_width, cellsize): pg.draw.line(window, DARKGRAY, (x, 0), (x, window_height)) for y in range(0, window_height, cellsize): pg.draw.line(window, DARKGRAY, (0, y), (window_width, y)) if __name__ == "__main__": main() |
更多Python小游戏源代码,请微信关注:Python代码大全
