Commit fa47bbf3 by BellCodeEditor

save project

parent 2c8cb43d
Showing with 8 additions and 9 deletions
...@@ -3,11 +3,9 @@ import random ...@@ -3,11 +3,9 @@ import random
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() 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时钟,控制游戏速度(帧数)
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右 right = pygame.image.load('right.png') # 头 朝右
...@@ -16,15 +14,12 @@ body = pygame.image.load('body.png') # 身体 ...@@ -16,15 +14,12 @@ 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') # 头 朝下
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)]
setheading = "right" setheading = "right"
snake_head = right snake_head = right
apple_x=240 apple_x=240
apple_y=120 apple_y=120
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -53,11 +48,15 @@ while True: ...@@ -53,11 +48,15 @@ while True:
y -= 30 y -= 30
else: else:
y += 30 y += 30
position.append((x, y))
position.pop(0)
if x==apple_x and y==apple_y: if x==apple_x and y==apple_y:
apple_x=random.randint(0,630) apple_x=random.randint(1,22)
apple_y=random.randint(0,450) apple_y=random.randint(1,16)
apple_x=apple_x*30-30
apple_y=apple_y*30-30
else:
position.pop(0)
position.append((x, y))
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去 # 将贪吃蛇的头画上去
......
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