Commit 0fcd8904 by BellCodeEditor

save project

parent 0b913573
Showing with 49 additions and 14 deletions
import pygame import pygame
from pygame import locals from pygame import locals
pygame.init() pygame.init() #初始化
screen = pygame.display.set_mode((800,600)) food = pygame.image.load('apple.png') #图片初始化
background = pygame.image.load('bg.png')
food = pygame.image.load('apple.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')
body = pygame.image.load('body.png')
background = pygame.image.load('bg.png')
screen = pygame.display.set_mode((800,600)) #创建窗口
x = 180
y = 180
setheading = right
position = [(x,y),(150,180),(120,180),(120,150)] #设置位置列表
fps = pygame.time.Clock() #s设置一个计时器用来记录画面刷新的帧数
while True: while True:
for i in pygame.event.get():
if i.type == locals.QUIT: for i in pygame.event.get(): #遍历pygame中的事件,优先处理事件
exit() if i.type == locals.KEYDOWN:
screen.blit(background,(0,0)) if i.key == locals.K_w and setheading!=down:
screen.blit(food,(180,180))
screen.blit(right,(150,180)) setheading = up
pygame.display.update() elif i.key == locals.K_s and setheading!=up:
\ No newline at end of file
setheading = down
elif i.key == locals.K_a and setheading!=right:
setheading = left
elif i.key == locals.K_d and setheading!=left:
setheading = right
if i.type == locals.QUIT: #如果事件编号和locals中推出的编号一样
exit() #终止程序
if setheading == up:
y-=30
if setheading == down:
y+=30
if setheading == right:
x+=30
if setheading == left:
x-=30
screen.blit(background,(0,0)) #画背景
screen.blit(food,(300,300)) #画食物
screen.blit(setheading,position[0])
for i in range(1,len(position)):
screen.blit(body,position[i])
pygame.display.update() #刷新窗口
position.insert(0,(x,y))
position.pop(-1)
fps.tick(3) #每秒刷新3帧
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