如何在蛇吃了一个街区之后,如何让更多的敌人弹出

发布于 2025-02-07 02:58:04 字数 4336 浏览 0 评论 0原文

因此,我制作了一款蛇游戏,但是为了使情况更难,我添加了一个用户必须避免的敌人(一个红色的正方形),但我想要它,所以当蛇吃掉其中一种食物时,另一个敌人就会随机产卵。一个例子是,如果他吃了5种食物,那么比赛中将有6个敌人。因此,我想知道如何使另一个敌人在不同的随机位置产卵,但以前仍然保留了观点数量的敌人。这是我现在拥有的代码。任何帮助都将受到赞赏。

import sys,pygame
import random

 
pygame.display.set_caption('Snake Game')
screen_width, screen_height = 600,470
screen = pygame.display.set_mode((screen_width, screen_height))

fps = pygame.time.Clock()
 
snake_speed = 22
 

white = pygame.Color(250, 250, 250)
green = pygame.Color(0, 250, 0)
blue = pygame.Color(250, 0, 0)
black = pygame.Color(0, 0, 0)
red = pygame.Color(250, 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


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()
                sys.exit()
    
    
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
 
    
    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
    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))
    
    pygame.draw.rect(screen, red, pygame.Rect(
      enemy_position[0], enemy_position[1], 10, 10))

    

    if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
        game_over()
    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)

so I made a snake game but in order to make it harder I added a enemy (a red square) that the user has to avoid but I want it so when the snake eats one of its food then another enemy will randomly spawn. An Example would be if has ate 5 of his food then there will be 6 enemies on the game. So I was wondering how to make another enemy spawn in a different random location but still keeping the orignal amount of enemies before. This is the code I have right now. Any help is appreciated.

import sys,pygame
import random

 
pygame.display.set_caption('Snake Game')
screen_width, screen_height = 600,470
screen = pygame.display.set_mode((screen_width, screen_height))

fps = pygame.time.Clock()
 
snake_speed = 22
 

white = pygame.Color(250, 250, 250)
green = pygame.Color(0, 250, 0)
blue = pygame.Color(250, 0, 0)
black = pygame.Color(0, 0, 0)
red = pygame.Color(250, 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


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()
                sys.exit()
    
    
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
 
    
    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
    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))
    
    pygame.draw.rect(screen, red, pygame.Rect(
      enemy_position[0], enemy_position[1], 10, 10))

    

    if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
        game_over()
    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 02:58:04

创建一个敌人的清单:

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

将敌人绘制在循环中,并与敌人进行碰撞测试以循环循环,并随着蛇的进食和成长,在列表中添加新敌人:

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

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

    else:
        snake_body.pop()

    # [...]

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

    # DELETE
    # pygame.draw.rect(screen, red, pygame.Rect(
    #     enemy_position[0], enemy_position[1], 10, 10))

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

    # DELETE
    # if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
    #    game_over()
    
    # [...]

Create a list of enemies:

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

Draw the enemies in a for loop and run the collision test with the enemy in a for loop and add a new enemy to the list as the snake eats and grows:

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

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

    else:
        snake_body.pop()

    # [...]

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

    # DELETE
    # pygame.draw.rect(screen, red, pygame.Rect(
    #     enemy_position[0], enemy_position[1], 10, 10))

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

    # DELETE
    # if snake_position[0] == enemy_position[0] and snake_position[1] == enemy_position[1]:
    #    game_over()
    
    # [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文