-1

Image showing rectangles used for collisions.

I am making an arcade-style game which involves moving an on-screen sprite. Since the in-game walls are part of the background image I have set up rectangles in order to test when a sprite collides with a wall (to block it from moving through for example). However I would like to know if there is a way to make the rectangles invisible or not viewable by the player (but still using them for collisions). I have just used the pygame.draw.rect() function to create the rectangles:

zone1rect = pygame.draw.rect(SURF, (0,0,0), (200, 418, 52, 188), 1)

EDIT: would it be possible to create a surface under the main one to add these to? And if so would this still allow collision between the sprite (which is on a different surface)? I am not asking about aloha colours, so please do not assosciate this with another question. Also that question talks about partly transparent rectangles, not what I want to know.

  • 2
    Does it need to be drawn at all for collision detection? – M4rtini Jan 27 '16 at 14:06
  • Can you try : `zone1rect = pygame.draw.rect(SURF, (0,0,0), (200, 418, 52, 188), -2)`. my opinion: need value -2 if width meaning border. – dsgdfg Jan 27 '16 at 14:10
  • I'm pretty new at the whole collision business so at the moment, the only way I can get this to work without setting the walls as separate objects is by using rectangles. And the -2 idea didn't work :( – gruntapocalypse Jan 27 '16 at 14:13
  • How are you comparing the location of the two rectangles to determine if a collision have happened? – M4rtini Jan 27 '16 at 14:17
  • Oh ! @fafl is right. But additional : Scan cursor point and listen mouse click(so don't need any marked area) – dsgdfg Jan 27 '16 at 14:20
  • I've already seen that question and it's talking about alpha colours and I didn't really understand it much, sorry if it seemed like a duplicate :( – gruntapocalypse Jan 27 '16 at 14:23
  • And I'm using .colliderect() to check collisions. – gruntapocalypse Jan 27 '16 at 14:24
  • `.colliderect()` should be usable for rect objects not drawn to the screen also. If you really wanna draw it though, you could draw those rects before the background\player so it would end up "beneath" everything else. – M4rtini Jan 27 '16 at 14:26
  • Will try that then :) Thankyou – gruntapocalypse Jan 27 '16 at 14:27
  • How would I create a separate surface just for adding the rectangles behind the main surface? Not sure it is working right :/ Have used SURF=pygame.display.set_mode((width,height),0,32) for the main surface. – gruntapocalypse Jan 27 '16 at 14:33
  • I'm not very familiar with pygame specifically, but usually stuff get drawn in the order they are called. So calling draw in the rectangles first would do it i guess – M4rtini Jan 27 '16 at 16:03
  • you have to use this rectangle [pygame.Rect()](http://www.pygame.org/docs/ref/rect.html) to check collision and not `pygame.draw.rect()` – furas Jan 27 '16 at 17:52

2 Answers2

2

You need pygame.Rect() which everybody use to keep player position, to check collision and to blit player on screen.

Player position and size

player_image = ...
player_rect = pygame.Rect(p_spritex, p_spritey, 30, 40)

check collision

player_rect.colliderect(enemy_rect)

draw player

SURF.blit(player_image, player_rect)

And you don't have to use draw.rect

prect = pygame.draw.rect(SURF, (0,0,0), (p_spritex, p_spritey, 30, 40), 1)

because it is useless

doc: pygame.Rect
tutorial: Program Arcade Games With Python And Pygame

furas
  • 134,197
  • 12
  • 106
  • 148
-1

Managed to get it to work in the end so thought I would post here to help anyone else with the same issue.

I set the rectangle variables before the "while True" line (which if you are unsure of what I mean it is the line that sets the code to run whilst the game window is actually open). Then I put the 'blit' section where all the images are set onto the surface, into a single function to run all the time. I made the rectangle variables global and simply called them in this function before the background image, which covered them:

def surface():
    global zone1rect
    global zone2rect
    global zone3rect
    global bl
    global prect
    zone1rect
    zone2rect
    zone3rect
    bl
    prect = pygame.draw.rect(SURF, (0,0,0), (p_spritex, p_spritey, 30, 40), 1)
    SURF.blit(background,(0,0))
    SURF.blit(p_sprite,(p_spritex,p_spritey))
surface() 

(^The surface function)

As you can see the player rectangle (surrounding the main sprite) is made global and 'created' in this function, simply because the coordinates change depending on the player's movements so I had to treat them a little differently.

Hope this helps anyone else with this issue, especially those who are unsure of alpha colours and such :)

  • you don't have to draw this rectangle if you don't use it . I thing answer for your problem is pygame.Rect which everybody use to check collission and blit player on screen. – furas Jan 27 '16 at 18:19
  • I was going to literally do something like sprite.get_rect() to simply get the rectangle info for the player but at the time it didn't seem to be working very well, but at least now this functions just as good :D – gruntapocalypse Jan 27 '16 at 18:30