Spider
unknown
python
2 years ago
5.5 kB
10
Indexable
import math
import pygame
import pygame.font
# VARIABLES
width, height = 1000, 700
Out = False
active_pendulum = None # Track the active pendulum
acceleration = False
# COLORS
white = (255, 255, 255)
black = (0, 0, 0)
gray = (169, 169, 169)
# BEFORE START
pygame.init()
background = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
# Set mass values and damping factor
mass_pendulum1 = 2
mass_pendulum2 = 3
damped_factor = 0.01 # Damping factor for proportional damping
# Load the background image
background_image = pygame.image.load("pipe.png")
background_image = pygame.transform.scale(background_image, (width, height))
# Load the spider images
spider1_image = pygame.image.load("spider1.png")
spider2_image = pygame.image.load("spider2.png")
# Set up font for the timer and buttons
font = pygame.font.Font(None, 20)
button_font = pygame.font.Font(None, 20)
# Set initial timer values
timer_seconds = 0
paused = False
# Set button positions
pause_button_pos = (150, height - 30)
reset_button_pos = (250, height - 30)
class Pendulum(object):
def __init__(self, XY, radius, mass, image):
self.x = XY[0]
self.y = XY[1]
self.radius = radius
self.length = 0
self.angle = 0
self.vel = 0
self.Aacc = 0
self.falling = False
self.mass = mass
self.image = pygame.transform.scale(image, (2*radius, 2*radius))
def draw(self, bg):
rotated_image = pygame.transform.rotate(self.image, math.degrees(-self.angle))
rotated_rect = rotated_image.get_rect(center=(self.x, self.y))
bg.blit(rotated_image, rotated_rect.topleft)
pygame.draw.line(bg, black, (width//2, 50), (self.x, self.y), 2)
def angle_length(pendulum):
length = math.sqrt(math.pow(pendulum.x - width/2, 2) + math.pow(pendulum.y - 50, 2))
angle = math.asin((pendulum.x - width/2)/ length)
return angle, length
def get_path(pendulum):
pendulum.x = round(width/2 + pendulum.length * math.sin(pendulum.angle))
pendulum.y = round(50 + pendulum.length * math.cos(pendulum.angle))
def redraw(pendulum1, pendulum2):
# Blit the background image onto the screen
background.blit(background_image, (0, 0))
pendulum1.draw(background)
pendulum2.draw(background)
draw_timer(background)
draw_buttons(background)
pygame.display.update()
def draw_timer(bg):
timer_text = font.render(f'Time: {timer_seconds} s', True, black)
bg.blit(timer_text, (10, height - 30))
def draw_buttons(bg):
# Draw "P" for pause button
pause_button_text = button_font.render("'P' Fo Paused", True, black)
bg.blit(pause_button_text, pause_button_pos)
# Draw "R" for reset button
reset_button_text = button_font.render("'R' For Reset", True, black)
bg.blit(reset_button_text, reset_button_pos)
pendulum1 = Pendulum((int(width / 2) - 50, height - 100), 50, mass_pendulum1, spider1_image)
pendulum2 = Pendulum((int(width / 2) + 50, height - 100), 50, mass_pendulum2, spider2_image)
while not Out:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Out = True
if event.type == pygame.MOUSEBUTTONDOWN:
# Check if the click is inside any pendulum
if pendulum1.x - pendulum1.radius < event.pos[0] < pendulum1.x + pendulum1.radius \
and pendulum1.y - pendulum1.radius < event.pos[1] < pendulum1.y + pendulum1.radius:
active_pendulum = pendulum1
elif pendulum2.x - pendulum2.radius < event.pos[0] < pendulum2.x + pendulum2.radius \
and pendulum2.y - pendulum2.radius < event.pos[1] < pendulum2.y + pendulum2.radius:
active_pendulum = pendulum2
acceleration = True
if event.type == pygame.MOUSEBUTTONUP:
if active_pendulum is not None:
active_pendulum.falling = True
acceleration = False
active_pendulum = None # Reset the active pendulum when the mouse button is released
# Button actions for pausing and resetting the timer
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p: # Press 'p' to pause/unpause
paused = not paused
elif event.key == pygame.K_r: # Press 'r' to reset timer
timer_seconds = 0
# Update pendulum position based on mouse movement
if active_pendulum is not None:
active_pendulum.x, active_pendulum.y = pygame.mouse.get_pos()
active_pendulum.angle, active_pendulum.length = angle_length(active_pendulum)
get_path(active_pendulum)
# Update falling pendulums
if pendulum1.falling:
pendulum1.Aacc = -0.005 * math.sin(pendulum1.angle) / pendulum1.mass
pendulum1.vel += pendulum1.Aacc
pendulum1.vel += -damped_factor * pendulum1.vel # Proportional damping
pendulum1.angle += pendulum1.vel
get_path(pendulum1)
if pendulum2.falling:
pendulum2.Aacc = -0.005 * math.sin(pendulum2.angle) / pendulum2.mass
pendulum2.vel += pendulum2.Aacc
pendulum2.vel += -damped_factor * pendulum2.vel # Proportional damping
pendulum2.angle += pendulum2.vel
get_path(pendulum2)
redraw(pendulum1, pendulum2)
if not paused:
timer_seconds += 1
pygame.quit()
Editor is loading...
Leave a Comment