Commit 74bb31f1 by BellCodeEditor

save project

parent 5fe01f68
Showing with 17 additions and 7 deletions
import pygame#导入pygame模块
import random#导入random模块
from pygame import locals#从pygame模块导入locals子模块
# 初始化pygame,为使用pygame做准备
pygame.init()
pygame.init()# 初始化pygame,为使用pygame做准备
background=pygame.image.load('bg.png')#载入背景图
right=pygame.image.load('right.png')#贪吃蛇朝右的蛇头载入
left=pygame.image.load('left.png')#贪吃蛇朝左的蛇头载入
......@@ -10,14 +9,15 @@ up=pygame.image.load('up.png')#贪吃蛇朝上的蛇头载入
down=pygame.image.load('down.png')#贪吃蛇朝下的蛇头载入
food=pygame.image.load('apple.png')#将食物苹果载入
body=pygame.image.load('body.png')#将身体载入进去
my_font=pygame.font.Font('neuropol.ttf',18)#将字体载入进去
x,y=240,120#将蛇头的坐标赋值给一个变量
apple_x=360
apple_y=300
position=[(180,90),(180,120),(210,120),(x,y)]#建立一个列表
setheading = "right" #设置贪吃蛇的朝向
snake_head=right
# 创建一个窗口
screen=pygame.display.set_mode(size=(660,480))
score=0
screen=pygame.display.set_mode(size=(660,480))# 创建一个窗口
FPSCLOCK=pygame.time.Clock()
while True:
for event in pygame.event.get():
......@@ -46,13 +46,22 @@ while True:
y+=30
position.append((x,y))#将新坐标添加到新列表里
if x==apple_x and y==apple_y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
position.pop(0)#删除掉第一个坐标
num_1=random.randint(0,22)
num_2=random.randint(0,16)
apple_x=num_1*30-30
apple_y=num_2*30-30
score+=10
else:
position.pop(0)#删除掉第一个坐标
if x<0 or x>630 or y<0 or y>450:
exit()
screen.blit(background,(0,0))#将背景图画上去
screen.blit(snake_head,position[-1])#将蛇头画上去
for i in range(len(position)-1):
screen.blit(body,position[i])
screen.blit(food,(apple_x,apple_y))#将食物画上去
info="Score"+str(score)
text=my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
pygame.display.update()#刷新画面
FPSCLOCK.tick(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