Commit d233550c by BellCodeEditor

save project

parent 5dd9b96e
Showing with 47 additions and 15 deletions
...@@ -4,24 +4,56 @@ from pygame import locals ...@@ -4,24 +4,56 @@ from pygame import locals
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
FPSCLOCK=pygame.time.Clock() FPSCLOCK=pygame.time.Clock()
a=pygame.image.load("bg.png") bg=pygame.image.load("bg.png")
b=pygame.image.load("right.png") R=pygame.image.load("right.png")
c=pygame.image.load("apple.png") apple=pygame.image.load("apple.png")
d=pygame.image.load("body.png") body=pygame.image.load("body.png")
e=pygame.time.Clock() U=pygame.image.load("up.png")
D=pygame.image.load("down.png")
L=pygame.image.load("left.png")
setheading='right'
snake_head=R
x,y=240,120
position=[(180,90),(180,120),(210,120),(x,y)]
# 创建一个窗口 # 创建一个窗口
screen=pygame.display.set_mode((660,480)) screen=pygame.display.set_mode((660,480))
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()
if event.type== locals.KEYDOWN:
if event.key==locals.K_RIGHT and setheading!='left':
screen.blit(a,(0,0)) setheading='right'
screen.blit(b,(240,300)) snake_head=R
screen.blit(c,(300,360)) if event.key==locals.K_LEFT and setheading!='right':
screen.blit(d,(210,300)) setheading='left'
screen.blit(d,(180,300)) snake_head=L
screen.blit(d,(180,270)) if event.key==locals.K_DOWN and setheading!='up':
setheading='down'
snake_head=D
if event.key==locals.K_UP and setheading!='down':
setheading='up'
snake_head=U
if setheading=='right':
x+=30
if setheading=='left':
x-=30
if setheading=='up':
y-=30
if setheading=='down':
y+=30
position.append((x,y))
position.pop(0)
screen.blit(bg,(0,0))
screen.blit(snake_head,position[-1])
for i in range (len(position)-1):
screen.blit(body,position[i])
screen.blit(apple,(300,360))
# screen.blit(body,(210,120))
# screen.blit(body,(180,120))
# screen.blit(body,(180,90))
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) FPSCLOCK.tick(3)
\ No newline at end of file \ 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