Commit 46ec1997 by BellCodeEditor

auto save

parent 62937466
Showing with 34 additions and 2 deletions
import pygame
from pygame import locals
xy = []
xy = [(180,90),(210,90),(240,90),(270,90)]
FPS = pygame.time.Clock()
fx = 'right'
x = 0
y = 0
# 初始化pygame,为使用硬件做准备
pygame.init()
......@@ -12,6 +15,9 @@ screen = pygame.display.set_mode((660, 480))
# 背景
background = pygame.image.load('bg.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')
body = pygame.image.load('body.png')
......@@ -21,10 +27,36 @@ while True:
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == K_RIGHT and fx != 'left':
fx = 'right'
elif event.key == K_LEFT and fx != 'right':
fx = 'left'
elif event.key == K_UP and fx != 'down':
fx = 'up'
else:
if fx != 'up':
fx = 'down'
xy.append((x,y))
xy.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, xy[3])
if fx == 'right':
screen.blit(right, xy[3])
x += 30
else:
if fx == 'left':
screen.blit(left,xy[3])
x -= 30
else:
if fx == 'up':
screen.blit(up,xy[3])
y -= 30
else:
screen.blit(down,xy[3])
y += 30
# 将贪吃蛇的身体画上去
screen.blit(body, xy[2])
screen.blit(body, xy[1])
......
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