Commit 30829f26 by BellCodeEditor

save project

parent 13b1bd8f
Showing with 15 additions and 10 deletions
import pygame import pygame
from pygame import locals from pygame import locals
fkfs = pygame.time.Clock()
import random import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FPSCLOCK=pygame.time.Clock()
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') right = pygame.image.load('right.png')
...@@ -15,8 +16,9 @@ down = pygame.image.load('down.png') ...@@ -15,8 +16,9 @@ down = pygame.image.load('down.png')
food = pygame.image.load('apple.png') food = pygame.image.load('apple.png')
body = pygame.image.load('body.png') body = pygame.image.load('body.png')
mf = pygame.font.Font('neuropol.ttf',18) mf = pygame.font.Font('neuropol.ttf',18)
x,y = 240, 120 x,y=240,120
a_x,a_y = 12, 10 apple_x=12
apple_y=10
snake = [(180,90),(180,120),(210,120),(x,y)] snake = [(180,90),(180,120),(210,120),(x,y)]
setheading = "right" setheading = "right"
st = right st = right
...@@ -50,11 +52,9 @@ while True: ...@@ -50,11 +52,9 @@ while True:
if setheading == "right": if setheading == "right":
x+=30 x+=30
snake.append((x,y)) snake.append((x,y))
if x == a_x*30 and y == a_y*30: if x==apple_x*30 and y==apple_y*30:
a_x = random.randint(0,21) apple_x=random.randint(0,22)
a_y = random.randint(0,15) apple_y=random.randint(0,16)
score +=10
else:
snake.pop(0) snake.pop(0)
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
...@@ -63,13 +63,18 @@ while True: ...@@ -63,13 +63,18 @@ while True:
for i in range(len(snake)-1): for i in range(len(snake)-1):
screen.blit(body,(snake[i])) screen.blit(body,(snake[i]))
# 将果实画上去 # 将果实画上去
screen.blit(food, (a_x*30,a_y*30)) screen.blit(food, (apple_x*30,apple_y*30))
# 刷新画面 # 刷新画面
if x<0 or x>630 or y<0 or y >450:
exit()
for o in range(len(snake)-1):
if snake[o] == (x,y):
exit()
info = 'Score'+str(score) info = 'Score'+str(score)
text = mf.render(info,True,(0,0,0)) text = mf.render(info,True,(0,0,0))
screen.blit(text,(540,10)) screen.blit(text,(540,10))
pygame.display.update() pygame.display.update()
fkfs.tick(3) FPSCLOCK.tick(3)
# a=(x,y) # a=(x,y)
# snake.append(a) # snake.append(a)
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