1

I am a beginner in Python and Pygame and I have a problem with my game in pygame. I have Maze class and Player class but I can't do proper collisions. I reached the moment where I have no idea why my code isn't working.

Here is my code:

class Maze(pygame.sprite.Sprite):
    def __init__(self):
        self.W = 25
        self.H = 14
        self.maze = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
                     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                     1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]
    def draw(self,screen):
        block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
        bx = 0
        by = 0
        for i in range(0,self.W*self.H):
            if self.maze[ bx + (by*self.W) ] == 1:
                screen.blit(block_surf,( bx * 40 , by * 40))
            bx = bx + 1
            if bx > self.W-1:
                bx = 0 
                by = by + 1

    def collisions(self):
        bx = 0 
        by = 0 
        for i in range(0, self.W * self.H):
            if self.maze[ bx + (by * self.W) ] == 1:

                rect = pygame.Rect(bx * 40, by * 40, 40, 40)

                if rect.colliderect(Player.get_rect):
                    pass  # Collision!
                    return False

                bx = bx + 1
                if bx > self.W-1:
                    bx = 0 
                    by = by + 1
class Player(pygame.sprite.Sprite):
    def __init__(self): #sprite for a player
        pygame.sprite.Sprite.__init__(self) #for sprite working
        self.image = pygame.image.load(os.path.join(img_folder, "player.png")).convert() #look of the sprite
        self.image.set_colorkey(white) #to delete black things around rect img
        self.rect = self.image.get_rect() #kind of border around it
        self.rect.centerx = width/2 + 40
        self.rect.bottom = height- 120
        self.speedx = 0
        self.speedy = 0
    def update(self): #moving player
        maze = Maze()
        collisions = maze.collisions
        self.speedx = 0
        self.speedy = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            if collisions == False:
                self.speedx = 0
            else:
                self.speedx = -1
        elif keystate[pygame.K_RIGHT]:
            if self.rect.x > 920:
                self.speedx = 0
            # elif self.rect.y <= 636 and self.rect.y > 580: 
                # self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 482 and self.rect.y > 401:
                self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 399 and self.rect.y > 282:
                self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 279 and self.rect.y > 160:
                self.speedy = 0 
            elif self.rect.x == 40 and self.rect.y < 157 and self.rect.y > 41:
                self.speedy = 0
            else:
                self.speedx = 1
        elif keystate[pygame.K_UP]:
            if self.rect.y < 41:
                self.speedy = 0
            elif self.rect.y == 159 and self.rect.x > 46 and self.rect.x < 160:
                self.speedy = 0
            elif self.rect.y == 281 and self.rect.x > 45 and self.rect.x < 154:
                self.speedy = 0 
            else:
                self.speedy = -1
        elif keystate[pygame.K_DOWN]:
            if self.rect.y > 480:
                self.speedy = 0
            elif self.rect.x > 405 and self.rect.x < 634 and self.rect.y > 440:
                self.speedy = 0
            elif self.rect.y == 280 and self.rect.x > 46 and self.rect.x < 155:
                self.speedy = 0
            else:
                self.speedy = 1
        self.rect.x += self.speedx
        self.rect.y += self.speedy
    def get_rect(self):
        return pygame.Rect(self.rect.x, self.rect.y, 40, 40)

Can someone explain to me why it isn't working and help to improve it?

I tried many ideas - even writing from hand positions x and y to create collisions. But it's really bad practice to do so I turned to pygame option sprite.colliderect(sprite2)

I want to receive information why my code isn't working properly and tips for improving it.

furas
  • 134,197
  • 12
  • 106
  • 148
  • use `print()` to display values in variables in different moments. it can help to find problem – furas Apr 23 '19 at 16:51
  • Hello, do you have a main() function from where your code starts execution? – Juanpa Apr 23 '19 at 16:53
  • 1
    why do you return `False` when you find collision ? I should rather return `True` – furas Apr 23 '19 at 17:46
  • you could use nested `for` loops - `for bx in range(self.W): for by in range(self.H)` to make simpler code. Or even `for row in self.maze: for cell in row` to get cell directly - without bx,by – furas Apr 23 '19 at 17:48
  • you could load `block_surf` only once - in `__init__` – furas Apr 23 '19 at 17:54
  • I'm so sorry for late reply. I will put whole code into pastebin so maybe it'll be easier to help with the code. – Rosalie Black Apr 23 '19 at 18:46
  • https://pastebin.com/mYPg1GFe --> here is the whole code for the game. There is no error shown while compilating. – Rosalie Black Apr 23 '19 at 18:49
  • Dear Furas - I used print in many places - it seems like function get_rect in player class and function collisions in maze class don't print anything - I probably do something wrong there. It's nice idea with the nestled loop. I will definitely change it. Also I'll load block_surf only once. – Rosalie Black Apr 23 '19 at 18:50
  • Dear Juanpa - yes, it'd be more obvious if collision returned True. I'll change it – Rosalie Black Apr 23 '19 at 18:51
  • Try changing the function reference from `maze.collisions` to a function call `maze.collisions()` – Alexander Freyr Apr 28 '19 at 22:47

0 Answers0