Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotatingsquare.py #59

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions hooman/bouncingball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# demo_bouncing_ball.py
from hooman import Hooman
import pygame

# Initialize Hooman
window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)


bg_col = (255, 255, 255)

# Ball
ball_x, ball_y = window_width // 2, window_height // 2
ball_radius = 20
ball_speed_x, ball_speed_y = 5, 5
ball_color = hapi.color["blue"]

# Effects
size_change_rate = 0.1
speed_change_rate = 0.01

def handle_events(event):
if event.type == pygame.QUIT:
hapi.is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
hapi.is_running = False


hapi.handle_events = handle_events


while hapi.is_running:

hapi.background(bg_col)


ball_x += ball_speed_x
ball_y += ball_speed_y


if ball_x - ball_radius <= 0 or ball_x + ball_radius >= window_width:
ball_speed_x = -ball_speed_x
ball_color = hapi.color["red"]

if ball_y - ball_radius <= 0 or ball_y + ball_radius >= window_height:
ball_speed_y = -ball_speed_y
ball_color = hapi.color["green"]


ball_radius += size_change_rate
if ball_radius <= 5 or ball_radius >= 30:
size_change_rate = -size_change_rate


ball_speed_x += speed_change_rate
ball_speed_y += speed_change_rate
if abs(ball_speed_x) >= 8 or abs(ball_speed_y) >= 8:
speed_change_rate = -speed_change_rate


hapi.fill(ball_color)
hapi.circle(ball_x, ball_y, int(ball_radius))


hapi.flip_display()
hapi.event_loop()

pygame.quit()
69 changes: 69 additions & 0 deletions hooman/rotatingsquare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from hooman import Hooman
import pygame
import math

# Initialize Hooman
window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)

# Square
square_size = 50
square_x, square_y = window_width // 2, window_height // 2
rotation_angle = 0
rotation_speed = 2
square_color = (128, 0, 128)

# Trail
trail_length = 10
trail_positions = []

# Set the desired frames per second (fps)
fps = 60
clock = pygame.time.Clock()

def handle_events(event):
if event.type == pygame.QUIT:
hapi.is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
hapi.is_running = False

hapi.handle_events = handle_events

while hapi.is_running:
hapi.background(bg_col)

# Update square position and rotation
rotation_angle += rotation_speed
if rotation_angle >= 360:
rotation_angle = 0

# Add current position to trail
trail_positions.append((square_x, square_y))
if len(trail_positions) > trail_length:
trail_positions.pop(0)

# Draw trail
for i, (x, y) in enumerate(trail_positions):
alpha = int(255 * (1 - i / trail_length))
hapi.fill(square_color[0], square_color[1], square_color[2])
hapi.set_alpha(alpha)
hapi.rect(x - square_size // 2, y - square_size // 2, square_size, square_size)
hapi.set_alpha(255)

# Draw rotating square
hapi.rotate(rotation_angle)
hapi.fill(square_color)
hapi.rect(square_x - square_size // 2, square_y - square_size // 2, square_size, square_size)
hapi.rotate(-rotation_angle)

# Update display and handle events
hapi.flip_display()
hapi.event_loop()

# Control frame rate
clock.tick(fps)

pygame.quit()