Skip to content

Instantly share code, notes, and snippets.

@vhxs
Created August 12, 2023 19:12
Show Gist options
  • Save vhxs/89b77e254deee2152ff24c5abfee6ffd to your computer and use it in GitHub Desktop.
Save vhxs/89b77e254deee2152ff24c5abfee6ffd to your computer and use it in GitHub Desktop.
ChatGPT-generated code to animate a coupla triangles
import pygame
# Initialize Pygame
pygame.init()
# Set up display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Glide Transformation Animation")
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
# Triangle vertices
triangle = [(100, 300), (200, 100), (300, 300)]
triangle_color = blue
# Initial positions of the triangles
x_pos1 = triangle[0][0]
y_pos1 = triangle[0][1]
x_pos2 = 500
y_pos2 = 300
# Animation parameters
speed = 2
# Main loop
running = True
move_first_triangle = True # Flag to indicate which triangle to move
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update triangle positions
if move_first_triangle:
x_pos1 += speed
if x_pos1 > width:
x_pos1 = -100 # Reset triangle position when it goes off-screen
else:
x_pos2 += speed
if x_pos2 > width:
x_pos2 = -100
# Clear the screen
screen.fill(white)
# Draw the triangles
transformed_triangle1 = [
(x + x_pos1, y) for x, y in triangle
] # Apply glide transformation to the first triangle
transformed_triangle2 = [
(x + x_pos2, y) for x, y in triangle
] # Apply glide transformation to the second triangle
pygame.draw.polygon(screen, triangle_color, transformed_triangle1)
pygame.draw.polygon(screen, red, transformed_triangle2)
# Update the display
pygame.display.flip()
# Control frame rate
pygame.time.Clock().tick(60)
# Switch the moving triangle
if x_pos1 > width and x_pos2 > -100:
move_first_triangle = False
if x_pos2 > width and x_pos1 > -100:
move_first_triangle = True
# Quit Pygame
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment