0

I'm making a game with Pygame, and now I have two problems:

  1. don't know how to make for objects looping back to the right edge when hitting the left edge
  2. I don't understand how to write collides between objects correctly(now its work incorrect)

Here is my code: It's a simple Scrolling Game where a player tries to avoid "harm" and collect "benefit". The Player stands at the left side and can move up and down, "harm" and "benefit" objects move from the right to the left

here I think the problem with loop back to the right edge

if len(enemies) == 0:
    wave_length += 1
    for i in range(wave_length):
        enemy = Enemy(random.randrange(500, 700), random.randrange(0, HEIGHT-100), random.choice(["1","2","3"]))
        enemies.append(enemy)


if len(fruits) == 0:
    wave_length += 1
    for i in range(wave_length):
        food = Food(random.randrange(500, 700), random.randrange(100, HEIGHT-100), random.choice(["one","two","three"]))
        fruits.append(food)

here is problems with crushing objects

for enemy in enemies[:]:
    enemy.move(enemy_val)
    if enemy.x + enemy.get_width() == player.x + player.get_width():
        lives -=1
        enemies.remove(enemy)

for food in fruits[:]:
    food.move(food_val)
    if food.x + food.get_width() == player.x + player.get_width():
        score += 1
        fruits.remove(food)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Nikolas
  • 77
  • 9
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. – Prune Dec 03 '20 at 04:35
  • 1
    "its work incorrect" is not a problem specification. Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. I doubt that anyone will push through 180 lines of under-documented code to figure out your program structure and figure out what you need. "Make it easy for others to help you," as the posting guidelines tell you. – Prune Dec 03 '20 at 04:37
  • @Prune Thanks for the answer, I tried to make code smaller for easier understanding – Nikolas Dec 03 '20 at 04:51

1 Answers1

0

If I understand your problem correctly, I would suggest doing a check in the move() function. We could do something like this:

def move(self, vel)
{
    self.x -= vel
    if self.x <= 0:
    {
        self.x = WIDTH
    }
}

Or self.x >= WIDTH that sets self.x = 0. Depending on which way you want to move the objects. You can get more in-depth with it if you want it to hit something specific or if you want it to account for the image's size, but this should just be a basic thing to try. At least this would be how I'd do it.

EDIT: You took some of the code away from the original post so I'm gonna assume it is still the same.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174