Python 在pygame中碰撞后,我如何在其他地方随机繁殖敌人?

Python 在pygame中碰撞后,我如何在其他地方随机繁殖敌人?,python,pygame,Python,Pygame,由于某种原因,当我与敌人碰撞时,它会在其他地方随机繁殖,这一切都很好,但当我解除碰撞时,敌人会回到它原来的位置 下面是完整的代码。如果你运行这段代码,看看会发生什么。我在寻找红色方块,当蓝色方块与它碰撞时,它会在一个随机的其他位置生成 导入pygame 随机输入 #敌人可以繁殖的地方(2个,一开始很简单) 敌方位置=[100200] pygame.init() #钟 clock=pygame.time.clock() #每秒帧数 fps=30 #颜色 背景颜色=(255、255、255) 玩家颜

由于某种原因,当我与敌人碰撞时,它会在其他地方随机繁殖,这一切都很好,但当我解除碰撞时,敌人会回到它原来的位置 下面是完整的代码。如果你运行这段代码,看看会发生什么。我在寻找红色方块,当蓝色方块与它碰撞时,它会在一个随机的其他位置生成

导入pygame
随机输入
#敌人可以繁殖的地方(2个,一开始很简单)
敌方位置=[100200]
pygame.init()
#钟
clock=pygame.time.clock()
#每秒帧数
fps=30
#颜色
背景颜色=(255、255、255)
玩家颜色=(0,0,255)
颜色=(255,0,0)
#屏幕的宽度和高度
宽度=1000
高度=800
#屏风
screen=pygame.display.set_模式((宽度、高度))
#x,y坐标播放器
玩家x=300
游戏者y=300
#ememy x,y坐标
敌人x=随机选择(敌人位置)
敌人y=随机选择(敌人位置)
#敌人玩家的新x,y坐标
新建x=随机选择(敌人位置)
新建y=随机选择(敌人位置)
#平局球员
def draw():
敌人方=pygame.rect(敌人方,敌人方,25,25)
player_rect=pygame.rect(player_x,player_y,25,25)
如果玩家正确碰撞(敌人正确):
敌方=pygame.draw.rect(屏幕,敌方颜色,(新x,新y,25,25))
其他:
敌方=pygame.draw.rect(屏幕、敌方颜色、敌方矩形)
player=pygame.draw.rect(屏幕、玩家颜色、玩家rect)
#pygame循环(始终包括)
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#播放器对象的控件
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
玩家_x-=10
如果event.key==pygame.K_RIGHT:
玩家x+=10
如果event.key==pygame.K_UP:
玩家_y-=10
如果event.key==pygame.K_向下:
玩家_y+=10
画()
pygame.display.update()
屏幕填充(背景颜色)
时钟滴答声(fps)

绘制方法明确规定在初始点或某个随机点绘制敌方矩形。随机点仅选择一次-在应用程序启动期间。每次在
draw
方法中发生冲突时,都应设置随机点,例如:

if player_rect.colliderect(enemy_rect):
    enemy_x = random.choice(enemy_locations)
    enemy_y = random.choice(enemy_locations)

正如其他人所说,您需要在列表中添加更多的“位置”,或者更好地使用
random.randint()
,而且敌人的坐标不会在每次碰撞后“变得随机”(或以任何方式更新),而只在初始化时才进行一次。 我添加了新函数
get_new_coord()
,它将返回随机数(我使用这些限制,这样敌人就不会离开屏幕),每次碰撞后,每个坐标都会调用一次。 此外,我还将玩家和敌人移动到循环之外,现在
draw()
返回
敌人
,以便在发生碰撞时保持更新

<>这是对代码的快速修复,应该考虑使用类。当然,如果您想这样做,我可以帮助您转换代码

import pygame
import random

# places where enemies can spawn (2 to make it simple at first)
enemy_locations = [100, 200]


pygame.init()
# clock
clock = pygame.time.Clock()

# frames per second
fps = 30

# colors
background_color = (255, 255, 255)
player_color = (0, 0, 255)
enemy_color = (255, 0, 0)

# width and height of screen
width = 1000
height = 800

# screen
screen = pygame.display.set_mode((width, height))

# x, y coordinates player
player_x = 300
player_y = 300

# ememy x, y coordinates
enemy_x = random.choice(enemy_locations)
enemy_y = random.choice(enemy_locations)

# new x, y coordinates for enemy player
new_x = random.choice(enemy_locations)
new_y = random.choice(enemy_locations)

def get_new_coord():
    return random.randint(0, 800-25)

# draw player
def draw(player_rect, enemy_rect):

    screen.fill(background_color)

    if player_rect.colliderect(enemy_rect):
        enemy_rect = pygame.Rect(get_new_coord(), get_new_coord(), 25, 25)
        enemy = pygame.draw.rect(screen, enemy_color, enemy_rect)
    else:
        enemy = pygame.draw.rect(screen, enemy_color, enemy_rect)

    player = pygame.draw.rect(screen, player_color, player_rect)

    pygame.display.update()

    return enemy_rect


# pygame loop (always include)
running = True
enemy_rect = pygame.Rect(enemy_x, enemy_y, 25, 25)
player_rect = pygame.Rect(player_x, player_y, 25, 25)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # controls for player object
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_rect.x -= 10
            if event.key == pygame.K_RIGHT:
                player_rect.x += 10
            if event.key == pygame.K_UP:
                player_rect.y -= 10
            if event.key == pygame.K_DOWN:
                player_rect.y += 10

    enemy_rect = draw(player_rect, enemy_rect)

    clock.tick(fps)

当玩家与敌人碰撞时,你必须创建一个新的随机敌人位置

如果玩家正确碰撞(敌人正确):
敌人x=随机选择(敌人位置)
敌人y=随机选择(敌人位置)
使用,将变量
敌军_x
敌军_y
解释为全局变量。使用语句
global
可以在函数中写入全局命名空间中的变量:

全球敌人x,敌人y
函数
绘制

def draw():
全球敌人
敌人方=pygame.rect(敌人方,敌人方,25,25)
player_rect=pygame.rect(player_x,player_y,25,25)
如果玩家正确碰撞(敌人正确):
敌人x=随机选择(敌人位置)
敌人y=随机选择(敌人位置)
敌方=pygame.draw.rect(屏幕、敌方颜色、敌方矩形)
player=pygame.draw.rect(屏幕、玩家颜色、玩家rect)

在这里使用
new\u x=random.choice()
时,您会给它一个只有2个值的列表作为参数,因此只有4种可能的x,y组合,敌人可能会在其中繁殖。而是使用
new\u x=random.randint(0,宽度)
new\u y=random.randint(0,高度)
。这将导致屏幕上任意位置出现随机位置。