I'm a begginer and I some stuff about Pygame so I decided t otry doing sothing by myself to practice my skills. I already know the colliderect but I don't know how exactly do you check. I'm making a copy of Adventure. I might sound so confucing. Sorry for that. Here is the code that i have right now:
import pygame
import sys
from pygame.locals import *
clock = pygame.time.Clock()
pygame.init()
pygame.display.set_caption("Neverout")
player = pygame.image.load("player.png")
green = (164, 164, 164)
WINDOW_SIZE = (1200, 700)
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
wall_rect = pygame.Rect(0, 0, 1200, 50)
wall_rect2 = pygame.Rect(0, 0, 50, 1200)
wall_rect3 = pygame.Rect(50, 650, 450, 50)
wall_rect4 = pygame.Rect(1150, 0, 50, 700)
wall_rect5 = pygame.Rect(700, 650, 750, 50)
player_pos = [385, 385]
up = False
down = False
right = False
left = False
while True:
screen.blit(player, player_pos)
if left is True:
player_pos[0] -= 8
if right is True:
player_pos[0] += 8
if up is True:
player_pos[1] -= 8
if down is True:
player_pos[1] += 8
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == pygame.K_LEFT:
left = True
elif event.key == pygame.K_RIGHT:
right = True
elif event.key == pygame.K_UP:
up = True
elif event.key == pygame.K_DOWN:
down = True
elif event.type == KEYUP:
if event.key == pygame.K_LEFT:
left = False
elif event.key == pygame.K_RIGHT:
right = False
elif event.key == pygame.K_UP:
up = False
elif event.key == pygame.K_DOWN:
down = False
elif event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
screen.fill(green)
pygame.draw.rect(screen, (230, 218, 0), wall_rect)
pygame.draw.rect(screen, (230, 218, 0), wall_rect2)
pygame.draw.rect(screen, (230, 218, 0), wall_rect3)
pygame.draw.rect(screen, (230, 218, 0), wall_rect4)
pygame.draw.rect(screen, (230, 218, 0), wall_rect5)
clock.tick(60)
Create a list of walls:
wall_list = [wall_rect, wall_rect2, wall_rect3, wall_rect4, wall_rect5]
Store the position of the player before you move the player:
prev_pos = player_pos[:]
Get the current rectangle (pygame.Rect
) after the player is moved:
plyer_rect = player.get_rect(topleft = player_pos)
Use colliderect()
to evaluate if the plyer_rect
intersects any
rectangle of wall_list
. If the player hits a wall then undo the movement:
if any(plyer_rect.colliderect(w) for w in wall_list):
player_pos = prev_pos
Furthermore I recommend to use pygame.key.get_pressed()
rather the the key events for the movement of the player:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_pos[0] -= 8
if keys[pygame.K_RIGHT]:
player_pos[0] += 8
if keys[pygame.K_UP]:
player_pos[1] -= 8
if keys[pygame.K_DOWN]:
player_pos[1] += 8
Complete example:
import pygame
import sys
from pygame.locals import *
clock = pygame.time.Clock()
pygame.init()
pygame.display.set_caption("Neverout")
player = pygame.image.load("player.png")
green = (164, 164, 164)
WINDOW_SIZE = (1200, 700)
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
wall_rect = pygame.Rect(0, 0, 1200, 50)
wall_rect2 = pygame.Rect(0, 0, 50, 1200)
wall_rect3 = pygame.Rect(50, 650, 450, 50)
wall_rect4 = pygame.Rect(1150, 0, 50, 700)
wall_rect5 = pygame.Rect(700, 650, 750, 50)
wall_list = [wall_rect, wall_rect2, wall_rect3, wall_rect4, wall_rect5]
player_pos = [385, 385]
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
prev_pos = player_pos[:]
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_pos[0] -= 8
if keys[pygame.K_RIGHT]:
player_pos[0] += 8
if keys[pygame.K_UP]:
player_pos[1] -= 8
if keys[pygame.K_DOWN]:
player_pos[1] += 8
plyer_rect = player.get_rect(topleft = player_pos)
if any(plyer_rect.colliderect(w) for w in wall_list):
player_pos = prev_pos
pygame.display.update()
screen.fill(green)
screen.blit(player, player_pos)
for w in wall_list:
pygame.draw.rect(screen, (230, 218, 0), w)
clock.tick(60)