2D游戏作业:贪吃蛇

1.游戏介绍

这款游戏通过上下左右键来引导贪吃蛇吃到食物从而贪吃蛇越变越长,当贪吃蛇碰到窗口的四壁是贪吃蛇撞死从而游戏结束。

2. 开发工具

Visual Studio Code

3.部分代码介绍

导入模块

1import pygame,sys,time,random 2from pygame.locals import *

设置窗口

1pygame.init() 2fpsClock = pygame.time.Clock() 3playSurface = pygame.display.set_mode((640,480)) 4pygame.display.set_caption('贪吃蛇游戏')

定义颜色

1redColour = pygame.Color(255,0,0) 2blackColour = pygame.Color(0,0,0) 3whiteColour = pygame.Color(255,255,255) 4greyColour = pygame.Color(150,150,150)

初始化变量

1snakePosition = [100, 100] 2snakeSegments = [[100, 100], [80, 100], [60, 100]] 3raspberryPosition = [300, 300] #位置 4raspberrySpawned = 1 #是否吃到树莓 5direction = 'right' 6changeDirection = direction

游戏结束方法

1def gameOver(): 2 gameOverFont = pygame.font.Font('simfang.ttf',72) 3 gameOverSurf = gameOverFont.render('Game Over', True, greyColour) 4 gameOverRect = gameOverSurf.get_rect() 5 gameOverRect.midtop = (320, 10) 6 playSurface.blit(gameOverSurf, gameOverRect) 7 pygame.display.flip() 8 time.sleep(5) 9 pygame.quit() 10 sys.exit()

贪吃蛇移动

1while True: 2 for event in pygame.event.get(): 3 if event.type == QUIT: 4 pygame.quit() 5 sys.exit() 6 elif event.type == KEYDOWN: 7 8 if event.key == K_RIGHT or event.key == ord('d'): 9 changeDirection = 'right' 10 if event.key == K_LEFT or event.key == ord('a'): 11 changeDirection = 'left' 12 if event.key == K_UP or event.key == ord('w'): 13 changeDirection = 'up' 14 if event.key == K_DOWN or event.key == ord('s'): 15 changeDirection = 'down' 16 if event.key == K_ESCAPE: 17 pygame.event.post(pygame.event.Event(QUIT)) 18 if changeDirection == 'right' and not direction == 'left': 19 direction = changeDirection 20 if changeDirection == 'left' and not direction == 'right': 21 direction = changeDirection 22 if changeDirection == 'up' and not direction == 'down': 23 direction = changeDirection 24 if changeDirection == 'down' and not direction == 'up': 25 direction = changeDirection 26 # 根据方向移动蛇头的坐标 27 if direction == 'right': 28 snakePosition[0] += 20 29 if direction == 'left': 30 snakePosition[0] -= 20 31 if direction == 'up': 32 snakePosition[1] -= 20 33 if direction == 'down': 34 snakePosition[1] += 20

4.参考代码

1import pygame,sys,time,random 2from pygame.locals import * 3pygame.init() 4fpsClock = pygame.time.Clock() 5playSurface = pygame.display.set_mode((640,480)) 6pygame.display.set_caption('贪吃蛇游戏') 7 8redColour = pygame.Color(255,0,0) 9blackColour = pygame.Color(0,0,0) 10whiteColour = pygame.Color(255,255,255) 11greyColour = pygame.Color(150,150,150) 12 13snakePosition = [100, 100] 14snakeSegments = [[100, 100], [80, 100], [60, 100]] 15raspberryPosition = [300, 300] #位置 16raspberrySpawned = 1 #是否吃到树莓 17direction = 'right' 18changeDirection = direction 19 20def gameOver(): 21 gameOverFont = pygame.font.Font('simfang.ttf',72) 22 gameOverSurf = gameOverFont.render('Game Over', True, greyColour) 23 gameOverRect = gameOverSurf.get_rect() 24 gameOverRect.midtop = (320, 10) 25 playSurface.blit(gameOverSurf, gameOverRect) 26 pygame.display.flip() 27 time.sleep(5) 28 pygame.quit() 29 sys.exit() 30while True: 31 for event in pygame.event.get(): 32 if event.type == QUIT: 33 pygame.quit() 34 sys.exit() 35 elif event.type == KEYDOWN: 36 37 if event.key == K_RIGHT or event.key == ord('d'): 38 changeDirection = 'right' 39 if event.key == K_LEFT or event.key == ord('a'): 40 changeDirection = 'left' 41 if event.key == K_UP or event.key == ord('w'): 42 changeDirection = 'up' 43 if event.key == K_DOWN or event.key == ord('s'): 44 changeDirection = 'down' 45 if event.key == K_ESCAPE: 46 pygame.event.post(pygame.event.Event(QUIT)) 47 if changeDirection == 'right' and not direction == 'left': 48 direction = changeDirection 49 if changeDirection == 'left' and not direction == 'right': 50 direction = changeDirection 51 if changeDirection == 'up' and not direction == 'down': 52 direction = changeDirection 53 if changeDirection == 'down' and not direction == 'up': 54 direction = changeDirection 55 56 if direction == 'right': 57 snakePosition[0] += 20 58 if direction == 'left': 59 snakePosition[0] -= 20 60 if direction == 'up': 61 snakePosition[1] -= 20 62 if direction == 'down': 63 snakePosition[1] += 20 64 65 snakeSegments.insert(0, list(snakePosition)) 66 67 if snakePosition[0] == raspberryPosition[0] and snakePosition[1] ==raspberryPosition[1]: 68 raspberrySpawned = 0 69 else: 70 snakeSegments.pop() 71 72 if raspberrySpawned == 0: 73 x = random.randrange(1,32) 74 y = random.randrange(1, 24) 75 raspberryPosition = [int(x * 20), int(y * 20)] 76 raspberrySpawned = 1 77 playSurface.fill(blackColour) 78 for position in snakeSegments: 79 pygame.draw.rect(playSurface, whiteColour, Rect(position[0], position[1], 20, 20)) 80 pygame.draw.rect(playSurface, redColour, Rect(raspberryPosition[0], raspberryPosition[1], 20, 20)) 81 pygame.display.flip() 82 if snakePosition[0] > 620 or snakePosition[0] < 0: 83 gameOver() 84 if snakePosition[1] > 460 or snakePosition[1] < 0: 85 gameOver() 86 for snakeBody in snakeSegments[1:]: 87 if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: 88 gameOver() 89 fpsClock.tick(10)

5.结果展示

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

使用Java 语言编写一个贪吃蛇游戏

使用Java语言编写一个贪吃蛇游戏,代码如下:GreedSnake.javapackagejzjsfx;publicclassGreedSnakepublicstaticvoidmain(Stringargs)SnakeModelmodelnewSnakeModel(20,30);SnakeControlc

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )