Python &引用;Player2";无法做与“相同的事情”;“玩家”; 随机导入 宽度=800 高度=500 背景=演员(“背景”) player=演员(‘player’) 敌人=演员(“敌人”) 金钱=演员(“金钱”,位置=(300300)) player2=演员(“外星人”) 分数=0 分数2=0 player.x=200 游戏者y=200 def draw(): screen.clear() background.draw() player.draw() 敌人,抽签 钱 player2.draw() def update(): 全球得分 如果键盘右键: player.x=player.x+4 如果键盘左键: player.x=player.x-4 如果键盘关闭: player.y=player.y+4 如果键盘打开: player.y=player.y-4 如果player.x>宽度: player.x=宽度 如果player.x宽度: player2.x=宽度 如果player2.x
我只是想在我的python游戏中引入一个player2,我正在用MU编辑器制作这个游戏,我可以看到player2并移动他,但是有一些问题。敌人没有追玩家2,玩家2也拿不到钱。我还想为player2添加一个score2变量,并在player2与金钱发生冲突时存储分数。要获取金钱,必须在Python &引用;Player2";无法做与“相同的事情”;“玩家”; 随机导入 宽度=800 高度=500 背景=演员(“背景”) player=演员(‘player’) 敌人=演员(“敌人”) 金钱=演员(“金钱”,位置=(300300)) player2=演员(“外星人”) 分数=0 分数2=0 player.x=200 游戏者y=200 def draw(): screen.clear() background.draw() player.draw() 敌人,抽签 钱 player2.draw() def update(): 全球得分 如果键盘右键: player.x=player.x+4 如果键盘左键: player.x=player.x-4 如果键盘关闭: player.y=player.y+4 如果键盘打开: player.y=player.y-4 如果player.x>宽度: player.x=宽度 如果player.x宽度: player2.x=宽度 如果player2.x,python,pygame,game-development,mu,Python,Pygame,Game Development,Mu,我只是想在我的python游戏中引入一个player2,我正在用MU编辑器制作这个游戏,我可以看到player2并移动他,但是有一些问题。敌人没有追玩家2,玩家2也拿不到钱。我还想为player2添加一个score2变量,并在player2与金钱发生冲突时存储分数。要获取金钱,必须在player2和金钱之间添加冲突测试: score2=0 def update(): 全球得分,2分 # [...] 如果money.CollizeRect(播放器2): money.x=random.randint
player2
和金钱之间添加冲突测试:
score2=0
def update():
全球得分,2分
# [...]
如果money.CollizeRect(播放器2):
money.x=random.randint(0,宽度)
money.y=random.randint(0,高度)
分数2+=1
要追逐玩家
或玩家2
你必须找到离敌人更近的玩家。比较敌人与玩家之间的距离,并追逐较近的玩家:
def update():
dx1,dy1=player.x-敌人.x,player.y-敌人.y
dx2,dy2=player2.x-敌人.x,player2.y-敌人.y
dist1sq=dx1*dx1+dy1*dy1
dist2sq=dx2*dx2+dy2*dy2
player_near=如果dist1sq玩家_靠近.x:
敌人x-=1
如果敌方.y玩家_靠近.y:
敌人。y-=1
如果玩家.collide rect(敌人):
退出()
如果玩家2.直接碰撞(敌人):
退出()
# [...]
功能更新:
def update():
全球得分,2分
如果键盘右键:
player.x+=4
如果键盘左键:
player.x-=4
如果键盘关闭:
player.y+=4
如果键盘打开:
player.y-=4
如果player.x>宽度:
player.x=宽度
如果player.x<0:
player.x=0
如果player.y<0:
player.y=0
如果player.y>高度:
player.y=高度
如果是键盘d:
player2.x+=4
如果是键盘。a:
player2.x-=4
如果键盘.s:
player2.y+=4
如果键盘.w:
player2.y-=4
如果player2.x>宽度:
player2.x=宽度
如果player2.x<0:
player2.x=0
如果player2.y<0:
player2.y=0
如果player2.y>高度:
player2.y=高度
dx1,dy1=player.x-敌人.x,player.y-敌人.y
dx2,dy2=player2.x-敌人.x,player2.y-敌人.y
dist1sq=dx1*dx1+dy1*dy1
dist2sq=dx2*dx2+dy2*dy2
player_near=如果dist1sq玩家_靠近.x:
敌人x-=1
如果敌方.y玩家_靠近.y:
敌人。y-=1
如果玩家.collide rect(敌人):
退出()
如果玩家2.直接碰撞(敌人):
退出()
如果是money.collide rect(玩家):
money.x=random.randint(0,宽度)
money.y=random.randint(0,高度)
分数=分数+1
如果money.CollizeRect(播放器2):
money.x=random.randint(0,宽度)
money.y=random.randint(0,高度)
分数2+=1
您需要更具体地回答您的问题,尝试标记我们正在查看的文件和您迄今为止尝试过的文件。这是一个python文件,我正在使用MU编辑器。我正在做一个有两个玩家和一个敌人的游戏。我希望敌人能够追逐玩家2,而不是只追逐玩家。我还想把分数和分数2加起来作为一个总数。我删除了if player.collide rect(player2):发现敌人和player2立即发生碰撞。我怎样才能让他们从不同的地方开始?我现在更新了帖子中的代码。@jacobstarheim不要更改问题中的代码!如果你改变密码,我的答案就没用了。我不知道为什么敌人
和玩家2
有相同的位置。您设置了player.x=200
player.y=200
。您必须为player2
import random
WIDTH = 800
HEIGHT = 500
background = Actor('background')
player = Actor('player')
enemy = Actor('enemy')
money = Actor('money', pos=(300, 300))
player2 = Actor('alien')
score = 0
score2 = 0
player.x = 200
player.y = 200
def draw():
screen.clear()
background.draw()
player.draw()
enemy.draw()
money.draw()
player2.draw()
def update():
global score
if keyboard.right:
player.x = player.x + 4
if keyboard.left:
player.x = player.x - 4
if keyboard.down:
player.y = player.y + 4
if keyboard.up:
player.y = player.y - 4
if player.x > WIDTH:
player.x = WIDTH
if player.x < 0:
player.x = 0
if player.y < 0:
player.y = 0
if player.y > HEIGHT:
player.y = HEIGHT
if enemy.x < player.x:
enemy.x = enemy.x + 1
if enemy.x > player.x:
enemy.x = enemy.x - 1
if enemy.y < player.y:
enemy.y = enemy.y + 1
if enemy.y > player.y:
enemy.y = enemy.y - 1
if player.colliderect(enemy):
exit()
if money.colliderect(player):
money.x = random.randint(0, WIDTH)
money.y = random.randint(0, HEIGHT)
score = score + 1
print ('Score:', score)
if keyboard.d:
player2.x = player2.x + 4
if keyboard.a:
player2.x = player2.x - 4
if keyboard.s:
player2.y = player2.y + 4
if keyboard.w:
player2.y = player2.y - 4
if player.colliderect(player2):
exit()
if player2.x > WIDTH:
player2.x = WIDTH
if player2.x < 0:
player2.x = 0
if player2.y < 0:
player2.y = 0
if player2.y > HEIGHT:
player2.y = HEIGHT