I am trying to create a basic “top-down” game using Pygame and Python.
There are a lot of copy/paste codes out there but what I wanted to create was a version that used “left mouse clicks” to move the player rather than the arrow keys or WASD.
Here is my code:
import pygame
import sys
import os
# Initialize Pygame
pygame.init()
# Constants
DESIRED_WIDTH, DESIRED_HEIGHT = 800, 600
PLAYER_SIZE = 50
ANIMATION_SPEED = 5
# Colors
RED = (255, 0, 0)
# Create the screen with the desired resolution
screen = pygame.display.set_mode((DESIRED_WIDTH, DESIRED_HEIGHT))
pygame.display.set_caption("Top-Down Mouse Click Movement")
# Create a back buffer surface
back_buffer = pygame.Surface((DESIRED_WIDTH, DESIRED_HEIGHT))
# Clock to control the frame rate
clock = pygame.time.Clock()
# Player properties
player_pos = pygame.Vector2(DESIRED_WIDTH // 2, DESIRED_HEIGHT // 2)
# Load background image
background_image_path = os.path.join("graphics", "ground.png")
background_image = pygame.image.load(background_image_path).convert()
# Variable to store the target position for background movement
background_target_pos = pygame.Vector2(DESIRED_WIDTH // 2, DESIRED_HEIGHT // 2)
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# Set the target position for background movement to the clicked position
background_target_pos = pygame.Vector2(pygame.mouse.get_pos())
# Calculate the direction from current background position to target position
background_direction = background_target_pos - player_pos
background_distance_to_target = background_direction.length()
if background_distance_to_target > 0:
# Normalize the direction vector
background_direction.normalize_ip()
# Calculate the movement vector
background_movement_vector = background_direction * min(background_distance_to_target, ANIMATION_SPEED)
new_player_pos = player_pos + background_movement_vector
# Update the player position
player_pos = new_player_pos
# Clear the back buffer
back_buffer.fill((0, 0, 0))
# Calculate the position for the camera to keep the player visibly centered
camera_x = max(0, min(player_pos.x - DESIRED_WIDTH // 2, background_image.get_width() - DESIRED_WIDTH))
camera_y = max(0, min(player_pos.y - DESIRED_HEIGHT // 2, background_image.get_height() - DESIRED_HEIGHT))
# Draw background image centered around the player on the back buffer
back_buffer.blit(background_image, (0 - camera_x, 0 - camera_y))
# Draw the player (red rectangle) at the center of the screen
pygame.draw.rect(back_buffer, RED, (DESIRED_WIDTH // 2 - PLAYER_SIZE // 2, DESIRED_HEIGHT // 2 - PLAYER_SIZE // 2, PLAYER_SIZE, PLAYER_SIZE))
# Copy the back buffer to the screen
screen.blit(back_buffer, (0, 0))
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
From what I understand, in order to keep the player centered, when the user clicks a position we don’t actually move the player to that position but instead we would move the background from it’s current position to the center of the screen?
For reference I am trying to recreate a game like Synthetic Reality Warpath.