Commit 15053023 by BellCodeEditor

save project

parent c6188a1e
Showing with 64 additions and 4 deletions
import pygame import pygame,random
from pygame import locals
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
?? screen = pygame.display.set_mode((640,480))
\ No newline at end of file #载入图片
bg = pygame.image.load("bg.png")
body = pygame.image.load("body.png")
left = pygame.image.load("left.png")
down = pygame.image.load("down.png")
up = pygame.image.load("up.png")
right = pygame.image.load("right.png")
apple = pygame.image.load("apple.png")
#计时器
Fps = pygame.time.Clock()
x,y=240,120
apple_x,apple_y=330,120
position = [(120,120),(150,120),(180,120),(210,120),(x,y)]
setheading = "right"
head = right
while True:
for event in pygame.event.get(): # 把窗口里面的所有事件遍历出来
if event.type == locals.QUIT: # 判断下窗口事件类型是否等于 locals中的QUIT 就代表退出的意思
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading!="left":
setheading="right"
head = right
if event.key == locals.K_LEFT and setheading!="right":
setheading="left"
head = left
if event.key == locals.K_DOWN and setheading!="up":
setheading="down"
head = down
if event.key == locals.K_UP and setheading!="down":
setheading="up"
head = up
if setheading == "right":
x+=30
elif setheading == "left":
x-=30
elif setheading == "down":
y+=30
else:
y-=30
position.append((x,y))
if apple_x == x and apple_y:
apple_x = random.randint(0,660)
apple_y = random.randint(0,480)
position.pop(0)
#图片的渲染
screen.blit(bg,(0,0))
screen.blit(head,position[-1])#蛇头
# screen.blit(body,(210,120))#蛇身体
# screen.blit(body,(180,120))
# screen.blit(body,(150,120))
# screen.blit(body,(120,120))
for i in range(len(position)-1):
screen.blit(body,position[i])
# 果实
screen.blit(apple,(apple_x,apple_y))
#刷新一下
pygame.display.update()
Fps.tick(10)
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