python 3.9(蛇游戏)中的队列模块出现问题


import pygame
import random
import queue
pygame.init()
#color values
black = (0, 0, 0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
yellow = (255, 255, 0)
#resolution
diswidth = 600
disheight = 400
#window size and name
dis = pygame.display.set_mode((diswidth,disheight))
pygame.display.set_caption("Snake")
#snake variables
snakeblock = 10
snakespeed = 15
clock = pygame.time.Clock()
Q = queue.Queue(maxsize=10)
#fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
#score function
def yourscore(score):
value = score_font.render(f"Score: {score}", True, yellow)
dis.blit(value, [0, 0])
#function to add tail segments
def oursnake(snakeblock,snakelist):
for x in snakelist:
pygame.draw.rect(dis, black, [x[0], x[1], snakeblock, snakeblock])
#message function
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [diswidth/10, disheight/1.2])
#lots of things
def gameLoop():
game_over = False
game_close = False
direction = (" ")
x1 = diswidth / 2
y1 = disheight / 2
x1_change = 0
y1_change = 0
snakelist = []
snakelenght = 1
foodx = round(random.randrange(0, diswidth - snakeblock) / 10.0) * 10.0
foody = round(random.randrange(0, disheight - snakeblock) / 10.0) * 10.0
#the main while loop
while not game_over:
#this is what happens if the snake collides with itself or with the walls
while game_close == True:
dis.fill(blue)
pygame.HWSURFACE
message("You lost! Press Q to quit or C to play again", red)
pygame.display.update()
#button mapings for game over screen
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
#keypresses
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != "right":
Q.put("left")
elif event.key == pygame.K_RIGHT and direction != "left":
Q.put("right")
elif event.key == pygame.K_UP and direction != "down":
Q.put("up")
elif event.key == pygame.K_DOWN and direction != "up":
Q.put("down")
if Q.empty() is False:
if Q.get() == "left" and direction != "right":
x1_change = -snakeblock
y1_change = 0
direction = "left"
elif Q.get() == "right" and direction != "left":
x1_change = snakeblock
y1_change = 0
direction = "right"
elif Q.get() == "up" and direction != "down":
x1_change = 0
y1_change = -snakeblock
direction = "up"
elif Q.get() == "down" and direction != "up":
x1_change = 0
y1_change = -snakeblock
direction = "down"
pygame.display.update()
if x1 >= diswidth or x1 < 0 or y1 >= disheight or y1 < 0:
game_close = True
#lots of things im too lazy to space out
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, black, [x1,y1,snakeblock,snakeblock])
pygame.draw.rect(dis, green, [foodx, foody, snakeblock, snakeblock])
pygame.display.update()
snakehead = []
snakehead.append(x1)
snakehead.append(y1)
snakelist.append(snakehead)
if len(snakelist) > snakelenght:
del snakelist[0]
for x in snakelist[:-1]:
if x == snakehead:
game_close = True
oursnake(snakeblock, snakelist)
yourscore(snakelenght - 1)
pygame.display.update()
#Eats food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, diswidth - snakeblock) / 10.0) * 10.0
foody = round(random.randrange(0, disheight - snakeblock) / 10.0) * 10.0
snakelenght += 1
clock.tick(snakespeed)
pygame.quit()
quit()
gameLoop()

这是我玩蛇游戏的代码。我在队列模块方面遇到了问题,因为每当我试图输入它不会输入的东西时,如果我去检查队列大小,它会在每次勾选时更新自己,如果它是空的,但一旦其中有东西就会停止。

我只能向左移动(其他方向不允许我移动(,当我向左移动后按下某个东西时,它会停在原地,我必须终止程序。

我想知道我的代码是否有问题(可能(,但如果是这样的话,为什么它让我向左移动,而不是任何其他方向。任何帮助都将不胜感激此外,如何使帧速率更平滑?

我认为这是因为您正在消耗第一个if Q.get() == "left"上的队列,所以elif Q.get() == "right"正在处理队列中的下一个项目,而这个项目通常只是空的。

在测试之前,将从队列中提取的项目放入本地变量中

next_command = Q.get()
if next_command == "left" and direction != "right":
x1_change = -snakeblock
y1_change = 0
direction = "left"
elif next_command == "right" and direction != "left":
x1_change = snakeblock
y1_change = 0
direction = "right"
elif next_command == "up" and direction != "down":
...

最新更新