Commit 6c9d6d28 by BellCodeEditor

auto save

parent 16f0d60e
Showing with 54 additions and 9 deletions
import pygame
from pygame import locals
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
......@@ -10,23 +11,67 @@ screen = pygame.display.set_mode((660, 480))
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')
food = pygame.image.load('apple.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
left = pygame.image.load('left.png')
apple = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
my_font = pygame.font.Font('neuropol.ttf', 18)
score = 0
CLOCK = pygame.time.Clock()
x , y = 90, 0
position = [(0,0),(30,0),(60,0),(x,y)]
heading = 'right'
snake_head = right
apple_x = 12
apple_y = 10
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_DOWN and heading != 'up':
heading = 'down'
snake_head = down
if event.key == locals.K_UP and heading != 'down':
heading = 'up'
snake_head = up
if event.key == locals.K_RIGHT and heading != 'left':
heading = 'right'
snake_head = right
if event.key == locals.K_LEFT and heading != 'right':
heading = 'left'
snake_head = left
if heading == 'right':
x += 30
elif heading == 'left':
x -= 30
elif heading == 'up':
y -= 30
else:
y += 30
position.append((x,y))
if x < 0 or x > 630 or y < 0 or y > 450:
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, (240, 120))
screen.blit(snake_head, (x, y))
# 将贪吃蛇的身体画上去
screen.blit(body, (210, 120))
screen.blit(body, (180, 120))
screen.blit(body, (180, 90))
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (360, 300))
screen.blit(apple, (apple_x*30, apple_y*30))
if x == (apple_x*30) and y == (apple_y*30):
apple_x = random.randint(0,21)
apple_y = random.randint(0,15)
score += 10
else:
position.pop(0)
info = 'Score ' + str(score)
text = my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
# 刷新画面
pygame.display.update()
\ No newline at end of file
pygame.display.update()
CLOCK.tick(3)
\ 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