Rotating right triangle

Joined
Jun 3, 2023
Messages
2
Reaction score
0

If anyone would like a coding challenge, I am curious about how to modify this so that the angle increases only by whole numbers (e.g. angle = 1, 2, 3, 4) when the user presses the right or left arrow keys. I think the solution may be to use vectors instead of trig functions, but I'm fairly new to coding and am still trying to get it working.
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
Here's an example of how you can modify the code in Python so that the angle increases or decreases by whole numbers when the user presses the right or left arrow keys:
Python:
import pygame
import math

# Initialize pygame
pygame.init()

# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Angle Modifier")

# Set up the initial position and angle
position = [width // 2, height // 2]
angle = 0

# Set the angle increment
angle_increment = 1

# 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:
            if event.key == pygame.K_RIGHT:
                # Increase the angle by angle_increment
                angle += angle_increment
            elif event.key == pygame.K_LEFT:
                # Decrease the angle by angle_increment
                angle -= angle_increment

    # Clear the screen
    screen.fill((255, 255, 255))

    # Calculate the new position based on the angle
    angle_radians = math.radians(angle)
    velocity = [math.cos(angle_radians), math.sin(angle_radians)]
    position[0] += velocity[0]
    position[1] += velocity[1]

    # Draw the line from the previous position to the new position
    pygame.draw.line(screen, (0, 0, 0), (width // 2, height // 2), position, 2)

    # Update the display
    pygame.display.flip()

# Quit the game
pygame.quit()
In this modified code, we use the Pygame library to create a window and handle user input. The angle increment is set to 1, and when the user presses the right arrow key, the angle increases by angle_increment, and when the user presses the left arrow key, the angle decreases by angle_increment. The new position is calculated using vectors, and a line is drawn from the previous position to the new position.
When you run the code, a window will appear, and you can press the right and left arrow keys to change the angle by whole numbers. The line will be drawn accordingly.
 
Joined
Jun 3, 2023
Messages
2
Reaction score
0
I think the solution may be to limit the framerate and set line_speed = 1

pygame.time.Clock().tick(10)
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
To limit the framerate and set the line speed to 1, you can modify the code as follows:

Python:
import pygame
import math

# Initialize pygame
pygame.init()

# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Angle Modifier")

# Set up the initial position and angle
position = [width // 2, height // 2]
angle = 0

# Set the angle increment
angle_increment = 1

# Set the line speed
line_speed = 1

# Create a clock object to control the framerate
clock = pygame.time.Clock()

# 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:
            if event.key == pygame.K_RIGHT:
                # Increase the angle by angle_increment
                angle += angle_increment
            elif event.key == pygame.K_LEFT:
                # Decrease the angle by angle_increment
                angle -= angle_increment

    # Clear the screen
    screen.fill((255, 255, 255))

    # Calculate the new position based on the angle
    angle_radians = math.radians(angle)
    velocity = [math.cos(angle_radians) * line_speed, math.sin(angle_radians) * line_speed]
    position[0] += velocity[0]
    position[1] += velocity[1]

    # Draw the line from the previous position to the new position
    pygame.draw.line(screen, (0, 0, 0), (width // 2, height // 2), position, 2)

    # Update the display
    pygame.display.flip()

    # Limit the framerate
    clock.tick(30)  # Set the desired framerate (e.g., 30 FPS)

# Quit the game
pygame.quit()

In this modified code, I added a clock object from Pygame to control the framerate. You can set the desired framerate by calling clock.tick() with the desired frames per second (FPS) value. In the example, I set it to 30 FPS. This will limit the execution speed of the game loop to the specified framerate.

I also added a line_speed variable, which determines the speed at which the line moves. You can adjust this value to control the speed of the line movement.

By incorporating these changes, the line will move at a constant speed of 1 pixel per frame, and the framerate will be limited to the specified value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top