0

I have this code that creates a heart shape inside a circular button.

import pygame
import math

# Initialize pygame
pygame.init()

# Set the dimensions of the screen
WINDOW_WIDTH = 1300
WINDOW_HEIGHT = 800

# Set the colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Set the font
font = pygame.font.SysFont('Arial', 20)

# Set the button properties
BUTTON_RADIUS = 70
BUTTON_X_SPACING = 400
BUTTON_Y_SPACING = 200

# Initialize the screen
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

# Set the caption
pygame.display.set_caption("Marmoset Buttons")

# Set the clock
clock = pygame.time.Clock()

# Set the loop variable
running = True

# Set the timer
timer = 0

# Create the heart points for each button
button_heart_points1 = []
button_heart_points2 = []
button_heart_points3 = []
num_points = 100  # Increase this value for a smoother heart shape
radius = BUTTON_RADIUS

for i in range(num_points):
    angle = 2 * math.pi * i / num_points
    x1 = BUTTON_X_SPACING - int(radius * math.sin(angle) ** 3)
    y1 = BUTTON_Y_SPACING - int(radius * (13 * math.cos(angle) - 5 * math.cos(2 * angle) - 2 * math.cos(3 * angle) - math.cos(4 * angle)) / 16)
    x2 = BUTTON_X_SPACING + int(radius * math.sin(angle) ** 3)
    y2 = BUTTON_Y_SPACING + 200 - int(radius * (13 * math.cos(angle) - 5 * math.cos(2 * angle) - 2 * math.cos(3 * angle) - math.cos(4 * angle)) / 16)
    x3 = BUTTON_X_SPACING - int(radius * math.sin(angle) ** 3)
    y3 = BUTTON_Y_SPACING + 400 - int(radius * (13 * math.cos(angle) - 5 * math.cos(2 * angle) - 2 * math.cos(3 * angle) - math.cos(4 * angle)) / 16)
    button_heart_points1.append((x1, y1))
    button_heart_points2.append((x2, y2))
    button_heart_points3.append((x3, y3))

# Draw the buttons on the screen
def draw_buttons():
    pygame.draw.circle(screen, WHITE, (BUTTON_X_SPACING, BUTTON_Y_SPACING), BUTTON_RADIUS)
    pygame.draw.polygon(screen, RED, button_heart_points1)
    pygame.draw.circle(screen, WHITE, (BUTTON_X_SPACING, BUTTON_Y_SPACING + 200), BUTTON_RADIUS)
    pygame.draw.polygon(screen, RED, button_heart_points2[::-1])
    pygame.draw.circle(screen, WHITE, (BUTTON_X_SPACING, BUTTON_Y_SPACING + 400), BUTTON_RADIUS)
    pygame.draw.polygon(screen, RED, button_heart_points3[::-1])

# Main game loop
while running:
    # Set the frame rate
    clock.tick(60)

    # Check for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            distance_button1 = pygame.math.Vector2((BUTTON_X_SPACING, BUTTON_Y_SPACING)) - pygame.math.Vector2(pygame.mouse.get_pos())
            distance_button2 = pygame.math.Vector2((BUTTON_X_SPACING, BUTTON_Y_SPACING + 200)) - pygame.math.Vector2(pygame.mouse.get_pos())
            distance_button3 = pygame.math.Vector2((BUTTON_X_SPACING, BUTTON_Y_SPACING + 400)) - pygame.math.Vector2(pygame.mouse.get_pos())
            if distance_button1.length() < BUTTON_RADIUS:
                timer = pygame.time.get_ticks()
            elif distance_button2.length() < BUTTON_RADIUS:
                timer = pygame.time.get_ticks()
            elif distance_button3.length() < BUTTON_RADIUS:
                timer = pygame.time.get_ticks()

    # Fill the screen with black
    screen.fill(BLACK)

    # Draw the buttons on the screen
    draw_buttons()

    # Check the timer
    if timer != 0:
        current_time = pygame.time.get_ticks()
        if current_time - timer < 3000:
            # Do something when the button is pressed
            pass
        else:
            timer = 0

    # Update the screen
    pygame.display.update()

# Quit pygame

I want to change this so that the heart is oriented 90 degrees. It would be facing sideways with the pointy side facing towards the left of the screen. I can't seem to change much without destroying the heart shape or where it is. Is there any built in library or easier way I can create the shape perhaps? I'm open to any suggestion that would make this easier.

432111
  • 11
  • 2
  • You can apply a [rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix) to every point. Note that it rotates around `0, 0`. I don't know about any built-in functions/libraries, but if you need to operate with 'hearts', I would just refactor the code and define something like `draw_heart(x, y, angle)`. – evlogii Jul 16 '23 at 05:56
  • Draw your heart on a separate surface and then rotate that. See [this answer](https://stackoverflow.com/a/54714144/2280890) for all you need to know about rotation. – import random Jul 16 '23 at 06:39

0 Answers0