Commit 139ca2e6 by BellCodeEditor

save project

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