Fondamentalement, le titre. Je ne suis pas sûr du problème. J'ai essayé quelques petites choses pour tenter de le réparer. Copiez et collez et attendez une seconde avant d'appuyer sur une touche. Il perd automatiquement. J'apprécierais l'aide. Peut-être cause de mon attente j'ai essayé d'ajouter mais je ne suis pas vraiment sûr. Devrait être une solution assez rapide. C'est la dernière chose à faire avant que mon jeu soit au moins fonctionnel.Jeu de serpent perd automatiquement
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()
Vraisemblablement les trois lignes après 'def message_to_screen' devraient être indentées, non? –
Vous pouvez simplifier des conditions telles que 'si x < 0 or x > y' à' sinon 0 <= x <= y'. –
ouais pour une raison quelconque mon pc a des problèmes avec mettre le code dans le format correct pour le site Web, donc je dois passer par et faire de mon mieux pour indenter tout correctement. Il y a quelques erreurs de ce genre qui ne sont pas dans le code réel –