RaZiiiGG
RaZiiiGG

Reputation: 193

Pygame collision between rectangles

So I want to make a basic 2d game where you control a rectangle and if you touch a wall you die. I've created some walls Picture

enter image description here

but I am stuck now. I really don't know how does the code should look like even though I looked at the documentation. And second thing is that I am not sure how to create "Sprite". How do I make my rectangle as "Sprite" or it doesn't matter and it can stay just a normal rectangle that moves?

import pygame
pygame.init()

win = pygame.display.set_mode((1200, 600))

pygame.display.set_caption("My Game")

x = 40
y = 45
width = 30
height = 30
vel = 4
black = (0,0,0)

run = True
while run:
pygame.time.delay(15)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

win.fill((255,255,255))

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and x > 0:
    x -= vel
if keys[pygame.K_RIGHT] and x < 1200 - width:
    x += vel
if keys[pygame.K_UP] and y > 0:
    y -= vel
if keys[pygame.K_DOWN] and y < 600 - height:
    y += vel

pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

# Boundaries
pygame.draw.rect(win, (black), (0, 0, 1200, 20))
pygame.draw.rect(win, (black), (0, 0, 20, 600))
pygame.draw.rect(win, (black), (0, 580, 1200, 20))
pygame.draw.rect(win, (black), (1180, 0, 20, 600))

# Obstacle walls
pygame.draw.rect(win, (black), (300, 0, 20, 530))
pygame.draw.rect(win, (black), (20, 100, 230, 20))
pygame.draw.rect(win, (black), (70, 200, 230, 20))
pygame.draw.rect(win, (black), (20, 300, 230, 20))
pygame.draw.rect(win, (black), (70, 400, 230, 20))

# Middle Wall
pygame.draw.rect(win, (black), (600, 100, 20, 500))


pygame.display.update()

pygame.quit()

Upvotes: 1

Views: 1051

Answers (1)

skrx
skrx

Reputation: 20438

You don't need pygame sprites and sprite groups for the collision detection, you can just use pygame.Rects. I'd put all walls into a list and make them pygame.Rect objects, then it's possible to use the colliderect of the rects for the collisions. You also need a rect for the player (just replace the x, y, width, height variables with a rect). Iterate over the walls list with for loops to check if one collides with the player rect and also to draw them.

import pygame


pygame.init()
win = pygame.display.set_mode((1200, 600))
clock = pygame.time.Clock()  # A clock to limit the frame rate.
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)

# The player variables have been replaced by a pygame.Rect.
player = pygame.Rect(40, 45, 30, 30)
vel = 4
# The walls are now pygame.Rects as well. Just put them into a list.
walls = [
    pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
    pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
    pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
    pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
    pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500),
    ]

run = True
while run:
    # Handle the events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    # Update the player coordinates.
    if keys[pygame.K_LEFT] and player.x > 0:
        player.x -= vel
    if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
        player.x += vel
    if keys[pygame.K_UP] and player.y > 0:
        player.y -= vel
    if keys[pygame.K_DOWN] and player.y < 600 - player.height:
        player.y += vel

    # Game logic.
    for wall in walls:
        # Check if the player rect collides with a wall rect.
        if player.colliderect(wall):
            print('Game over')
            # Then quit or restart.

    # Draw everything.
    win.fill(WHITE)
    pygame.draw.rect(win, RED, player)
    # Use a for loop to draw the wall rects.
    for wall in walls:
        pygame.draw.rect(win, BLACK, wall)

    pygame.display.update()
    clock.tick(60)  # Limit the frame rate to 60 FPS.

pygame.quit()

Upvotes: 1

Related Questions