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 = [375, 350, 150, 125, 100, 75, 50, 25, 0] #随机数
appley = [225, 200, 175, 150, 125, 100, 75, 50, 25, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575]
def Apple():
applexy = [random.sample(applex,1)[0],random.sample(appley,1 )[0],20 ,20]#生成苹果的坐标
return applexy
class Snake():
snakelong = [1,2,3]
Snakebody =[[150, 300, 20, 20], [150, 325, 20, 20], [150,350,20,20]]
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][0] == self.Snakebody[-1][0] and self.Snakebody[-2][1] < self.Snakebody[-1][1]:
self.Snakebody.append([self.Snakebody[-1][0],self.Snakebody[-1][1]+25,20,20])#当尾巴在下
elif self.Snakebody[-2][0] == self.Snakebody[-1][0] and self.Snakebody[-2][1] > self.Snakebody[-1][1]:
self.Snakebody.append([self.Snakebody[- 1][0], self.Snakebody[- 1][1] - 25, 20, 20])#尾当上
elif self.Snakebody[- 2][0] < self.Snakebody[- 1][0] and self.Snakebody[- 2][1] == self.Snakebody[-1][1]:
self.Snakebody.append([self.Snakebody[- 1][0]-25, self.Snakebody[- 1][1] , 20, 20])#尾当左
elif self.Snakebody[- 2][0] > self.Snakebody[- 1][0] and self.Snakebody[- 2][1] == self.Snakebody[-1][1]:
self.Snakebody.append([self.Snakebody[- 1][0] + 25, self.Snakebody[- 1][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[1:-1]:
i1 -= 1
self.Snakebody[i1] = self.Snakebody[i1 - 1]
if self.direction == "up":
self.Snakebody[0] = [self.Snakebody[0][0], self.Snakebody[0][1] - 25, 20, 20] #如果蛇头碰壁,则穿越到对面墙壁上
if self.Snakebody[0][1] < 0:
self.Snakebody[0][1] = 575
elif self.direction == "down":
self.Snakebody[0] = [self.Snakebody[0][0], self.Snakebody[0][1] + 25, 20, 20]
if self.Snakebody[0][1] >600:
self.Snakebody[0][1] = 0
elif self.direction == "left":
self.Snakebody[0] = [self.Snakebody[0][0] -25, self.Snakebody[0][1] , 20, 20]
if self.Snakebody[0][0] < 0:
self.Snakebody[0][0] = 375
elif self.direction == "right":
self.Snakebody[0] = [self.Snakebody[0][0] + 25, self.Snakebody[0][1] , 20, 20]
if self.Snakebody[0][0] > 400:
self.Snakebody[0][0] = 0
pygame.time.delay(50)
return self.Snakebody
apple = Apple()
f = Snake()
#只能用这种方法来增加体块
while True:#游戏主程序死循环
for event in pygame.event.get([pygame.QUIT,pygame.KEYUP]):#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[0]:
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[1:]:
if f.Snakebody[0] == j :
sys.exit()
pygame.display.update()#刷新屏
Stime.tick(fps)