2
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

I just tried to introduce a player2 to into my python game that I am making in MU-editor, I can see player2 and move him, but there are some problems. enemy is not chasing player2 and player2 can not pick up money. I also want to add a score2 variable for player2 and store the score when he collides with the money.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You need to be more specific in your question, try labeling what file we are looking at and what you have tried so far. – Nick Juelich Jun 09 '20 at 17:23
  • It is a python file and I am using MU editor. I am making a game with two players and an enemy. I want the enemy to be able to chase player2, instead of only chasing player. I also want to add the score and score2 together as a total sum. – jacob starheim Jun 09 '20 at 18:10

1 Answers1

2

To pickup the money, you have to add the collision test between playyer2 and money:

score2 = 0

def update():
    global score, score2

    # [...]

    if money.colliderect(player2):
        money.x = random.randint(0, WIDTH)
        money.y = random.randint(0, HEIGHT)
        score2 += 1

For chasing player or player2 you have to find the player which is nearer to the enemy. Compare the square of the Euclidean distance from the enemy to the players and chase the player which is nearer:

def update():

    dx1, dy1 = player.x - enemy.x, player.y - enemy.y
    dx2, dy2 = player2.x - enemy.x, player2.y - enemy.y
    dist1sq = dx1*dx1 + dy1*dy1
    dist2sq = dx2*dx2 + dy2*dy2 

    player_near = player if dist1sq < dist2sq else player2

    if enemy.x < player_near.x:
        enemy.x += 1
    if enemy.x > player_near.x:
        enemy.x -= 1
    if enemy.y < player_near.y:
        enemy.y += 1
    if enemy.y > player_near.y:
        enemy.y -= 1
    if player.colliderect(enemy):
        exit()
    if player2.colliderect(enemy):
        exit()

    # [...]

function update:

def update():
    global score, score2

    if keyboard.right:
        player.x += 4
    if keyboard.left:
        player.x -= 4
    if keyboard.down:
        player.y += 4
    if keyboard.up:
        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 keyboard.d:
        player2.x += 4
    if keyboard.a:
        player2.x -= 4
    if keyboard.s:
        player2.y += 4
    if keyboard.w:
        player2.y -= 4

    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

    dx1, dy1 = player.x - enemy.x, player.y - enemy.y
    dx2, dy2 = player2.x - enemy.x, player2.y - enemy.y
    dist1sq = dx1*dx1 + dy1*dy1
    dist2sq = dx2*dx2 + dy2*dy2 

    player_near = player if dist1sq < dist2sq else player2

    if enemy.x < player_near.x:
        enemy.x += 1
    if enemy.x > player_near.x:
        enemy.x -= 1
    if enemy.y < player_near.y:
        enemy.y += 1
    if enemy.y > player_near.y:
        enemy.y -= 1
    if player.colliderect(enemy):
        exit()
    if player2.colliderect(enemy):
        exit()

    if money.colliderect(player):
        money.x = random.randint(0, WIDTH)
        money.y = random.randint(0, HEIGHT)
        score = score + 1

    if money.colliderect(player2):
        money.x = random.randint(0, WIDTH)
        money.y = random.randint(0, HEIGHT)
        score2 += 1
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I removed if player.colliderect(player2): and found out that enemy and player2 are colliding instantly. How can I make it so they start in different locations? I updated the code in the post now btw. – jacob starheim Jun 09 '20 at 18:51
  • @jacobstarheim Do not change the code in the question! If you change the code, then my answer becomes useless. I don't know why `enemy` and `player2` have the same location. You set `player.x = 200` `player.y = 200`. You've to do something else for `player2` – Rabbid76 Jun 09 '20 at 18:55