Commit d9b6ea0d by BellCodeEditor

save project

parent b193f201
Showing with 17 additions and 4 deletions
import pygame import pygame
import random
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
...@@ -8,6 +9,7 @@ pygame.init() ...@@ -8,6 +9,7 @@ pygame.init()
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FPS=pygame.time.Clock() FPS=pygame.time.Clock()
# 背景 # 背景
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')
left = pygame.image.load('left.png') left = pygame.image.load('left.png')
...@@ -19,6 +21,8 @@ x,y=240,120 ...@@ -19,6 +21,8 @@ x,y=240,120
setheading="right" setheading="right"
snake=right snake=right
position=[(180,90),(180,120),(210,120),(x,y)] position=[(180,90),(180,120),(210,120),(x,y)]
apple_x,apple_y=300,300
score=0
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:
...@@ -47,7 +51,14 @@ while True: ...@@ -47,7 +51,14 @@ while True:
y-=30 y-=30
snake=up snake=up
position.append((x,y)) position.append((x,y))
position.pop(0) if x<0 or x>630 or y<0 or y>450:
exit()
if x==apple_x and y==apple_y:
apple_y=random.randint(1,16)*30-30
apple_x=random.randint(1,22)*30-30
score+=10
else:
position.pop(0)
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
screen.blit(snake,position[-1]) screen.blit(snake,position[-1])
...@@ -57,7 +68,9 @@ while True: ...@@ -57,7 +68,9 @@ while True:
#screen.blit(body, (180, 120)) #screen.blit(body, (180, 120))
#screen.blit(body, (180, 90)) #screen.blit(body, (180, 90))
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x, apple_y))
text = my_font.render('Score:'+str(score),True,(200,200,200))
screen.blit(text,(0,0))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(3) FPS.tick(10)
\ No newline at end of file \ 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