Commit ef01f323 by BellCodeEditor

auto save

parent 161ffa81
Showing with 59 additions and 6 deletions
import pygame
import random
from pygame import locals
pygame.init()
screen=pygame.display.set_mode((660,480))
background=pygame.image.load('bg.png')
apple=pygame.image.load('apple.png')
screen=pygame.display.set_mode((640,480))
right=pygame.image.load('right.png')
food=pygame.image.load('apple.png')
body=pygame.image.load('body.png')
setheading='right'
up=pygame.image.load('up.png')
down=pygame.image.load('down.png')
left=pygame.image.load('left.png')
myfont=pygame.font.Font('neuropol.ttf',18)
score=0
head=right
t=pygame.time.Clock()#pygame时钟,控制游戏速度(帧数)
x,y=240,120
applex=360
appley=300
position=[(180,90),(180,120),(210,120),(x,y)]
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'
head=right
if event.key == locals.K_LEFT and setheading != 'right':
setheading='left'
head=left
if event.key == locals.K_UP and setheading != 'down':
setheading='up'
head=up
if event.key == locals.K_DOWN and setheading != 'up':
setheading='down'
head=down
if setheading=='right':
x+=30
elif setheading=='left':
x-=30
elif setheading=='up':
y-=30
else:
y+=30
if applex==x and appley==y:
x1=random.randint(1,22)
y1=random.randint(1,16)
applex=x1*30-30
appley=y1*30-30
score+=1
else:
position.pop(0)
if x<0 or x>630 or y<0 or y>450:
exit()
xinxi='score'+str(score)
text=myfont.render(xinxi,True,(0,0,0))
screen.blit(text,(540,200))
screen.blit(background,(0,0))
screen.blit(apple,(0,0))
screen.blit(apple,(0,450))
screen.blit(apple,(610,0))
screen.blit(apple,(610,450))
position.append((x,y))
screen.blit(head,position[-1])
for i in range(len(position)-1):
screen.blit(body,position[i])
screen.blit(food,(applex,appley))
pygame.display.update()
t.tick(3)
\ No newline at end of file
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