Commit 139ca2e6 by BellCodeEditor

save project

parent 4aa4d8b5
Showing with 35 additions and 5 deletions
import pygame,random
import pygame,random,os
from pygame import locals
score=0
# 初始化pygame,为使用pygame做准备
def shuax():
global apple_x
global apple_y
apple_x = 30*random.randint(0,20)
apple_y = 30*random.randint(0,16)
pygame.init()
my_font = pygame.font.Font('neuropol.ttf',18)
background = pygame.image.load('bg.png')
right= pygame.image.load('right.png')
food = pygame.image.load('apple.png')
......@@ -19,6 +27,7 @@ FPSCLOCK = pygame.time.Clock()
x,y=240,120
pos=[(180,90),(180,120),(210,120),(x,y)]
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
......@@ -47,20 +56,41 @@ while True:
elif setheading == "down":
y+=30
if (x,y)==(apple_x,apple_y):
print()
apple_x = 30*random.randint(0,20)
apple_y = 30*random.randint(0,16)
shuax()
score+=1
# os.system('cls')
# print('score:'+str(score))
if (apple_x,apple_y) in pos:
shuax()
else:
pos.pop(0)
pos.pop(0)
if (x,y) in pos[0:-2]:
exit()
pos.append((x,y))
screen.blit(background,(0,0))
screen.blit(snake_head,pos[-1])
screen.blit(food,(apple_x,apple_y))
for i in pos[0:-1]:
screen.blit(body,i)
info = 'score:'+ str(score)
text = my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
pygame.display.update()
if x>660:
(x,y)=(0,y)
elif x<0:
(x,y) = (680,y)
elif y>480:
(x,y) = (x,0)
elif y<0:
(x,y) = (x,480)
FPSCLOCK.tick(3)
......
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