Commit 00b0c339 by BellCodeEditor

save project

parent e6b0378c
Showing with 20 additions and 14 deletions
import pygame
from pygame import locals
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
......@@ -24,6 +24,7 @@ staion=[(180,90),(180,120),(210,120),(x,y)]
setheading = "right"
snack_head = right
apple_x,apple_y = 360,300
while True:
for event in pygame.event.get():
......@@ -31,27 +32,27 @@ while True:
# 接收到退出事件后退出程序
exit()
elif event.type == locals.KEYDOWN:
if event.type == locals.K_RIGHT and setheading !="left":
if event.key == locals.K_RIGHT and setheading !="left":
setheading = "right"
snack_head = right
if event.type == locals.K_LEFT and setheading !="right":
setheading = "right"
snack_head = right
if event.key == locals.K_LEFT and setheading !="right":
setheading = "left"
snack_head = left
if event.type == locals.K_UP and setheading !="down":
setheading = "right"
snack_head = right
if event.key == locals.K_UP and setheading !="down":
setheading = "up"
snack_head = up
if event.type == locals.K_DOWN and setheading !="up":
setheading = "right"
snack_head = right
if event.key == locals.K_DOWN and setheading !="up":
setheading = "down"
snack_head = down
if setheading == "right":
x-=30
x+=30
elif setheading =="left" :
x+=30
x-=30
elif setheading == "up":
y-=30
else:
......@@ -59,6 +60,10 @@ while True:
staion.append((x,y))
staion.pop(0)
if apple_x == x and apple_y == y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
......@@ -67,7 +72,7 @@ while True:
for i in range(len(staion)-1):
screen.blit(body,staion[i])
# 将果实画上去
screen.blit(food, (360, 300))
screen.blit(food, (apple_x,apple_y))
# 刷新画面
pygame.display.update()
FPSCLOCK .tick(2)
\ 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