Commit b9f4832d by BellCodeEditor

auto save

parent 4689ccfd
Showing with 46 additions and 6 deletions
import pygame
from pygame import locals
import random
# 初始化pygame,为使用pygame做准备
pygame.init()
......@@ -10,22 +10,62 @@ screen = pygame.display.set_mode((660,480))
bg = pygame.image.load('bg.png')
apple = pygame.image.load('apple.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')
applex,appley =(210,180)
x,y=(120,30)
fps=pygame.time.Clock()
position = [(30,30),(60,30),(90,30),(x,y)]
heading ='right'
head = right
score = 0
font = pygame.font.Font('neuropol.ttf',15)
while True:
for event in pygame.event.get():
if event.type==locals.QUIT:
exit()
x+=30
if event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and heading != 'left':
heading='right'
head =right
if event.key==locals.K_LEFT and heading != 'right':
heading='left'
head = left
if event.key==locals.K_UP and heading != 'down':
heading='up'
head = up
if event.key==locals.K_DOWN and heading != 'up':
heading='down'
head = down
if heading =='right':
x+=30
if heading =='left':
x-=30
if heading =='up':
y-=30
if heading =='down':
y+=30
if x < 0 or x >630 or y < 0 or y > 450:
exit()
position.append((x,y))
position.pop(0)
if x == applex and y == appley:
applex = random.randint(0,21)*30
appley = random.randint(0,15)*30
score += 1
else :
position.pop(0)
screen.blit(bg,(0,0))
screen.blit(apple,(90,150))
screen.blit(right,(x,y))
screen.blit(apple,(applex,appley))
screen.blit(head,(x,y))
for i in range (len(position)-1):
if (x,y)==position[i]:
exit()
screen.blit(body,position[i])
text ="Score:"+str(score)
info =font.render(text,True,(0,0,0))
screen.blit(info,(540,30))
pygame.display.update()
fps.tick(3)
fps.tick(2+score/4)
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