# 基本内容
import pygame
import random
import PySimpleGUI as sg
# 初始化pygame
pygame.init()
pygame.mixer.init()
# 颜色参数
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
ching = (0, 128, 128)
purple = (160, 32, 240)
grey = (128, 128, 128)
orange = (255, 153, 51)
# 屏幕大小参数
dis_width = 700
dis_height = 500
# 音乐
# eat_music = pygame.mixer.Sound("eat.wav")
# end_music = pygame.mixer.Sound("end.wav")
# 屏幕大小设置
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇小游戏')
pygame.display.set_palette
# 帧数设置
clock = pygame.time.Clock()
pygame.init()
# 字体参数设置
font_style = pygame.font.SysFont(['simHei', "bahnschrift"], 25)
score_font = pygame.font.SysFont('simHei', 35)
lost_style = pygame.font.SysFont('simHei', 50)
a = pygame.font.get_fonts()
# 速度输出函数
def Your_speed(speed):
s = score_font.render("你的速度:{}".format(speed), True, red)
dis.blit(s, [400, 0])
pygame.display.update()
# 得分输出函数
def Your_score(score):
value = score_font.render("你的分数:" + str(score), True, blue)
dis.blit(value, [0, 0])
# pygame.display.update()
# 消息输出函数
def message(msg, color, ft, pos):
mesg = ft.render(msg, True, color)
dis.blit(mesg, pos)
hard = 1.5
bool = False
def start():
sg.theme("SystemDefault")
layout = [[sg.Text(" 贪吃蛇小游戏", font=("微软雅黑", 50))],
[sg.Text()],
[sg.Text(" ", font=("微软雅黑", 40)), sg.Button("开始游戏", font=("微软雅黑", 40))],
[sg.Text()],
[sg.Text(" ", font=("微软雅黑", 40)), sg.Button(" 备注 ", font=("微软雅黑", 40))],
[sg.Text()],
[sg.Text(" ", font=("微软雅黑", 40)), sg.Button(" 退出 ", font=("微软雅黑", 40))]]
window = sg.Window("开始", layout, size=(800, 600))
while True:
event, values = window.read()
if event in (None, " 退出 "):
exit()
if event in ("开始游戏"):
bool = True
break
if event in (" 备注 "):
sg.Print("游戏有四种颜色的食物,且每过一段时间会生成新的食物:")
sg.Print("1.绿色:分数加1,无特殊变化")
sg.Print("2.紫色:分数加2,但长度加2")
sg.Print("3.蓝色:分数加1,速度增加")
sg.Print("4.橙色:分数加1,速度降低")
sg.Print("基础操作:键盘的上下左右键控制运动")
window.close()
return bool
class Snake:
def __init__(self, x1, y1):
self.x1 = x1
self.y1 = y1
self.block = 10
self.speed = 10
self.length = 1
self.List = []
self.Head = []
def our_snake(self, game_close, x1, y1, block, snake_list):
if (game_close == False):
if (60 <= y1 <= 460):
pygame.draw.rect(dis, black, [x1, y1, block, block])
for x in snake_list[1:-1]:
pygame.draw.rect(dis, grey, [x[0], x[1], block, block])
pygame.display.update()
class Food:
def __init__(self, snake_block, snake, List):
same = False
self.foodx = round(random.randrange(70, dis_width - snake_block - 60) / 10.0) * 10.0
self.foody = round(random.randrange(70, dis_height - snake_block - 60) / 10.0) * 10.0
for i in snake.List:
if i == [self.foodx, self.foody]:
same = True
self.type = random.randint(0, 3)
self.Head = [self.foodx, self.foody, self.type]
while same:
self.foodx = round(random.randrange(70, dis_width - snake_block - 60) / 10.0) * 10.0
self.foody = round(random.randrange(70, dis_height - snake_block - 60) / 10.0) * 10.0
for i in snake.List:
if i == [self.foodx, self.foody]:
same = True
self.block = snake_block
def appear(self, list):
for i in list:
if i[2] == 0:
pygame.draw.rect(dis, green, [i[0], i[1], self.block, self.block])
if i[2] == 1:
pygame.draw.rect(dis, purple, [i[0], i[1], self.block, self.block])
if i[2] == 2:
pygame.draw.rect(dis, blue, [i[0], i[1], self.block, self.block])
if i[2] == 3:
pygame.draw.rect(dis, orange, [i[0], i[1], self.block, self.block])
def gameLoop():
list = []
num = [0, 0]
count = 0
s = [25]
x1_change = 0
y1_change = 0
game_over = False
game_close = False
snake = Snake(80, 80)
food = Food(snake.block, snake, list)
list.append(food.Head)
bo0 = True
bo1 = True
while not game_over:
while game_close == True:
dis.fill(white)
message("失败", red, lost_style, [dis_width / 2 - 50, dis_height / 3 - 25])
message("接下来请按C键重新开始或者Q键退出游戏。", red, font_style,
[dis_width / 2 - 9 * 25, dis_height / 3 + 25 + 1 * 25])
if eat == 0:
message("死因:创墙!", red, font_style, [280, dis_height / 3 - 50])
if eat == 1:
message("死因:自己咬自己!", red, font_style, [250, dis_height / 3 - 50])
if eat == 2:
message("死因:太慢!", red, font_style, [280, dis_height / 3 - 50])
Your_score(snake.length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
if event.key == pygame.K_c:
gameLoop()
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 移动(如果w,a,s,d之类的chang就对应相应的值,移动长度为snake.block,即为蛇的大小)
if pygame.event.peek():
events = pygame.event.get()
for event in events:
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
bo = False
if len(list) < 5:
n = int(pygame.time.get_ticks() / 1000)
if len(num) == 1:
num[1] = n
bo1 = False
else:
if bo1 == True:
num[1] = n
bo1 = False
bo0 = True
elif bo0 == True:
num[0] = n
bo0 = False
bo1 = True
if num[1] - num[0] >= 1:
bo = True
if bo:
food.__init__(snake.block, snake, list)
food.appear(list)
list.append(food.Head)
if snake.x1 > dis_width - 70 or snake.x1 < 6
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏 python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python制作的贪吃蛇小游戏python
资源推荐
资源详情
资源评论
收起资源包目录
贪吃蛇小游戏程序+原码.zip (2个子文件)
testgame.py 10KB
贪吃蛇小游戏.exe 16.85MB
共 2 条
- 1
资源评论
兜兜有糖自己吃
- 粉丝: 201
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功