|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问大神下面代码问题出在哪里?为什么控制不了蛇改变方向?
###导入库
import pygame
from pygame.locals import *
import time
import random
import sys
#初始化pygame
pygame.init()
###全局变量定义
#蛇的移动速度
snake_speed = 5
#游戏窗口尺寸
window_x = 720
window_y = 480
win_size = (720, 480)
#颜色定义
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
#初始化游戏title和窗口
pygame.display.set_caption('贪吃蛇_v1.0')
game_window = pygame.display.set_mode(win_size)
#定义游戏刷新帧率
fps = pygame.time.Clock()
#定义蛇的初始位置
snake_position = [100, 50]
#定义蛇身的前4个块
snake_body = [[100, 50],
[90, 50],
[80, 50],
[70, 50]
]
#定义水果位置
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
#设置蛇的默认方向
direction = 'RIGHT'
change_to = direction
#初始分数
score = 0
#显示得分功能
def show_score(choice, color, font, size):
#创建字体对象
score_font = pygame.font.SysFont(font, size)
#创建surface对象
score_surface = score_font.render('Score: ' + str(score), True, color)
#为上述文本表面对象创建矩形对象
score_rect = score_surface.get_rect()
#显示文字
game_window.blit(score_surface, score_rect)
#游戏结束功能
def game_over():
#创建字体对象my_font
my_font = pygame.font.SysFont('times new roman', 50)
#创建文本表面对象
game_over_surface = my_font.render('Your Score is: ' + str(score), True, red)
#创建文本的矩形对象
game_over_rect = game_over_surface.get_rect()
#设置文本位置
game_over_rect.midtop = (window_x/2, window_y/2)
#显示文字
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
#2秒后退出程序
time.sleep(2)
pygame.quit()
quit()
#主程序
while 1:
#处理键盘事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to == 'UP'
if event.key == pygame.K_DOWN:
change_to == 'DOWN'
if event.key == pygame.K_LEFT:
change_to == 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
#防止蛇调头
if (change_to == 'UP') and (direction != 'DOWN'):
direction = 'UP'
if (change_to == 'DOWN') and (direction != 'UP'):
direction = 'DOWN'
if (change_to == 'LEFT') and (direction != 'RIGHT'):
direction = 'LEFT'
if (change_to == 'RIGHT') and (direction != 'LEFT'):
direction = 'RIGHT'
#蛇移动
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'RIGHT':
snake_position[0] += 10
#得分和(没有蛇体增长)
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
score += 10
#fruit_spawn = False
else:
snake_body.pop()
if not fruit_spawn:
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
game_window.fill(black)
#蛇身显示
for pos in snake_body:
pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
#显示水果
pygame.draw.rect(game_window, white, pygame.Rect(fruit_position[0], fruit_position[1], 10, 10))
'''#游戏结束条件
if snake_position[0] < 0 or snake_position[0] > window_x-10:
game_over()
if snake_position[1] < 0 or snake_position[0] > window_y-10:
game_over()
#蛇头碰撞蛇身
for block in snake_body:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()
'''
'''#连续显示分数
show_score(1, white, 'times new roman', 20)
'''
#游戏刷新和帧率
pygame.display.update()
fps.tick(snake_speed) |
|