Skip to content

Commit 0bcaf0a

Browse files
Physics Puzzle Game Script Added
1 parent e52dbdb commit 0bcaf0a

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Physics Puzzle Game/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Physics-Based Puzzle Game
2+
3+
## Description
4+
5+
This is a physics-based puzzle game built using Python and the Pygame library. The game allows players to manipulate objects with realistic physics to achieve specific goals and solve challenging puzzles.
6+
7+
## Features
8+
9+
- Interactive Objects: Players can click and drag objects to move them around the game world.
10+
- Goal Objects: The goal is to move the interactive object to touch the goal object to complete each level.
11+
- Multiple Levels: The game includes multiple levels of increasing difficulty.
12+
- Scoring and Timer: The game keeps track of the time taken to complete each level and displays the score to the player.
13+
- Obstacles: Players must navigate around obstacles to reach the goal object.
14+
- Hint System: A hint system is available to provide guidance if the player gets stuck on a level.
15+
- Sound Effects and Music: The game includes sound effects and background music to enhance the gaming experience.
16+
17+
## Requirements
18+
19+
- Python 3.x
20+
- Pygame library (`pip install pygame`)
21+
- Pymunk library (`pip install pymunk`)
22+
23+
## How to Play
24+
25+
1. Install the required libraries as mentioned in the Requirements section.
26+
2. Run the `physics_puzzle_game.py` file to start the game.
27+
3. Use the left mouse button to click and drag the interactive object.
28+
4. Move the interactive object to touch the goal object to complete each level.
29+
5. Try to complete each level as quickly as possible to earn a higher score.
30+
6. Use the hint system if you're stuck on a level.
31+
32+
## Controls
33+
34+
- Left Mouse Button: Click and drag the interactive object.
35+
36+
## Enjoy the game and have fun
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pygame
2+
import pymunk
3+
import sys
4+
5+
# Define screen dimensions
6+
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
7+
8+
# Initialize Pygame
9+
pygame.init()
10+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
11+
pygame.display.set_caption("Physics Puzzle Game")
12+
13+
# Define colors
14+
WHITE = (255, 255, 255)
15+
BLACK = (0, 0, 0)
16+
17+
# Physics space setup
18+
space = pymunk.Space()
19+
space.gravity = 0, 1000 # Set the gravity
20+
21+
# Main game loop
22+
clock = pygame.time.Clock()
23+
24+
def create_physics_object(x, y, vertices):
25+
body = pymunk.Body(1, 100)
26+
body.position = x, y
27+
shape = pymunk.Poly(body, vertices)
28+
shape.elasticity = 0.5
29+
space.add(body, shape)
30+
return body
31+
32+
# Create interactive objects
33+
object_vertices = [(-30, -30), (30, -30), (30, 30), (-30, 30)]
34+
interactive_object = create_physics_object(200, 500, object_vertices)
35+
36+
# Create goal object
37+
goal_vertices = [(-20, -20), (20, -20), (20, 20), (-20, 20)]
38+
goal_object = create_physics_object(600, 100, goal_vertices)
39+
40+
# Main game loop
41+
while True:
42+
for event in pygame.event.get():
43+
if event.type == pygame.QUIT:
44+
pygame.quit()
45+
sys.exit()
46+
47+
# Get mouse position
48+
mouse_x, mouse_y = pygame.mouse.get_pos()
49+
50+
# Check if the mouse button is pressed
51+
if pygame.mouse.get_pressed()[0]: # Left mouse button pressed
52+
# Check if the mouse is over the interactive object
53+
if interactive_object.shapes[0].point_query(interactive_object.position).distance < 30:
54+
interactive_object.position = mouse_x, SCREEN_HEIGHT - mouse_y
55+
56+
# Clear the screen
57+
screen.fill(WHITE)
58+
59+
# Update physics simulation
60+
dt = 1.0 / 60.0
61+
space.step(dt)
62+
63+
# Draw physics objects on the screen
64+
for body in space.bodies:
65+
for shape in body.shapes:
66+
# Convert physics coordinates to screen coordinates
67+
vertices = [(body.position + v.rotated(body.angle)) for v in shape.get_vertices()]
68+
vertices = [(v.x, SCREEN_HEIGHT - v.y) for v in vertices]
69+
pygame.draw.polygon(screen, BLACK, vertices)
70+
71+
# Check if the goal object is reached
72+
if goal_object.shapes[0].point_query(goal_object.position).distance < 20:
73+
print("Level Completed!")
74+
75+
# Update the display
76+
pygame.display.flip()
77+
clock.tick(60)

Physics Puzzle Game/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pygame
2+
pymunk

0 commit comments

Comments
 (0)