aboutsummaryrefslogtreecommitdiff
path: root/src/macpan.py
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-03-20 14:58:52 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-03-20 14:58:52 +0200
commitf0250205a8f69550632c686c6402b2d1026a08a2 (patch)
treec0e69aa861c5a25cd41fd5d5d02dcbfb0403310f /src/macpan.py
downloadMacpan-f0250205a8f69550632c686c6402b2d1026a08a2.tar.xz
Macpan-f0250205a8f69550632c686c6402b2d1026a08a2.zip
Added a very basic player movement for pacman using w-a-s-d
Diffstat (limited to 'src/macpan.py')
-rw-r--r--src/macpan.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/macpan.py b/src/macpan.py
new file mode 100644
index 0000000..7ab4711
--- /dev/null
+++ b/src/macpan.py
@@ -0,0 +1,77 @@
+import pygame
+
+
+# Initialize Pygame
+pygame.init()
+
+# Set the dimensions of the window
+width, height = 640, 480
+screen = pygame.display.set_mode((width, height))
+
+# Set the color of the circle
+color = (255, 255, 153) # Light yellow
+
+# Set the center position of the circle
+center = [320, 240] # Center of the window
+radius = 16
+
+# Set the circle's velocity
+dx = 0
+dy = 0
+
+# Set the speed of the circle's movement
+speed = 10
+
+
+fps = 30
+
+clock = pygame.time.Clock()
+
+# Checks collision with walls
+def check_collision(circle_center_x, circle_center_y, dx, dy):
+ # edges of the circle
+ upper_circle_point = circle_center_y + radius
+ lower_circle_point = circle_center_y - radius
+ right_circle_point = circle_center_x + radius
+ left_circle_point = circle_center_x - radius
+ return upper_circle_point + dy > height or lower_circle_point + dy < 0 or right_circle_point + dx > width or left_circle_point + dx < 0
+
+
+# Main game loop
+running = True
+while running:
+ # Handle events
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ running = False
+ elif event.type == pygame.KEYDOWN:
+ # Move the circle based on the pressed key
+ if event.key == pygame.K_w:
+ dy = -speed
+ dx = 0 # Necssarry to move only horizontal or vertical
+ elif event.key == pygame.K_s:
+ dy = speed
+ dx = 0 # Necssarry to move only horizontal or vertical
+ elif event.key == pygame.K_a:
+ dx = -speed
+ dy = 0 # Necssarry to move only horizontal or vertical
+ elif event.key == pygame.K_d:
+ dx = speed
+ dy = 0 # Necssarry to move only horizontal or vertical
+
+ # Update the circle's position
+ if not check_collision(center[0], center[1], dx, dy):
+ center[0] += dx
+ center[1] += dy
+
+ # Draw the filled circle on the screen
+ screen.fill((0, 0, 0)) # Clear the screen
+ pygame.draw.circle(screen, color, center, radius)
+
+ # Update the screen
+ pygame.display.flip()
+
+ clock.tick(fps)
+
+# Quit Pygame
+pygame.quit()