课程:《Python程序设计》
班级: 2213
姓名: 杨渝
学号:20221321
实验教师:王志强
实验日期:2023年5月22日
必修/选修: 公选课
1.实验内容
利用python的pygame模块写一个贪吃蛇小游戏。
2. 实验过程及结果
导入模块
import pygame, sys, time, random
利用模块定义颜色红,白,绿三色
color_red = pygame.Color(255, 0, 0)
color_white = pygame.Color(255, 255, 255)
color_green = pygame.Color(0, 255, 0)
定义窗口
screen = pygame.display.set_mode((600, 400))#窗口大小
screen.fill(color_white)#窗口颜色
pygame.display.set_caption("贪吃蛇")#窗口字样
蛇和食物
x = 10 # 蛇的初始x坐标
y = 10 # 蛇的初始y坐标
foodx = random.randint(1, 60) # 食物随机生成的x坐标
foody = random.randint(1, 40) # 食物随机生成的y坐标
arr[foodx][foody] = -1
snake_lon = 3 # 蛇的长度
way = 1 # 蛇的运动方向
循环开始
while True:
screen.fill(color_white)
time.sleep(0.1)
for event in pygame.event.get(): # 监听器
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_RIGHT) and (way != 2): # 向右移动且避免反向移动
way = 1
if (event.key == pygame.K_LEFT) and (way != 1): # 向左移动且避免反向移动
way = 2
if (event.key == pygame.K_UP) and (way != 4): # 向上移动且避免反向移动
way = 3
if (event.key == pygame.K_DOWN) and (way != 3): # 向下移动且避免反向移动
way = 4
if way == 1:
x += 1
if way == 2:
x -= 1
if way == 3:
y -= 1
if way == 4:
y += 1
if (x > 60) or (y > 40) or (x < 1) or (y < 1) or (arr[x][y] > 0): # 判断死亡(撞墙或自食)
print("You have died !")
sys.exit()
arr[x][y] = snake_lon
for a, b in enumerate(arr, 1):
for c, d in enumerate(b, 1):
# 在二维数组中,食物为-1,空地为0,蛇的位置为正数
if (d > 0):
# print(a,c) #输出蛇的当前坐标
arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1
pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (d < 0):
pygame.draw.rect(screen, color_red, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (x == foodx) and (y == foody): #蛇吃到食物
snake_lon += 1 #长度+1
while (arr[foodx][foody] != 0): #刷新食物
foodx = random.randint(1, 60)
foody = random.randint(1, 40)
arr[foodx][foody] = -1
pygame.display.update()
测试结果
Rec 0002
源码
import pygame, sys, time, random
color_red = pygame.Color(255, 0, 0)
color_white = pygame.Color(255, 255, 255)
color_green = pygame.Color(0, 255, 0)
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill(color_white)
pygame.display.set_caption("贪吃蛇")
arr = [([0] * 41) for i in range(61)] # 创建一个二维数组
x = 10 # 蛇的初始x坐标
y = 10 # 蛇的初始y坐标
foodx = random.randint(1, 60) # 食物随机生成的x坐标
foody = random.randint(1, 40) # 食物随机生成的y坐标
arr[foodx][foody] = -1
snake_lon = 3 # 蛇的长度
way = 1 # 蛇的运动方向
while True:
screen.fill(color_white)
time.sleep(0.1)
for event in pygame.event.get(): # 监听器
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_RIGHT) and (way != 2): # 向右移动且避免反向移动
way = 1
if (event.key == pygame.K_LEFT) and (way != 1): # 向左移动且避免反向移动
way = 2
if (event.key == pygame.K_UP) and (way != 4): # 向上移动且避免反向移动
way = 3
if (event.key == pygame.K_DOWN) and (way != 3): # 向下移动且避免反向移动
way = 4
if way == 1:
x += 1
if way == 2:
x -= 1
if way == 3:
y -= 1
if way == 4:
y += 1
if (x > 60) or (y > 40) or (x < 1) or (y < 1) or (arr[x][y] > 0): # 判断死亡(撞墙或自食)
print("You have died !")
sys.exit()
arr[x][y] = snake_lon
for a, b in enumerate(arr, 1):
for c, d in enumerate(b, 1):
# 在二维数组中,食物为-1,空地为0,蛇的位置为正数
if (d > 0):
# print(a,c) #输出蛇的当前坐标
arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1
pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (d < 0):
pygame.draw.rect(screen, color_red, ((a - 1) * 10, (c - 1) * 10, 10, 10))
if (x == foodx) and (y == foody): #蛇吃到食物
snake_lon += 1 #长度+1
while (arr[foodx][foody] != 0): #刷新食物
foodx = random.randint(1, 60)
foody = random.randint(1, 40)
arr[foodx][foody] = -1
pygame.display.update()
上传gitee图片
感想
上了一个学期的python,从开始见到王老师的那一刻,我就觉得这课应该很有意思,果不其然,不像其他编程语言那般死板,那么无趣,老师上课是开着玩笑的上,在大家积极性不是很高的时候就给大家开个小玩笑,讲课简洁,通俗易懂,就像python的特点一样,能一行代码讲完的事情,绝对不写两行,学了c语言的我乐开了花,觉得这门语言肯定很好学,于是中间有些懈怠了,成绩和努力是挂钩的,我在后面的爬虫学习就有些吃力了,这才感慨,万事万物都需要努力去学习,去了解,这是不变的真理。
最后一节课,老师拿着奖品来上课,嬉皮笑脸的对着我们,还一边说着不要嫌弃这些小礼品,真的,听到这,我就想起了高中班主任给我们道别的时候,大老爷们儿突然就有些小伤感,虽不说是泪流满面,但是眼睛还是有些湿润。
虽然最后我还是没有鼓起勇气举手回答问题拿下小奖品,虽然我学了一个学期还是在老师所说的领进门的状态,但是还是要感谢王老师,谢谢半年来的陪伴,让我在大学感受到了教师的温暖。