Commit b23b5c2e by BellCodeEditor

save project

parent 055e671d
Showing with 28 additions and 9 deletions
import pygame import pygame
import random
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
...@@ -15,13 +16,17 @@ body = pygame.image.load('body.png') ...@@ -15,13 +16,17 @@ body = pygame.image.load('body.png')
left = pygame.image.load('left.png') left = pygame.image.load('left.png')
up = pygame.image.load('up.png') up = pygame.image.load('up.png')
down = pygame.image.load('down.png') down = pygame.image.load('down.png')
my_font = pygame.font.Font("neuropol.ttf",18)
apple_x = 360
apple_y = 300
FPSCLOCK = pygame.time.Clock() FPSCLOCK = pygame.time.Clock()
x,y = 240,120 x,y = 240,120
u=[(180,90),(180,120),(210,120),(x,y)] u=[(180,90),(180,120),(210,120),(x,y)]
setheading = "right" setheading = "right"
snake_head = right snake_head = right
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:
...@@ -35,10 +40,10 @@ while True: ...@@ -35,10 +40,10 @@ while True:
setheading = 'left' setheading = 'left'
snake_head = left snake_head = left
if event.key == locals.K_UP and setheading != "down": if event.key == locals.K_UP and setheading != "down":
setheading == 'up' setheading = 'up'
snake_head = up snake_head = up
if event.key == locals.K_DOWN and setheading != "up": if event.key == locals.K_DOWN and setheading != "up":
setheading == 'down' setheading = 'down'
snake_head = down snake_head = down
# 将贪吃蛇画上去 # 将贪吃蛇画上去
...@@ -50,13 +55,24 @@ while True: ...@@ -50,13 +55,24 @@ while True:
y -=30 y -=30
else: else:
y +=30 y +=30
#x+=30
u.append((x,y)) u.append((x,y))
u.pop(0)
if x == apple_x and y == apple_y:
num1 = random.randint(1,22)
num2 = random.randint(1,16)
apple_x = num1 * 30-30
apple_y = num2 * 30-30
score += 10
#apple_x = random.randint(0,660)
#apple_y = random.randint(0,480)
else:
u.pop(0)
if x < 0 or x > 630 or y < 0 or y > 450:
exit()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
screen.blit(right, u[-1]) screen.blit(snake_head, u[-1])
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
for i in range(len(u)-1): for i in range(len(u)-1):
screen.blit(body,u[i]) screen.blit(body,u[i])
...@@ -64,7 +80,10 @@ while True: ...@@ -64,7 +80,10 @@ 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))
info = "Score"+ str(score)
text = my_font.render(info,True,(0,0,200))
screen.blit(text,(540,10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) 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