Commit 3f07ae55 by BellCodeEditor

save project

parent d5739bd2
Showing with 82 additions and 41 deletions
import pygame import pygame
import random
from pygame import locals from pygame import locals
record = 0
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 背景 # 背景
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')
body = pygame.image.load('body.png') # 身体 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')
snake_x , snake_y = 240 , 120
BLACK = (0,0,0)
GREEN = (0,255,0)
BLUE = (0,0,128)
shengcheng = True
x, y = 240, 120 chaoxiang = 'right'
position = [(180, 90), (180, 120), (210, 120), (x, y)] chaoxiang_1 = right
FPSCLOOK = pygame.time.Clock()
setheading = "right" xy_tanchishe = [(180,90), (180,120), (210,120), (snake_x, snake_y)]
snake_head = right
while True: while True:
fontObj = pygame.font.Font('freesansbold.ttf',32)
textSurfaceObj = fontObj.render('Hello world!',True,GREEN,BLUE)
#get_rect()方法返回rect对象
textRectObj = textSurfaceObj.get_rect()
textRectObj.center=(200,150)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left": #print(1)
setheading = 'right' if event.key == locals.K_RIGHT and chaoxiang != 'left':
snake_head = right chaoxiang = 'right'
if event.key == locals.K_LEFT and setheading != "right": chaoxiang_1 = right
setheading = 'left' if event.key == locals.K_LEFT and chaoxiang != 'right':
snake_head = left chaoxiang = 'left'
if event.key == locals.K_UP and setheading != "down": chaoxiang_1 = left
setheading = 'up' if event.key == locals.K_DOWN and chaoxiang != 'up':
snake_head = up chaoxiang = 'down'
if event.key == locals.K_DOWN and setheading != "up": chaoxiang_1 = down
setheading = 'down' if event.key == locals.K_UP and chaoxiang != 'down':
snake_head = down chaoxiang = 'up'
chaoxiang_1 = up
# 设置贪吃蛇的头部坐标 if chaoxiang == 'right':
if setheading == "right": snake_x += 30
x += 30 elif chaoxiang == 'left':
elif setheading == "left": snake_x -= 30
x -= 30 elif chaoxiang == 'up':
elif setheading == "up": snake_y -= 30
y -= 30
else: else:
y += 30 snake_y += 30
position.append((x, y))
position.pop(0) xy_tanchishe.append((snake_x,snake_y))
xy_tanchishe.pop(0)
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇头画上去 # 将贪吃蛇头画上去
screen.blit(snake_head, position[-1]) screen.blit(chaoxiang_1, (snake_x, snake_y))
# 将贪吃蛇的身体画上去 # 将贪吃蛇的身体画上去
for i in range(len(position)-1): for i in range(len(xy_tanchishe)-1):
screen.blit(body, position[i]) screen.blit(body,xy_tanchishe[i])
# screen.blit(body, (210, 120))
# screen.blit(body, (180, 120))
# screen.blit(body, (180, 90))
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) #screen.blit(food, (random.randint(30, 650), random.randint(30, 450)))
if shengcheng:
shengcheng_x = random.randrange(0, 630, 30)
shengcheng_y = random.randrange(0, 450, 30)
shengcheng = False
screen.blit(food, (shengcheng_x, shengcheng_y))
if snake_x == shengcheng_x and snake_y == shengcheng_y:
shengcheng = True
xy_tanchishe.insert(0, xy_tanchishe[0])
record += 1
if snake_x < 0 or snake_x > 660 or snake_y < 0 or snake_y > 480:
print('分数:', record)
exit()
FPSCLOOK.tick(3)
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
\ No newline at end of file
FPSCLOCK.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