1 贪吃蛇


分析:
1 .后面的个子往前 跟着前面的
2 . 吃,重合的时候,末尾添加一个

2 初始化框架

背景设置:
#画出背景 使用 draw函数 第一个参数是 屏幕对象 第二个颜色,第三个位置与长宽
pygame.draw.rect(screen,GREEN,(0,0,width,height))

使用到的方法:
pygame.display 显示
pygame.time 时间
pygame.event 事件
pygame.draw 绘制
3 绘制蛇
设置行列

定义方格的一个类

生成蛇头与食物的位置

4 绘制函数
def rect(point,color):
left = point.row*width/p_row
top = point.col * height/p_col
pygame.draw.rect(screen,color,(left,top,p_row,p_col))


5 移动蛇
向左: col-=1
向右: col+=1
向上 :row-=1
向下: row+=1

如果太快 需要修改 fps帧率即可
这里需要对键盘控制蛇头进行编译 ,因此修改:
查看上下左右对应的数值


6 跟随蛇身
使用一个列表储存 这些小方块
list_snake = [
(10,8),
(10,9),
(11,9),
]
1 原来的头插入进去
2 最后一个删除
定义复制的类

定义蛇的列表

先给3个身子 对象

将身子画出来

7 食物随机
food = Point(row=random.randint(0,Row-1),col=random.randint(0,Column-1))

8 吃东西


eat = (head.row == food.row and head.col == food.col)
if eat:
food = Point(row = random.randint(0,Row-1),col = random.randint(0,Col-1))
else:
snake.pop()
9 修改
食物不在蛇头与蛇身子上
def gen_food():
while True:
pos = Point(row = random.randint(0,Row-1),col = random.randint(0,Column-1)) #随机产生一个位置
is_col = False #标记碰撞为假
if head.row == pos.row and head.col = pos.col :
is_col = True #如果头碰上 标记碰上
for s in snake: #循环身子 如果身子碰上 将标记设置碰上为真
if s.row == pos.row and s.col ==pos.col:
is_col = True
break #跳出 for
if not is_col: #如果整体没有碰上 跳 while
break
return pos # 返回新生成的 位置 食物

10 修改 不能从左边直接到右边 上下同样道理

11 不能碰到墙 与自己

全部代码:
import pygame,random
class Point:
row = 0
col = 0
def __init__(self,row,col):
self.row = row
self.col = col
def copy(self):
#复制自己
return Point(row=self.row,col=self.col)
#初始化程序
pygame.init()
size = width , height = 800,600
#设置行列
# Row= 6
# Column = 8
Row= 30
Column = 40 #分成更多的份
WHITE = 255,255,255
BLACK = 0,0,0
GREEN = 0,255,0
BLUE = 0,0,255
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
#每个行与每个列的小单位
p_row = height/Row #行高度
p_col = width/Column #列宽度
#定义蛇的头坐标
head = Point(row = int(Row/2),col=int(Column/2))
#定义蛇蛇身体列表
snake = [
Point(row=head.row,col=head.col+1),
Point(row=head.row, col=head.col + 2),
Point(row=head.row, col=head.col + 3),
]
#定义吃的食物位置 随机产生 randint(a,b)从 a=<n<=b
def gen_food():
while True:
pos = Point(row=random.randint(0,Row-1),col=random.randint(0,Column-1))
#查看坐标是否与蛇碰上了
is_coll = False #默认 没有碰上
if head.row == pos.row and head.col == pos.col:
is_coll = True
#蛇的身子
for s in snake:
if s.row == pos.row and s.col==pos.col:
is_coll = True
break
if not is_coll:
break
return pos
food = gen_food()
#定义绘制的函数
def rect(point,color):
left = point.col*p_col # x 坐标
top = point.row*p_row # y坐标
pygame.draw.rect(screen,color,(left,top,p_col,p_row)) #画出来具体位置
#默认 向左
direct = 'left'
#定义刷新帧数
click = pygame.time.Clock()
fps = 10
running = True
#循环过程
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == 273 or event.key == 119:
#只能是左右方向的时候才可以上
if direct == 'left' or direct == 'right':
direct = 'up'
if event.key ==274 or event.key ==115:
# 只能是左右方向的时候才可以下
if direct == 'left' or direct == 'right':
direct = 'down'
if event.key == 276 or event.key==97:
#只有上下方向的时候才可以左右移动
if direct == 'up' or direct == 'down':
direct = 'left'
if event.key==275 or event.key ==100:
# 只有上下方向的时候才可以左右移动
if direct == 'up' or direct == 'down':
direct = 'right'
#处理身体
#1 将头 插入在头上
snake.insert(0,head.copy())
#吃东西
eat = (head.row==food.row and head.col==food.col)
if not eat: #如果没有吃到的话将最后一个身体方块删除 吃到的话就不删除
# 2 将最后一个删除
snake.pop()
else: #吃到的话就重新产生食物
# food = Point(row=random.randint(0, Row - 1), col=random.randint(0, Column - 1))
food = gen_food() #使用函数生成
#画出背景 使用 draw函数 第一个参数是 屏幕对象 第二个颜色,第三个位置与长宽
pygame.draw.rect(screen,WHITE,(0,0,width,height))
#自动移动
if direct == 'left':
head.col -=1
elif direct == 'right':
head.col+=1
elif direct == 'up':
head.row-=1
elif direct =='down':
head.row +=1
#检测是不是死掉
dead = False
# 碰到墙
if head.col<0 or head.row <0 or head.col>=Column or head.row>=Row:
dead = True
#碰到自己
for s in snake:
if head.col == s.col and head.row == s.row:
dead = True
break
if dead:
running = False
#绘制身体
for s in snake:
rect(s,GREEN)
#绘制蛇头
rect(head,BLUE)
#绘制食物
rect(food,BLACK)
#游戏渲染
pygame.display.update()
#设置帧率
click.tick(fps)

网友评论