基本上是標題。我不確定問題是什麼。我嘗試了幾件小事來嘗試修復它。複製並粘貼並等待一秒鐘,然後按任意鍵。它會自動丟失。將不勝感激的幫助。可能是因爲我試圖添加的等待,但我不確定。應該是一個相當快速的修復。這是我的遊戲至少在功能上最後要做的事情。貪吃蛇遊戲自動失敗
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()
大概在'def message_to_screen'之後的三行應該縮進,對吧? –
您可以簡化如'if x < 0 or x > y'到'如果不是0 <= x <= y'的條件。 –
是的,出於某種原因,我的電腦有問題,把網站的正確格式的代碼,所以我必須經過並盡我所能正確縮進一切。有可能出現一些像這樣的錯誤,不在實際的代碼 –