自己写的贪吃蛇程序+_+
有两个bug,一个是有时快速的按两个方向键,会闪退。还一个是有时贪吃蛇会走出屏幕外,不会传到另一边。实在不知道错在哪了
import pygame, sys , random
pygame.init() #pygame模块初始化
win = pygame.display.set_mode((400,600))
pygame.display.set_caption("pygame事件处理")
fclock = pygame.time.Clock()
RED = pygame.Color("red")
BLACK=0,0,0
fps = 4
Stime = pygame.time.Clock()
napp = 3
applex = #随机数
appley =
def Apple():
applexy = ,random.sample(appley,1 ),20 ,20]#生成苹果的坐标
return applexy
class Snake():
snakelong =
Snakebody =[, , ]
i2 = 3
def __init__(self,direction = "up"):#up down left right 上 下 左 右
self.direction = direction
def generateSnakeboy(self):
for i in self.snakelong:#自动创建蛇身列表
if i == 1 or i == 2 or i == 3:
pass
elif self.i2 > i:
pass
elif self.i2 < i:
self.i2 += 1
if self.i2 == i:#判断增加的体块的方向,真tm恐怖-_-
if self.Snakebody[-2] == self.Snakebody[-1] and self.Snakebody[-2] < self.Snakebody[-1]:
self.Snakebody.append(,self.Snakebody[-1]+25,20,20])#当尾巴在下
elif self.Snakebody[-2] == self.Snakebody[-1] and self.Snakebody[-2] > self.Snakebody[-1]:
self.Snakebody.append(, self.Snakebody[- 1] - 25, 20, 20])#尾当上
elif self.Snakebody[- 2] < self.Snakebody[- 1] and self.Snakebody[- 2] == self.Snakebody[-1]:
self.Snakebody.append(-25, self.Snakebody[- 1] , 20, 20])#尾当左
elif self.Snakebody[- 2] > self.Snakebody[- 1] and self.Snakebody[- 2] == self.Snakebody[-1]:
self.Snakebody.append( + 25, self.Snakebody[- 1] , 20, 20])
def snackanim(self):#实现蛇爬行
i1=len(self.Snakebody)-1
self.Snakebody[- 1] = self.Snakebody[- 2]
pygame.time.delay(50)
for z in self.Snakebody:
i1 -= 1
self.Snakebody = self.Snakebody
if self.direction == "up":
self.Snakebody = , self.Snakebody - 25, 20, 20] #如果蛇头碰壁,则穿越到对面墙壁上
if self.Snakebody < 0:
self.Snakebody = 575
elif self.direction == "down":
self.Snakebody = , self.Snakebody + 25, 20, 20]
if self.Snakebody >600:
self.Snakebody = 0
elif self.direction == "left":
self.Snakebody = -25, self.Snakebody , 20, 20]
if self.Snakebody < 0:
self.Snakebody = 375
elif self.direction == "right":
self.Snakebody = + 25, self.Snakebody , 20, 20]
if self.Snakebody > 400:
self.Snakebody = 0
pygame.time.delay(50)
return self.Snakebody
apple = Apple()
f = Snake()
#只能用这种方法来增加体块
while True:#游戏主程序死循环
for event in pygame.event.get():#pygame事件判断
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_w:
if f.direction == "down":
pass
else:
f.direction = "up"
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
if f.direction == "up":
pass
else:
f.direction = "down"
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
if f.direction == "right":
pass
else:
f.direction = "left"
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
if f.direction == "left":
pass
else:
f.direction = "right"
win.fill(BLACK)
if apple == f.Snakebody:
napp += 1
f.snakelong.append(napp)
f.generateSnakeboy()
apple = Apple()
else:
app = pygame.draw.rect(win, RED, apple)
for sbody in f.snackanim():# 绘制蛇蛇身
newrect = pygame.draw.rect(win, RED, sbody)
for j in f.Snakebody:
if f.Snakebody == j :
sys.exit()
pygame.display.update()#刷新屏
Stime.tick(fps) 支持一下!{:10_257:}
页:
[1]