Commit f0020769 by BellCodeEditor

auto save

parent 5413bdf9
Showing with 54 additions and 1 deletions
import pygame
import random
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
#加载图片
background=pygame.image.load("bg.png")
apple=pygame.image.load("apple.png")
body=pygame.image.load("body.png")
right=pygame.image.load("right.png")
left=pygame.image.load("left.png")
down=pygame.image.load("down.png")
up=pygame.image.load("up.png")
times=pygame.time.Clock()
x=180
y=120
setheading="right"
snake_head=right
apple_x=240
apple_y=330
position=[(90,90),(120,120),(150,120),(x,y)]
# 创建一个窗口
screen=pygame.display.set_mode((600,800))
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 setheading!="left":
setheading="right"
snake_head=right
if event.key==locals.K_LEFT and setheading!="right":
setheading="left"
snake_head=left
if event.key==locals.K_DOWN and setheading!="up":
setheading="down"
snake_head=down
if event.key==locals.K_UP and setheading!="down":
setheading="up"
snake_head=up
if setheading=="right":
x+=30
elif setheading=="left":
x-=30
elif setheading=="down":
y+=30
else:
y-=30
position.append((x,y))
position.pop(0)
if x==apple_x and y==apple_y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
screen.blit(background,(0,0))
for i in range(len(position)):
screen.blit(body,position[i])
screen.blit(apple,(apple_x,apple_y))
screen.blit(snake_head,position[-1])
times.tick(3)
pygame.display.update()
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