Commit 54b9bb77 by BellCodeEditor

save project

parent c19981f3
Showing with 16 additions and 10 deletions
...@@ -22,7 +22,6 @@ x, y = 240, 120 ...@@ -22,7 +22,6 @@ x, y = 240, 120
a_x=360 a_x=360
a_y=300 a_y=300
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
...@@ -30,7 +29,7 @@ while True: ...@@ -30,7 +29,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()
if event.type == locals.KEYDOWN: if 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'
...@@ -44,10 +43,14 @@ while True: ...@@ -44,10 +43,14 @@ while True:
if event.key == locals.K_DOWN and setheading != "up": if event.key == locals.K_DOWN and setheading != "up":
setheading = 'down' setheading = 'down'
snake_head = down snake_head = down
#按键控制蛇移动1
if x==a_x and y==a_y: if x==a_x and y==a_y:
a_x=random.randint(0,33)*30 a_x=random.randint(0,32)*30
a_y=random.randint(0,16)*30 a_y=random.randint(0,15)*30
screen.blit(food, (a_x, a_y)) screen.blit(food, (a_x, a_y))
else:
# 苹果被吃后重新出现 # 苹果被吃后重新出现
if setheading == "right": if setheading == "right":
...@@ -58,21 +61,24 @@ while True: ...@@ -58,21 +61,24 @@ while True:
y -= 30 y -= 30
else: else:
y += 30 y += 30
#按键控制蛇移动 #按键控制蛇移动2
position.append((x, y)) position.append((x, y))
position.pop(0) position.pop(0)
# 将蛇头的图画上去
screen.blit(background, (0, 0))
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1]) screen.blit(snake_head, 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, (a_x, a_y)) screen.blit(food, (a_x, a_y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(3)
\ No newline at end of file print("game is over")
\ 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