python游戏贪吃蛇csdn_pygame:做一个简单的python贪吃蛇小游戏

最近发现大家都比较喜欢用python做一些游戏实验,本篇小编为大家带来最常玩的贪吃蛇。

贪吃蛇游戏链接:

下载pygame模块pip install pygame

编写的是最简单的贪吃蛇游戏(实现最基本的功能)

效果图:

附上代码: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  0):  # 判断死亡(撞墙或自食)

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 

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
Python是一种高级编程语言,非常适合编写小游戏。下面是一个简单贪吃游戏的示例代码: ```python import pygame import random pygame.init() # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) # 定义游戏窗口大小和标题 dis_width = 800 dis_height = 600 dis = pygame.display.set_mode((dis_width, dis_height)) pygame.display.set_caption('Snake Game by CSDN C知道') # 定义时间和速度 clock = pygame.time.Clock() snake_block = 10 snake_speed = 30 font_style = pygame.font.SysFont(None, 50) def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, green, [x, x, snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) def gameLoop(): game_over = False game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: dis.fill(blue) message("You Lost! Press Q-Quit or C-Play Again", red) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change dis.fill(blue) pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop() ``` 运行这段代码后,你可以开始玩贪吃游戏了。如果想要退出游戏,可以按下Q键。如果想要重新开始游戏,可以按下C键。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值