Commit d3e6a0e0 by BellCodeEditor

save project

parent 05008268
Showing with 30 additions and 2 deletions
...@@ -10,22 +10,43 @@ screen = pygame.display.set_mode((660, 480)) ...@@ -10,22 +10,43 @@ screen = pygame.display.set_mode((660, 480))
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') right = pygame.image.load('right.png')
left = pygame.image.load('left.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
food = pygame.image.load('apple.png') food = pygame.image.load('apple.png')
body = pygame.image.load('body.png') body = pygame.image.load('body.png')
x,y = 240,120 x,y = 240,120
pos = [(180,60),(180,90),(180,120),(210,120),(x,y)] pos = [(180,60),(180,90),(180,120),(210,120),(x,y)]
clock = pygame.time.Clock() clock = pygame.time.Clock()
heading = 'r'
snake_head_img = right
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:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
elif event.type == locals.KEYDOWN:
if event.type == locals.K_RIGHT and heading != 'l':
heading = 'r'
snake_head_img = right
elif event.type == locals.K_LEFT and heading != 'r':
heading = 'l'
snake_head_img = left
elif event.type == locals.K_UP and heading != 'd':
heading = 'u'
snake_head_img = up
elif event.type == locals.K_DOWN and heading != 'u':
heading = 'd'
snake_head_img = down
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
screen.blit(right,pos[-1]) screen.blit(snake_head_img,pos[-1])
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
for i in range(len(pos)-1): for i in range(len(pos)-1):
screen.blit(body,pos[i]) screen.blit(body,pos[i])
...@@ -33,7 +54,14 @@ while True: ...@@ -33,7 +54,14 @@ while True:
screen.blit(food, (360, 300)) screen.blit(food, (360, 300))
# 刷新画面 # 刷新画面
x += 30 if heading == 'r':
x += 30
if heading == 'l':
x -= 30
if heading == 'u':
y -= 30
if heading == 'd':
y += 30
pos.append((x,y)) pos.append((x,y))
pos.pop(0) pos.pop(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