Commit 297d0384 by BellCodeEditor

save project

parent e273e0b5
apple.png

2.05 KB

bg.png

22.5 KB

body.png

1.4 KB

down.png

2.01 KB

left.png

2.07 KB

File added
right.png

2.05 KB

import pygame
from pygame import locals
import random
# 初始化pygame,为使用pygame做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660,480))
fps=pygame.time.Clock()
#载入图片
bg=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')
apple=pygame.image.load('apple.png')
body=pygame.image.load('body.png')
x=360
y=300
pos=[(300,270),(330,300),(360,300),(x,y)]
heah='right'
a_x=360
a_y=210
while True:
for event in pygame.event.get():
if event.type==locals.QUIT:
exit()
if event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and heah!='left':
heah='right'
if event.key==locals.K_LEFT and heah!='right':
heah='left'
if event.key==locals.K_UP and heah!='down':
heah='up'
if event.key==locals.K_DOWN and heah!='up':
heah='down'
if heah=="right":
x+=30
tou=right
if heah=="left":
x-=30
tou=left
if heah=="down":
y+=30
tou=down
if heah=="up":
y-=30
tou=up
pos.append((x,y))
pos.pop(0)
if x==a_x and y==a_y:
a_x=random.randint(0,22)*30
a_y=random.randint(0,16)*30
screen.blit(bg,(0,0))
screen.blit(apple,(a_x,a_y))
screen.blit(tou,(x,y))
for i in range(len(pos)-1):
screen.blit(body,pos[i])
pygame.display.update()
fps.tick(3)
\ No newline at end of file
up.png

2.05 KB

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