Commit 92b5b8d7 by BellCodeEditor

save project

parent 9276b9d7
Showing with 31 additions and 7 deletions
import pygame import pygame
from pygame import locals
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
...@@ -10,30 +9,54 @@ screen = pygame.display.set_mode((660, 480)) ...@@ -10,30 +9,54 @@ 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')
down = pygame.image.load('down.png')
up = pygame.image.load('up.png')
left = pygame.image.load('left.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=240 x=240
y=120 y=120
f=pygame.time.Clock() f=pygame.time.Clock()
eva="right"
ave=right
l=[(210,120),(180,120),(180,90),(x,y)] l=[(210,120),(180,120),(180,90),(x,y)]
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == pygame.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
# 将背景图画上去 if event.type==pygame.KEYDOWN:
screen.blit(background, (0, 0)) if event.type == pygame.K_RIGHT and eva!="left":
eva="right"
ave=right
if event.type == pygame.K_LEFT and eva!="right":
eva="left"
ave=left
if event.type == pygame.K_UP and eva!="down":
eva="up"
ave=up
if event.type == pygame.K_DOWN and eva!="up":
eva="down"
ave=down
# 将贪吃蛇画上去 # 将贪吃蛇画上去
if eva=="left":
x=x-30
elif eva=="right":
x=x+30
elif eva=="up":
y-=30
else:
y+=30
l.append((x,y)) l.append((x,y))
l.pop(0) l.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(right,l[-1]) screen.blit(right,l[-1])
x+=30
y+=30
for i in range(len(l)-1): for i in range(len(l)-1):
screen.blit(body,l[i]) screen.blit(body,l[i])
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (360, 300))
f.tick(5) f.tick(4)
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
\ 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