Walls that block the movement of a player
wall_of_blocks_with_player.py
import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) done = False clock = pygame.time.Clock() x,y = 170,120 walls=[] for horiz in range(0,400,25): walls.append(pygame.Rect(horiz,0,25,25)) #top wall for horiz in range(0,400,25): walls.append(pygame.Rect(horiz,275,25,25)) #bottom wall for vert in range(0,325,25): walls.append(pygame.Rect(0,vert,25,25)) #left wall for vert in range(0,325,25): walls.append(pygame.Rect(375,vert,25,25)) #right wall while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pressed = pygame.key.get_pressed() xprevious = x yprevious = y if pressed[pygame.K_UP]: y = y - 3 if pressed[pygame.K_DOWN]: y = y + 3 if pressed[pygame.K_LEFT]: x = x - 3 if pressed[pygame.K_RIGHT]: x = x + 3 me = pygame.Rect(x, y, 60, 60) if me.collidelistall(walls) != []: #no collisions returns an empty list x = xprevious y = yprevious me = pygame.Rect(x, y, 60, 60) screen.fill((255, 255, 255)) pygame.draw.rect(screen, (0,0,0), me) for wall in walls: pygame.draw.rect(screen, (255,0,0), wall) pygame.display.flip() clock.tick(60) pygame.quit()