简体   繁体   English

Pygame随机坐标生成

[英]Pygame Random Coordinates Spawning

In the game I'm trying to create, I want to spawn mummies outside of the screen and have them run towards the player. 在我尝试创建的游戏中,我想在屏幕外生成木乃伊并使它们向玩家奔跑​​。 The problem I'm getting is that it won't take one variable for both the x and y coordinates. 我遇到的问题是,x和y坐标都不会采用一个变量。 How can I make it so I can use just one variable for both the x and y coordinates? 如何做到这一点,所以我只能在x和y坐标中使用一个变量?

screenx = 800
screeny = 600

class enemy():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alive = False
        self.vel = 2

randomspawnabove = (random.randint(0, screenx), -100)
randomspawnbelow = (random.randint(0, screenx), (screeny + 100))
randomspawnleft = (-100, random.randint(0, screeny))
randomspawnright = ((screenx + 100), random.randint(0, screeny))

mummy_Spawn = [randomspawnleft, randomspawnright, randomspawnabove, randomspawnbelow]
mummy = enemy(random.choice(mummy_Spawn), 134, 134)

您可以使用*运算符解压缩坐标元组:

mummy = enemy(*random.choice(mummy_Spawn), 134, 134)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM