Commit 916ac598 by BellCodeEditor

save project

parent d354f024
Showing with 15 additions and 0 deletions
...@@ -15,12 +15,16 @@ up = pygame.image.load('up.png') ...@@ -15,12 +15,16 @@ up = pygame.image.load('up.png')
down = pygame.image.load('down.png') down = pygame.image.load('down.png')
food = pygame.image.load('apple.png') food = pygame.image.load('apple.png')
body = pygame.image.load('body.png') body = pygame.image.load('body.png')
my_info=pygame.font.Font('neuropol.ttf',20)
x,y=240,120 #将头部的坐标设为变量 x,y=240,120 #将头部的坐标设为变量
position=[(180,90),(180,120),(210,120),(x,y)] #将蛇身各部位坐标列表 position=[(180,90),(180,120),(210,120),(x,y)] #将蛇身各部位坐标列表
apple_x=360 apple_x=360
apple_y=300 apple_y=300
score=0
setheading="right" #头部初始变量 setheading="right" #头部初始变量
snake_head=right #头部初始朝向 snake_head=right #头部初始朝向
...@@ -28,6 +32,7 @@ while True: ...@@ -28,6 +32,7 @@ while True:
for event in pygame.event.get(): #遍历事件 for event in pygame.event.get(): #遍历事件
if event.type == locals.QUIT: if event.type == locals.QUIT:
exit() #接收到退出事件后退出程序 exit() #接收到退出事件后退出程序
elif event.type==locals.KEYDOWN: elif event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and setheading!='left': if event.key==locals.K_RIGHT and setheading!='left':
setheading='right' setheading='right'
...@@ -49,18 +54,27 @@ while True: ...@@ -49,18 +54,27 @@ while True:
y-=30 y-=30
else: else:
y+=30 #移动 y+=30 #移动
position.append((x,y)) position.append((x,y))
if x==apple_x and y==apple_y: if x==apple_x and y==apple_y:
num1=random.randint(0,22) num1=random.randint(0,22)
num2=random.randint(0,16) num2=random.randint(0,16)
apple_x=num1*30-30 apple_x=num1*30-30
apple_y=num2*30-30 apple_y=num2*30-30
score+=10
else: else:
position.pop(0) position.pop(0)
screen.blit(background, (0, 0)) #将背景图画上去 screen.blit(background, (0, 0)) #将背景图画上去
screen.blit(right,position[-1]) #将贪吃蛇画上去 screen.blit(right,position[-1]) #将贪吃蛇画上去
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body,position[i]) #将贪吃蛇画上去 screen.blit(body,position[i]) #将贪吃蛇画上去
screen.blit(food, (apple_x, apple_y)) #将食物画上去 screen.blit(food, (apple_x, apple_y)) #将食物画上去
info="Score:"+str(score)
text=my_info.render(info,True,(0,0,0))
screen.blit(text,(540,10))
pygame.display.update() #刷新画面 pygame.display.update() #刷新画面
FPSCLOCK.tick(3) #游戏画面每秒不超过3帧 FPSCLOCK.tick(3) #游戏画面每秒不超过3帧
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment