🎮 5-Day Python: Simple Navigation Challenge Practice moving your character around a screen and interacting with objects!
- Create
move_game.pyand set up a basic Pygame window (e.g., 600x400). - Create a red square
character_rect = pygame.Rect(250, 150, 50, 50)and aspeedvariable. - Inside your game loop, add code to move the square up using
pygame.K_UPand down usingpygame.K_DOWN(like in class). - Add simple boundary checks so the square doesn't go off the top or bottom of the screen:
- If
character_rect.y < 0, setcharacter_rect.y = 0. - If
character_rect.y > screen_height - character_rect.height, setcharacter_rect.y = screen_height - character_rect.height.
- If
- Run your game and make sure your red square moves up and down and stops at the edges.
- Continue using your
move_game.pyfrom Day 1. - Add code to move your red square left using
pygame.K_LEFTand right usingpygame.K_RIGHT. - Add simple boundary checks so the square doesn't go off the left or right sides of the screen:
- If
character_rect.x < 0, setcharacter_rect.x = 0. - If
character_rect.x > screen_width - character_rect.width, setcharacter_rect.x = screen_width - character_rect.width.
- If
- Run your game. Your square should now move in all four directions and stay on screen.
- In
move_game.py, create a second blue squareplayer2_rect(e.g., at(50, 50, 50, 50)). - Give this second square its own movement controls using
pygame.K_w(up),pygame.K_s(down),pygame.K_a(left), andpygame.K_d(right). - Add the same boundary checks for
player2_rectas you did forcharacter_recton Day 1 and Day 2. - (After core) Change the color of your first character to green.
- Run your game. Now two squares should move independently with different keys and stay on screen.
- Keep your two moving characters.
- Create a small, light gray
safe_zone_rect(e.g.,pygame.Rect(500, 150, 80, 80)) and draw it on the screen. - If
character_rect(your first square) moves into thesafe_zone_rectarea, print a message like "Player 1 is safe!" to your console. (Hint: Check ifcharacter_rect.centerxis betweensafe_zone_rect.leftandsafe_zone_rect.right, AND ifcharacter_rect.centeryis betweensafe_zone_rect.topandsafe_zone_rect.bottom). - (After core) Change the
safe_zone_rectcolor to bright green while Player 1 is inside it. Change it back to light gray when Player 1 leaves.
- Continue with your
move_game.py. - (Open) Add one small feature to make your game more fun or different. You could:
- Make the characters move faster or slower if you press the
SPACEkey. - Add a different color background to the game.
- Change the size of one of the characters (e.g., make it wider).
- Add a simple "Reset" button (e.g.,
Rkey) that moves both characters back to their starting positions.
- Make the characters move faster or slower if you press the
- Show off your unique addition!