核心内容
1.创建食物、蛇身
2.刷新展示屏幕
3.检测pygame.event
4.改变方向的实现逻辑
5.检测碰撞以及超出范围
6.死亡重置
Code
import os
import random
import pygame
# 屏幕大小
SCREEN_X = 600
SCREEN_Y = 600
def main():
# 初始化pygame
pygame.init()
screen_size = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
# 设置窗口名
pygame.display.set_caption('贪吃蛇---by斌')
# 创建一个clock对象来跟踪时间
clock = pygame.time.Clock()
# 初始化得分
scores = 0
# 初始化存活状态
isdead = False
# 创建snake实例
snake = Snake()
# 创建food实例
food = Food()
while True:
for event in pygame.event.get():
# 检测退出
if event.type == pygame.QUIT:
os._exit(0)
if event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死亡后空格重新开始
if event.key == pygame.K_SPACE and isdead:
return main()
# 死亡后Esc退出
elif event.key == pygame.K_ESCAPE and isdead:
os._exit(0)
# 设置屏幕背景颜色
screen.fill((255, 255, 255))
# 未死亡则向前移动
if not isdead:
snake.move()
# 刷新蛇身
for rect in snake.body:
pygame.draw.rect(screen, (20, 100, 30), rect, 0)
isdead = snake.isdead()
# 死亡后展示文本
if isdead:
scores_num = []
# 寻找最高得分
with open('scores.txt', 'r') as file:
file.seek(0, 0)
scores_str = file.readlines()
for x in scores_str:
scores_num.append(int(x))
best_scores = max(scores_num)
show_text(screen, (125, 200), 'YOU DEAD', (227, 29, 18), False, 100)
show_text(screen, (230, 260), "Best Score:{}".format(best_scores), (22, 220, 22), False, 30)
show_text(screen, (185, 290), "press 'Spcae' to try again", (0, 0, 22), False, 30)
show_text(screen, (230, 310), "or 'Esc' to exit", (0, 0, 22), False, 30)
# 检测吃到食物
if food.rect == snake.body[0]:
# 吃到食物后加1分
scores += 1
# 储存历史得分
with open('scores.txt', 'a+') as file:
file.write(str(scores) + '\n')
file.flush()
file.close()
# 吃到食物后移除原食物且蛇身增加
food.remove()
snake.addnode()
# 重新生成食物
food.set()
# 刷新食物
pygame.draw.rect(screen, (136, 0, 24), food.rect)
#展示积分
show_text(screen, (50, 500), "Scores:" + str(scores), (233, 233, 233))
# 按时刷新
pygame.display.update()
clock.tick(10)
class Snake(object):
def __init__(self):
# 规定初始方向为右
self.direction = pygame.K_RIGHT
self.body = []
for x in range(5):
self.addnode()
def addnode(self):
# 初始坐标
left, top = (0, 0)
if self.body:
left, top = (self.body[0].left, self.body[0].top)
# 创建蛇身并规定大小
node = pygame.Rect(left, top, 25, 25)
# 检测按键
if self.direction == pygame.K_LEFT:
node.left -= 25
elif self.direction == pygame.K_RIGHT:
node.left += 25
elif self.direction == pygame.K_UP:
node.top -= 25
elif self.direction == pygame.K_DOWN:
node.top += 25
self.body.insert(0, node)
def delnode(self):
self.body.pop()
def isdead(self): # 检测死亡
# 检测超出屏幕
if self.body[0].x not in range(SCREEN_X):
return True
if self.body[0].y not in range(SCREEN_Y):
return True
# 检测身体碰撞
if self.body[0] in self.body[1:]:
return True
return False
def move(self): # 向前移动
self.addnode()
self.delnode()
def changedirection(self, curkey): #改变方向
lr = [pygame.K_LEFT, pygame.K_RIGHT]
ud = [pygame.K_UP, pygame.K_DOWN]
if curkey in (lr + ud):
if (curkey in lr) and (self.direction in lr):
return
if (curkey in ud) and (self.direction in ud):
return
self.direction = curkey
class Food: #食物
def __init__(self):
# 创建食物并规定大小
self.rect = pygame.Rect(-25, 0, 25, 25)
def remove(self):
# 移除食物
self.rect.x = -25
def set(self):
# 移除食物后在随机位置重新生成
if self.rect.x == -25:
allpos = []
for pos in range(25, SCREEN_X - 25, 25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)
def show_text(screen, pos, text, color, font_bold=False,
font_size=50, font_italic=False): # 设置展示文本
cur_font = pygame.font.SysFont('宋体', font_size)
cur_font.set_bold(font_bold)
cur_font.set_italic(font_italic)
text_fmt = cur_font.render(text, True, color)
screen.blit(text_fmt, pos)
if __name__ == "__main__":
main()
预览效果
请给我打钱,谢谢!
- 本文链接:http://ayitido.github.io/2022/05/11/pygame_snake/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。