基本上是标题。我不确定问题是什么。我尝试过一些小东西试图修复它。复制并粘贴并等待一秒钟,然后按任意键。它自动失败。很感激帮助。可能是因为我试图添加的等待但我不太确定。应该是一个相当快速的修复。在我的游戏至少功能正常之前,这是最后要做的事情。
import pygame
import random
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
lime = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
Aqua = (0,255,255)
display_height = 800
display_width = 600
snake_width = 10
snake_length = 10
snake_x = 400
snake_y = 300
f = 0
food_w = 10
food_h = 10
gameDisplay = pygame.display.set_mode((display_height,display_width))
pygame.init()
clock = pygame.time.Clock()
def message_to_screen(msg, color, msgx, msgy):
font = pygame.font.SysFont("Times New Roman", 18)
datext = font.render(msg, 1, color)
gameDisplay.blit(datext, [msgx, msgy])
def wait():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key ==
pygame.K_RETURN:
game()
def food(food_x,food_y):
pygame.draw.rect(gameDisplay, white, [food_x, food_y, food_w, food_h])
pygame.display.update()
def snake(block_size, snakelist):
for XnY in snakelist:
pygame.draw.rect(gameDisplay, Aqua, [XnY[0], XnY[1], snake_width,
snake_length])
pygame.display.update()
def text_objects(text,font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def display_message(text):
largeText = pygame.font.Font('freesansbold.ttf', 35)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((400),(300))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def score(count):
myFont = pygame.font.SysFont("Times New Roman", 18)
score_label = myFont.render("You're Score:" +str(count), 1, red)
gameDisplay.blit(score_label, (0, 0))
def lose():
display_message('You Lose! Press ENTER to Play Again')
wait()
game()
def game():
food_startx = round(random.randrange(0, display_width-food_w)/10.0)*10.0
food_starty = round(random.randrange(0, display_height-
food_h)/10.0)*10.0
snakelist = []
snakeLength = 50
f = 0
snake_startx = 400
snake_starty = 300
snake_speedx = 0
snake_speedy = 0
block_size = (10,10)
snake_head = (snake_startx, snake_starty)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_speedx = -5
snake_speedy = 0
if event.key == pygame.K_RIGHT:
snake_speedx = 5
snake_speedy = 0
if event.key == pygame.K_UP:
snake_speedy = -5
snake_speedx = 0
if event.key == pygame.K_DOWN:
snake_speedy = 5
snake_speedx = 0
if snake_startx < 0 or snake_startx > 800 or snake_starty > 600 or
snake_starty < 0:
lose()
score(f)
snake_startx += snake_speedx
snake_starty += snake_speedy
snakeHead = []
snakeHead.append(snake_startx)
snakeHead.append(snake_starty)
snakelist.append(snakeHead)
if len(snakelist) > snakeLength and snakeHead!= 1:
del snakelist[0]
for eachSegment in snakelist[:-1]:
if eachSegment == snakeHead and snakeHead != 1:
lose()
snake(block_size, snakelist)
food(food_startx, food_starty)
if snake_startx == food_startx and snake_starty == food_starty:
food_startx = round(random.randrange(0, display_width - food_w)
/ 10.0) * 10.0
food_starty = round(random.randrange(0, display_height - food_h)
/ 10.0) * 10.0
f += 1
snakeLength += 1
if food_startx >= 800 or food_startx <= 0:
food_startx = round(random.randrange(0, display_width - food_w)
/ 10.0) * 10.0
food_starty = round(random.randrange(0, display_height - food_h)
/ 10.0) * 10.0
if food_starty >= 600 or food_starty <= 0:
food_startx = round(random.randrange(0, display_width - food_w)
/ 10.0) * 10.0
food_starty = round(random.randrange(0, display_height - food_h)
/ 10.0) * 10.0
pygame.display.update()
gameDisplay.fill(black)
clock.tick(45)
game()
pygame.quit()
答案 0 :(得分:0)
您遇到的问题是您已将速度初始化为(0, 0)
,因此下一个生成的细分受到了影响,导致立即失败。除了其他一些虫子之外,我还修好了这个虫子,比如你的蛇的长度超出了它的长度,这意味着蛇可能会错过半个长度的食物,所以它会半食穿过食物。我还整理了你的命名,并在这里和那里添加了一些更简洁的习语(例如评论中建议的链式比较器范围检查)。
import pygame
import random
directions = {pygame.K_LEFT: (-1, 0),
pygame.K_RIGHT: (1, 0),
pygame.K_UP: (0, -1),
pygame.K_DOWN: (0, 1),
}
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
lime = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
aqua = (0,255,255)
display_width = 600
display_height = 800
snake_width = 10
snake_length = 10
snake_x = 400
snake_y = 300
food_w = 10
food_h = 10
game_display = pygame.display.set_mode((display_height,display_width))
pygame.init()
clock = pygame.time.Clock()
def round_to_nearest(num, interval):
return round(float(num) / interval) * interval
def rounded_in_range(start, stop, interval):
return round_to_nearest(random.randrange(start, stop), interval)
def message_to_screen(msg, color, msgx, msgy):
font = pygame.font.SysFont("Times New Roman", 18)
datext = font.render(msg, 1, color)
game_display.blit(datext, [msgx, msgy])
def wait():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
game()
def food(food_x,food_y):
pygame.draw.rect(game_display, white, [food_x, food_y, food_w, food_h])
pygame.display.update()
def snake(snake_width, snake_length, snake_segments):
for XnY in snake_segments:
pygame.draw.rect(game_display, aqua, [XnY[0], XnY[1], snake_width, snake_length])
pygame.display.update()
def text_objects(text,font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def display_message(text):
largeText = pygame.font.Font('freesansbold.ttf', 35)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = (400,300)
game_display.blit(TextSurf, TextRect)
pygame.display.update()
def score(count):
myFont = pygame.font.SysFont("Times New Roman", 18)
score_label = myFont.render("Your Score: {}".format(count), 1, red)
game_display.blit(score_label, (0, 0))
def lose():
display_message('You Lose! Press ENTER to Play Again')
wait()
game()
def game():
food_x = rounded_in_range(food_w, display_width - food_w, food_w)
food_y = rounded_in_range(food_h, display_height - food_h, food_h)
snake_segments = []
target_segments = 50
player_score = 0
snake_x = 400
snake_y = 300
snake_speedx, snake_speedy = directions[pygame.K_RIGHT]
snake_speedx *= snake_width
snake_speedy *= snake_length
snake_head = (snake_x, snake_y)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
snake_speedx, snake_speedy = directions[event.key]
snake_speedx *= snake_width
snake_speedy *= snake_length
if not 0 < snake_x < game_display.get_width() or not 0 < snake_y < game_display.get_height():
lose()
score(player_score)
snake_x += snake_speedx
snake_y += snake_speedy
if len(snake_segments) > target_segments:
del snake_segments[0]
snake_head = [snake_x, snake_y]
snake_segments.append(snake_head)
for segment in snake_segments[:-1]:
if segment == snake_head:
lose()
snake(snake_width, snake_length, snake_segments)
food(food_x, food_y)
if snake_x == food_x and snake_y == food_y:
food_x = rounded_in_range(food_w, display_width - food_w, food_w)
food_y = rounded_in_range(food_h, display_height - food_h, food_h)
print("new food at {} {}".format(food_x, food_y))
player_score += 1
target_segments += 1
pygame.display.update()
game_display.fill(black)
clock.tick(10)
game()
pygame.quit()
我不保证这个程序是完美的 - 例如你仍然可以回到自己身上,造成直接死亡,食物可以在蛇体内产卵,但现在它应该可以运作了。