Commit cb0ae6a3 by BellCodeEditor

auto save

parent d5739bd2
import pygame import pygame
import random
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
...@@ -15,9 +16,14 @@ food = pygame.image.load('apple.png') # 食物 苹果 ...@@ -15,9 +16,14 @@ food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体 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') # 头 朝下
font =pygame.font.Font("neuropol.ttf",18) #字体
fenshu=0
x, y = 240, 120 x, y = 240, 120
ax, ay = 360, 300
position = [(180, 90), (180, 120), (210, 120), (x, y)] position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right" setheading = "right"
...@@ -42,6 +48,8 @@ while True: ...@@ -42,6 +48,8 @@ while True:
setheading = 'down' setheading = 'down'
snake_head = down snake_head = down
# 设置贪吃蛇的头部坐标 # 设置贪吃蛇的头部坐标
if setheading == "right": if setheading == "right":
x += 30 x += 30
...@@ -51,7 +59,27 @@ while True: ...@@ -51,7 +59,27 @@ while True:
y -= 30 y -= 30
else: else:
y += 30 y += 30
#撞墙退出游戏
if x<0 or y<0 or x>630 or y>450:
exit()
#撞到身体退出游戏
if (x, y) in position:
exit()
position.append((x, y)) position.append((x, y))
if x==ax and y==ay:
#设置苹果的坐标
ax=random.randint(0,21)*30
ay=random.randint(0,15)*30
#苹果不随机到身体上重新随机坐标
while (ax,ay) in position:
ax=random.randint(0,21)*30
ay=random.randint(0,15)*30
fenshu=fenshu+1
else:
position.pop(0) position.pop(0)
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
...@@ -61,8 +89,15 @@ while True: ...@@ -61,8 +89,15 @@ while True:
for i in range(len(position)-1): for i in range(len(position)-1):
screen.blit(body, position[i]) screen.blit(body, position[i])
#写入内容
text="fenshu "+str(fenshu)
info=font.render(text,True,(255,0,0))
#分数渲染到游戏里
screen.blit(info, (540, 10))
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (ax, ay))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) 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