skip to Main Content

I am currently working on creating a game using python 3.2.3 and pygame. I am creating a game that is very similar to Mario (a side scroller); my game involves a series of ‘bricks’ that the character will need to jump on and be able to collide with. I have created a level in photoshop and mask that is colour coded so that red is the area that the character will die, green is the colour for the bricks, and blue is the colour of the ground. Here is the code I am having a problem with:

    guy[y]+=guy[vy]     # add current speed to Y

col = mask.get_at((int(guy[x]+662), int(guy[y]+59)))
if col == (0,0,255,255):
    guy[y] = 537
    guy[vy] = 0
    guy[onground] = True
if col == (0,255,0,255):
    guy[vy] = 0
    guy[onground]=True
elif col == (255,0,0,255):
    guy[vy] = 5
    guy[onground] = False
if guy[y]+59 >= 720:
    lives -= 1
    screen.blit(guyPic,(640,guy[y]))
guy[vy]+=.75

When I try to make the character land on a brick (or on the green), he lands but he begins to sink. He can also walk right through bricks. I would like the character to be able to collide with the brick so that he cannot walk right through them, but he is able to jump and land on them without sinking through. I do know another method that is possible which is to draw rects at the coordinates of each brick but this method would be very inefficient as it would be too much code. I would greatly appreciated some help with this error as I have tried to fix it for the past two weeks. Any help is appreciated.

2

Answers


  1. Drawing rects is better. And it’s logically easier. You can just detect if the two rects collides, because pygame has already supported collide detection. You can see this:pygame.Rect.colliderect

    Login or Signup to reply.
  2. Make sure you put the ‘guy[vy]+=.75’ is at the start of the code. Before despite what the collisions set the y speed too, it was always being set back to .75 at the end of the code. therefore when you hit the ground you would still move through it at a speed of .75 y. Also try limiting the maximum fall speed by adding a conditional statement for the ‘guy[vy]+=.75’ so it only runs ‘if guy[y] <= 10:’
    Good Luck :).

        guy[y]+=guy[vy]     # add current speed to Y
    guy[vy]+=.75
    col = mask.get_at((int(guy[x]+662), int(guy[y]+59)))
    if col == (0,0,255,255):
        guy[y] = 537
        guy[vy] = 0
        guy[onground] = True
    if col == (0,255,0,255):
        guy[vy] = 0
        guy[onground]=True
    elif col == (255,0,0,255):
        guy[vy] = 5
        guy[onground] = False
    if guy[y]+59 >= 720:
        lives -= 1
        screen.blit(guyPic,(640,guy[y]))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search