效果演示
主界面选择难度
难度不同 蛇的移动速度不同
进入游戏,蛇默认长度为2

游戏结束
选择yes新开始游戏,选择no 退出游戏

过程记录
如何实现蛇头的移动
- 移动其实就是对现有坐标进行加减运算,然后得到一个新坐标,再将新坐标画出来
- 需要限制帧率,用帧率控制蛇的移动速度
如何实现蛇身的移动
- 观察蛇的移动,我们发现,每次都是蛇尾少了一个格子,而蛇身另一端多了头部的移动格子
- 所以我们构造一个list,每次移动,就将头部格子加入到list头部,然后使用pop去掉List尾部一个格子,如果吃到食物时,就不进行pop,这样就实现了蛇的移动
关于食物生成
- 食物生成要注意不能生成到与蛇重合的格子里,如果重合了,就重新生成直到不重合为止
关于运动的边界
- 这需要判断蛇头的移动即可,这个蛇身是跟着蛇头移动的,蛇头不出边界就不会出边界
pygame模块
- 花半天时间过一下就行
- 不需要记忆函数具体,这需要看有哪些功能有个印象
代码
import pygame
import sys
import random
width=640#定义屏幕的宽
hight=480#定义屏幕的高
width_num=64#定义宽度总格子数量
hight_num=48#定义高度总格子数量
pixel_width=width/width_num
pixel_hight=hight/hight_num
class point(object):
def __init__(self,x,y):
self.x=x
self.y=y
def copy(self):
return point(x=self.x,y=self.y)
#画像素点的函数
def draw_pixel(point,color):
Rect=[point.x,point.y,pixel_width,pixel_hight]
pygame.draw.rect(screen, color, Rect, width=0)
#控制贪吃蛇移动,并判断蛇头是否碰到边界
def snake_move(direction,head,food,snake_body):
if head.x==food.x and head.y == food.y:
return 'eat_food'
#蛇头是否出边界判断
if head.x<0 or head.x>width or head.y<0 or head.y>hight:
return 'game_over'
if direction == 'snake_left':
head.x-=pixel_width
elif direction == 'snake_right':
head.x+=pixel_width
elif direction == 'snake_up':
head.y-=pixel_hight
elif direction == 'snake_down':
head.y+=pixel_hight
if len(snake_body)>1:
for i in snake_body:
if i.x==head.x and i.y==head.y:
return 'game_over'
return 'snake_move'
#生成食物函数
def food_funtion(head,snake_body):
flag=True
#生成食物坐标,与头部重合的话,就一直重新生成
while flag==True:
food=point(random.randint(0,width_num-1)*pixel_width,random.randint(0,hight_num-1)*pixel_hight)
if food.x==head.x and food.y==head.y:
flag=True
else:
flag=False
#检查是否与身体重合,只要有一个重合就继续循环
for i in snake_body:
if i.x==food.x and i.y==food.y:
flag=True
return food
#贪吃蛇主体函数
def snake_code(level_value):
head=point(width_num/2*pixel_width,hight_num/2*pixel_hight)#初始化蛇头
food=point(pixel_width*3,pixel_hight*4)#初始化食物位置
snake_body=[point(head.x+pixel_width,head.y)]#初始化蛇身
direction='snake_left'
judge_state='snake_move'
while True:
# 处理帧频,即降低程序的运行速度
clock.tick(level_value)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:#键盘有按下?
if event.key ==pygame.K_LEFT:#按下左方向键
if direction=='snake_up' or direction=='snake_down':
direction='snake_left'
elif event.key == pygame.K_RIGHT:#按下右方向键
if direction=='snake_up' or direction=='snake_down':
direction='snake_right'
elif event.key == pygame.K_UP:#按下向上键
if direction=='snake_left'or direction=='snake_right':
direction='snake_up'
elif event.key == pygame.K_DOWN:#按下向下键
if direction=='snake_left'or direction=='snake_right':
direction='snake_down'
if judge_state=='eat_food':
#如果吃到食物则重新生成食物
food=food_funtion(head,snake_body)
snake_body.insert(0,head.copy())#将新移动点插入到头部
judge_state=snake_move(direction,head,food,snake_body)#移动蛇头
elif judge_state=='snake_move':
snake_body.insert(0,head.copy())
snake_body.pop()
judge_state=snake_move(direction,head,food,snake_body)#移动蛇头
elif judge_state=='game_over':
return 'windows_gameover'
# 背景画图
pygame.draw.rect(screen, (255, 255, 155), (0, 0, width, hight))
draw_pixel(head,(90,90,90)) #画头部
draw_pixel(food,(0,0,0)) #画食物
for i in snake_body:#画身体
draw_pixel(i,(100,100,100))
pygame.display.update() #刷新画面
# 绘制game over界面
def draw_windows_gameover():
pygame.draw.rect(screen, (255, 255, 200), (0, 0, width, hight))
my_font = pygame.font.SysFont("arial", 70)
title_1 = my_font.render('game over', True, (0, 0, 0))
screen.blit(title_1,(150, 50))
my_font_2 = pygame.font.SysFont("arial", 50)
title_2 = my_font_2.render('Do you want to start over', True, (0, 0, 0))
title_3 = my_font_2.render('yes', True, (0, 0, 0),(200,200,200))
title_4 = my_font_2.render(' no ', True, (0, 0, 0),(200,200,200))
screen.blit(title_2,(100, 200))
screen.blit(title_3,(200, 300))
screen.blit(title_4,(400, 300))
# 绘制进入界面的画面
def draw_windows_begin():
pygame.draw.rect(screen, (255, 255, 200), (0, 0, width, hight))
my_font = pygame.font.SysFont("arial", 30)
title_1 = my_font.render('please choose game difficulty', True, (0, 0, 0))
screen.blit(title_1,(150, 50))
my_font_2 = pygame.font.SysFont("arial", 50)
title_2 = my_font_2.render('easy', True, (0, 0, 0),(200,200,200))
title_3 = my_font_2.render('medium', True, (0, 0, 0),(200,200,200))
title_4 = my_font_2.render('difficult', True, (0, 0, 0),(200,200,200))
screen.blit(title_2,(50, 110))
screen.blit(title_3,(200, 110))
screen.blit(title_4,(400, 110))
if __name__ =='__main__':
pygame.init()
screen=pygame.display.set_mode((width,hight),0,32)#设置窗口
pygame.display.set_caption("贪吃蛇游戏")#设置窗口标题
clock = pygame.time.Clock()
system_state='windows_begin'
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: # 鼠标按下
x, y = pygame.mouse.get_pos()# 获取当前鼠标单击位置
if pygame.mouse.get_pressed()==(True, False, False):
if system_state=='windows_begin':
if x>50 and x<140 and y>110 and y<170:#设定游戏难度为简单
system_state=snake_code(6)
elif x>200 and x<350 and y>110 and y<170:#设定游戏难度为中等
system_state=snake_code(13)
elif x>400 and x<525 and y>110 and y<170:#设置游戏难度为困难
system_state=snake_code(20)
# 游戏结束界面的操作
elif system_state=='windows_gameover':#重新开始游戏
if x>200 and x<270 and y>300 and y<360:
system_state='windows_begin'
elif x>400 and x<470 and y>300 and y<360:#退出游戏
pygame.quit()
sys.exit()
if system_state=='windows_gameover':
draw_windows_gameover()
elif system_state=='windows_begin':
draw_windows_begin()
pygame.display.update() #刷新画面
1567

被折叠的 条评论
为什么被折叠?



