Commit 85d98579 by BellCodeEditor

save project

parent 4c381640
Showing with 15 additions and 3 deletions
...@@ -8,7 +8,11 @@ pygame.init() ...@@ -8,7 +8,11 @@ pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
score=0
b=random.randint(10,50)
c=random.randint(0,200)
d=random.randint(0,200)
e=random.randint(0,200)
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右 right = pygame.image.load('right.png') # 头 朝右
...@@ -17,7 +21,7 @@ body = pygame.image.load('body.png') # 身体 ...@@ -17,7 +21,7 @@ body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左 left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上 up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下 down = pygame.image.load('down.png') # 头 朝下
fen=pygame.font.Font('neuropol.ttf',b)
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
...@@ -27,6 +31,7 @@ snake_head = right ...@@ -27,6 +31,7 @@ snake_head = right
while True: while True:
a=random.randint(0,10) a=random.randint(0,10)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
...@@ -58,9 +63,12 @@ while True: ...@@ -58,9 +63,12 @@ while True:
if x==apple_x and y==apple_y: if x==apple_x and y==apple_y:
apple_x=(random.randint(0,630))//30*30 apple_x=(random.randint(0,630))//30*30
apple_y=(random.randint(0,450))//30*30 apple_y=(random.randint(0,450))//30*30
score+=10
else: else:
position.pop(0) position.pop(0)
# if (apple_x,apple_y) in position[:len[position]-2]:
# apple_x=(random.randint(0,630))//30*30
# apple_y=(random.randint(0,450))//30*30
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
...@@ -71,6 +79,9 @@ while True: ...@@ -71,6 +79,9 @@ while True:
# 将果实画上去 # 将果实画上去
screen.blit(food, (apple_x, apple_y)) screen.blit(food, (apple_x, apple_y))
info='Score:'+str(score)
fen1=fen.render(info,True,(c,d,e))
screen.blit(fen1,(480,0))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(a) FPSCLOCK.tick(a)
\ 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