Created
January 13, 2024 11:01
-
-
Save horstjens/894d94192690c63dc6861d4717c4deb2 to your computer and use it in GitHub Desktop.
pygame starter example: moving a circle, changing the colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example file showing a circle moving on screen | |
import pygame | |
# pygame setup | |
pygame.init() | |
screen = pygame.display.set_mode((1280, 720)) | |
clock = pygame.time.Clock() | |
running = True | |
dt = 0 | |
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2) | |
r,g,b = 255,0,0 | |
radius = 40 | |
while running: | |
# poll for events | |
# pygame.QUIT event means the user clicked X to close your window | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
# fill the screen with a color to wipe away anything from last frame | |
#screen.fill("purple") | |
pygame.draw.circle(screen, ((r,g,b)), player_pos, radius) | |
keys = pygame.key.get_pressed() | |
if keys[pygame.K_w]: | |
player_pos.y -= 300 * dt | |
if keys[pygame.K_s]: | |
player_pos.y += 300 * dt | |
if keys[pygame.K_a]: | |
player_pos.x -= 300 * dt | |
if keys[pygame.K_d]: | |
player_pos.x += 300 * dt | |
if keys[pygame.K_u]: | |
r += 1 | |
r = min(255,r) | |
if keys[pygame.K_j]: | |
r -= 1 | |
r = max(0, r) | |
if keys[pygame.K_i]: | |
g += 1 | |
g = min(255,g) | |
if keys[pygame.K_k]: | |
g -= 1 | |
g = max(0, g) | |
if keys[pygame.K_o]: | |
b += 1 | |
b = min(255,b) | |
if keys[pygame.K_l]: | |
b -= 1 | |
b = max(0, b) | |
if keys[pygame.K_n]: | |
radius -= 10 * dt | |
radius = max(0, radius) | |
if keys[pygame.K_m]: | |
radius += 10 * dt | |
radius = min(500, radius) | |
# flip() the display to put your work on screen | |
pygame.display.flip() | |
pygame.display.set_caption(f"red {r} green {g} blue {b} radius {radius:.2f}") | |
# limits FPS to 60 | |
# dt is delta time in seconds since last frame, used for framerate- | |
# independent physics. | |
dt = clock.tick(60) / 1000 | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment