为什么我的游戏没有开始?我刚刚添加了一个列表和2个循环,但现在它不会启动

发布于 2025-02-07 05:56:48 字数 4787 浏览 0 评论 0原文

我有一个蛇游戏,但是每次蛇都会吃一个障碍时,敌人的含量有所不同。我查看了代码,游戏不会显示,有人知道为什么吗?任何帮助都将受到赞赏。

import sys,pygame
import random

 
pygame.display.set_caption('Snake Game')
screen_width, screen_height = 500,400
screen = pygame.display.set_mode((screen_width, screen_height))

fps = pygame.time.Clock()
 
snake_speed = 20
 
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(255, 0, 0)

pygame.init()

snake_position = [100, 50]
 

snake_body = [  [150, 80],
                [100, 50],
                [100, 50],
                [80, 50]
            ]

food_position = [random.randrange(1, (screen_width//10)) * 10,
                  random.randrange(1, (screen_height//10)) * 10]
food_spawn = True

enemy_position = [random.randrange(1, (screen_width//10)) * 10,
                  random.randrange(1, (screen_height//10)) * 10]

enemy_spawn = True

这是我添加的第一件事。 (列表)

enemy_list = [random.randrange(1, (screen_width//10)) * 10,
              random.randrange(1, (screen_height//10)) * 10]

direction = 'RIGHT'


score = 0
 

def show_score(choice, color, font, size):
   
    
    score_font = pygame.font.SysFont(font, size)
     
    
    score_surface = score_font.render('Score : ' + str(score), True, color)
     
    
    score_rect = score_surface.get_rect()
     
    
    screen.blit(score_surface, score_rect)
blue = pygame.Color(0, 0, 255)

def game_over():
            
    my_font = pygame.font.SysFont('comic sans', 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 = (screen_width/2, screen_height/4)
    
    screen.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            
while True:
    change_to = direction
    for event in pygame.event.get():
        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
 

然后我添加了此循环并插入了列表

while True:
    for enemy_position in enemy_list:
        pygame.draw.rect(screen, red, pygame.Rect(
            enemy_position[0], enemy_position[1], 10, 10))

    for enemy_position in enemy_list:
        if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
            game_over()

,然后我放入的另一个循环。(和列表)

while True:
    
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        score += 1
        food_spawn = False

        enemy_list.append(
            [random.randrange(1, (screen_width//10)) * 10,
             random.randrange(1, (screen_height//10)) * 10]
        )

    else:
        snake_body.pop()


         
    if not food_spawn:
        food_position = [random.randrange(1, (screen_width//10)) * 10,
                          random.randrange(1, (screen_height//10)) * 10]

        food_spawn = True
    screen.fill(black)
    
    for pos in snake_body:
        pygame.draw.rect(screen, blue, pygame.Rect(
          pos[0], pos[1], 10, 10))
    
         
    pygame.draw.rect(screen, green, pygame.Rect(
      food_position[0], food_position[1], 10, 10))
           
        
    if snake_position[0] < 0 or snake_position[0] > screen_width-10:
        game_over()
        
    if snake_position[1] < 0 or snake_position[1] > screen_height-10:
        game_over()
    
    
    for block in snake_body[1:]:
        if snake_position[0] == block[0] and snake_position[1] == block[1]:
            game_over()

    
    show_score(1, white, 'comic sans', 20)
    show_score(1, white, 'comic sans', 20)
    
    
    pygame.display.update()

    
    fps.tick(snake_speed)

    

I have a snake game but what's different is enemies spawn every time the snake eats a block. I've looked over the code and the game just won't display, does anyone know why? Any help is appreciated.

import sys,pygame
import random

 
pygame.display.set_caption('Snake Game')
screen_width, screen_height = 500,400
screen = pygame.display.set_mode((screen_width, screen_height))

fps = pygame.time.Clock()
 
snake_speed = 20
 
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(255, 0, 0)

pygame.init()

snake_position = [100, 50]
 

snake_body = [  [150, 80],
                [100, 50],
                [100, 50],
                [80, 50]
            ]

food_position = [random.randrange(1, (screen_width//10)) * 10,
                  random.randrange(1, (screen_height//10)) * 10]
food_spawn = True

enemy_position = [random.randrange(1, (screen_width//10)) * 10,
                  random.randrange(1, (screen_height//10)) * 10]

enemy_spawn = True

This was the first thing I added. (The list)

enemy_list = [random.randrange(1, (screen_width//10)) * 10,
              random.randrange(1, (screen_height//10)) * 10]

direction = 'RIGHT'


score = 0
 

def show_score(choice, color, font, size):
   
    
    score_font = pygame.font.SysFont(font, size)
     
    
    score_surface = score_font.render('Score : ' + str(score), True, color)
     
    
    score_rect = score_surface.get_rect()
     
    
    screen.blit(score_surface, score_rect)
blue = pygame.Color(0, 0, 255)

def game_over():
            
    my_font = pygame.font.SysFont('comic sans', 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 = (screen_width/2, screen_height/4)
    
    screen.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            
while True:
    change_to = direction
    for event in pygame.event.get():
        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
 

Then I added this loop and inserted the list

while True:
    for enemy_position in enemy_list:
        pygame.draw.rect(screen, red, pygame.Rect(
            enemy_position[0], enemy_position[1], 10, 10))

    for enemy_position in enemy_list:
        if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
            game_over()

Then my another loop I put in. (and the list)

while True:
    
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        score += 1
        food_spawn = False

        enemy_list.append(
            [random.randrange(1, (screen_width//10)) * 10,
             random.randrange(1, (screen_height//10)) * 10]
        )

    else:
        snake_body.pop()


         
    if not food_spawn:
        food_position = [random.randrange(1, (screen_width//10)) * 10,
                          random.randrange(1, (screen_height//10)) * 10]

        food_spawn = True
    screen.fill(black)
    
    for pos in snake_body:
        pygame.draw.rect(screen, blue, pygame.Rect(
          pos[0], pos[1], 10, 10))
    
         
    pygame.draw.rect(screen, green, pygame.Rect(
      food_position[0], food_position[1], 10, 10))
           
        
    if snake_position[0] < 0 or snake_position[0] > screen_width-10:
        game_over()
        
    if snake_position[1] < 0 or snake_position[1] > screen_height-10:
        game_over()
    
    
    for block in snake_body[1:]:
        if snake_position[0] == block[0] and snake_position[1] == block[1]:
            game_over()

    
    show_score(1, white, 'comic sans', 20)
    show_score(1, white, 'comic sans', 20)
    
    
    pygame.display.update()

    
    fps.tick(snake_speed)

    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2025-02-14 05:56:48

据我了解,您在true 方法时写了三个单独的。第一个进行控制,第二个要进行状态控制,第三个用于实际绘制游戏。

但是,如果您希望所有这些都立即运行,则必须在同一循环中。目前,您的第一个是true循环一遍又一遍地运行。因此,您的第二和第三个循环实际上将游戏从未运行。

From what I understand, you have written three separate while True methods. The first to take in controls, the second to do state control, and the third for actually drawing the game.

However, if you want all of these to run at once, they must be inside the same loop. At the moment, your first while True loop is running over and over again. Thus, your second and third loops, which actually draw the game out, are never running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文