1

Trying to make collision detection as means to make sprites bounce off one another, but my wall sprites aren't showing up after coords (5, 5) I wasn't sure if maybe it had to do with fill and colorkey both being white, or the fact that pygame.Surface(x, y) is the same as the x, y for the rect.

Here's my wall class:

class Wall(pygame.sprite.Sprite):

def __init__(self, color, h, d, x, y):
    super().__init__()

    self.image = pygame.Surface([x, y])
    self.image.fill(WHITE)
    self.image.set_colorkey(WHITE)

    pygame.draw.rect(self.image, color, [h, d, x, y])

    self.rect = self.image.get_rect()

and here's my code for my call to create wall 3 an wall 4:

wall3 = Wall(BLACK, 0, 400, 700, 2)
wall_list.add(wall3)
all_sprite_list.add(wall3)

wall4 = Wall(BLACK, 700, 0, 2, 400)
wall_list.add(wall4)
all_sprite_list.add(wall4)
ajjanna12
  • 25
  • 3
  • 1
    You should provide a [Minimum, Complete and Verifiable Example](https://stackoverflow.com/help/mcve). This way it is easier for people to be able to help you. I would guess you have a problem with your coordinate system. You could try creating your image of the desired size and then moving it to the specified position, [e.g.](https://stackoverflow.com/a/48292064/7675174) – import random Jan 17 '18 at 06:15
  • and where do you blit() wall on screen ? – furas Jan 17 '18 at 09:20
  • your variables are missleading - you use `x,y` for width and height in `Surface([width, height])` and in `rect(..., [start_x,start_y, width, height])`. And you don't use `x,y` to set position in `Rect()` - `get_rect(x=start_x, y=start_y)` – furas Jan 17 '18 at 09:26
  • to create wall you would need only self.image = Surface([width,height])` and sel.image.fill(color)` and `self.rect = self.image.get_rect(x=x, y=y)`. You don't need `set_color()` and `draw.rect()` Besides your `[h,d,x,y]` seems incorrect and you draw outside Surface - second wall has width 2 but you draw in x=700, first wall has height 2 and you draw in y=400. Surface has own size and coordinates (started always in 0,0) and it doesnt use screen coordinates – furas Jan 17 '18 at 09:31

1 Answers1

0

As for me you have two problems

First: you use missleading names - variables x,y should be rather width, height but this later.

Second: you assume that surface uses the same coordinates as screen but it not true. It starts at (0,0) and ends in your (x,y) but you try to draw rect in position (h,d) which is outside surface.

So in line

pygame.draw.rect(self.image, color, [h, d, x, y])

you need (0,0) instead of (h,d)

pygame.draw.rect(self.image, color, [0, 0, x, y])

and you have to use (h,d) with Rect()

self.rect = self.image.get_rect()
self.rect.x = h
self.rect.y = d

Frankly, draw.rect() will use all surface so you could do the same using only fill()

def __init__(self, color, h, d, x, y):
    super().__init__()

    self.image = pygame.Surface([x, y])
    self.image.fill(color)

    self.rect = self.image.get_rect()
    self.rect.x = h
    self.rect.y = d

If you use better names for variables then you get

def __init__(self, color, x, y, width, height):
    super().__init__()

    self.image = pygame.Surface([width, height])
    self.image.fill(color)

    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
furas
  • 134,197
  • 12
  • 106
  • 148