Walls that block the movement of a player
wall_of_blocks_with_player.py
import pygamepygame.init()screen = pygame.display.set_mode((400, 300))done = Falseclock = pygame.time.Clock()x,y = 170,120walls=[]for horiz in range(0,400,25): walls.append(pygame.Rect(horiz,0,25,25)) #top wallfor horiz in range(0,400,25): walls.append(pygame.Rect(horiz,275,25,25)) #bottom wallfor vert in range(0,325,25): walls.append(pygame.Rect(0,vert,25,25)) #left wallfor vert in range(0,325,25): walls.append(pygame.Rect(375,vert,25,25)) #right wallwhile not done:for event in pygame.event.get():if event.type == pygame.QUIT:done = Truepressed = pygame.key.get_pressed()xprevious = xyprevious = yif pressed[pygame.K_UP]: y = y - 3if pressed[pygame.K_DOWN]: y = y + 3if pressed[pygame.K_LEFT]: x = x - 3if pressed[pygame.K_RIGHT]: x = x + 3me = pygame.Rect(x, y, 60, 60)if me.collidelistall(walls) != []: #no collisions returns an empty listx = xpreviousy = ypreviousme = 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()