import gl
# Initialize the graphics library
gl.init()
width = gl.get_monitorwidth() # Get screen width
height = gl.get_monitorheight() # Get screen height
gl.initwindow("Sample animation", width, height, ["resizable"]) # Create a resizable window
gl.background(10, 10, 10, 255) # Set background color
gl.setfps(60) # Set frames per second
# Create a circle
circle = gl.createcircle()
circle.set_radius(50) # Set radius
circle.set_background(255, 0, 0, 255) # Red fill
circle.set_border(0, 0, 0, 255, 3) # Black border
x = 0 # Horizontal position
going_left = False # Direction flag
running = True # Game loop flag
# Main game loop
while running:
if gl.window_closed() or gl.key_pressed() == ord('q'): # Close window or press 'q'
running = False
# Reverse direction if hitting screen edges
if x >= width:
going_left = True
elif x <= 0:
going_left = False
# Move circle
x += -5 if going_left else 5
circle.set_pos(x, height // 2) # Set circle position
circle.draw() # Draw circle
gl.present() # Update display
gl.clear() # Clear for next frame
gl.cleanup() # Cleanup resources
Ping Pong Game
import gl
# Screen dimensions
WIDTH = 0
HEIGHT = 0
# Paddle and ball settings
PADDLE_HEIGHT = 140
PADDLE_SPEED = 600.0
BALL_RADIUS = 14
BALL_SPEED = 650.0
# Arrow key codes
KEY_UP = 1073741906
KEY_DOWN = 1073741905
# Initialize graphics
gl.init()
gl.initwindow("Ping Pong", WIDTH, HEIGHT, ["fullscreen_desktop"])
gl.background(0, 0, 0, 255)
gl.setfps(60)
# Get actual screen size
HEIGHT = gl.get_monitorheight()
WIDTH = gl.get_monitorwidth()
# Create paddles and ball
left = gl.createline()
right = gl.createline()
ball = gl.createcircle()
# Set paddle colors
left.set_color(255,0,0,255)
right.set_color(0,0,255,255)
# Set ball properties
ball.set_radius(BALL_RADIUS)
ball.set_background(255,255,255,255)
ball.set_border(255,255,255,255,0)
# Initial positions
left_x = 80
left_y = HEIGHT // 2
right_x = WIDTH - 80
right_y = HEIGHT // 2
# Ball initial position and velocity
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_vx = BALL_SPEED
ball_vy = BALL_SPEED * 0.5
running = True
# Function to draw a paddle
def draw_paddle(line, x, y):
y1 = int(y - PADDLE_HEIGHT / 2)
y2 = int(y + PADDLE_HEIGHT / 2)
line.set_points(int(x), y1, int(x), y2)
line.draw()
# Game loop
while running:
dt = gl.getdt() # Delta time for smooth movement
if gl.window_closed():
running = False
keys = gl.get_holdkeys() # Currently pressed keys
# Paddle movement controls
if ord('w') in keys: left_y -= PADDLE_SPEED * dt
if ord('s') in keys: left_y += PADDLE_SPEED * dt
if KEY_UP in keys: right_y -= PADDLE_SPEED * dt
if KEY_DOWN in keys: right_y += PADDLE_SPEED * dt
# Update ball position
ball_x += ball_vx * dt
ball_y += ball_vy * dt
gl.clear() # Clear screen
# Draw paddles
draw_paddle(left, left_x, left_y)
draw_paddle(right, right_x, right_y)
# Draw ball
ball.set_pos(int(ball_x), int(ball_y))
ball.draw()
gl.present() # Update display
gl.cleanup() # Cleanup resources
Rectangle Player Movement
import gl
# Initialize graphics
gl.init()
WIDTH, HEIGHT = gl.get_monitorwidth(), gl.get_monitorheight()
gl.initwindow("Moving game sample", WIDTH, HEIGHT, ["resizable"])
gl.background(30,30,30,255)
gl.setfps(60)
# Player rectangle properties
rect_width, rect_height = 40, 40
x = WIDTH // 2 # Horizontal position
y = HEIGHT # Vertical position
speed = 300 # Horizontal speed
vel_y = 0 # Vertical velocity
gravity = 900 # Gravity acceleration
jump_strength = -420 # Jump impulse
on_ground = True
running = True
while running:
dt = gl.getdt() # Time delta
if gl.window_closed() or gl.key_pressed() == ord('q'):
running = False
keys = gl.get_holdkeys()
# Horizontal movement
if ord('a') in keys: x -= speed * dt
if ord('d') in keys: x += speed * dt
# Screen boundaries
if x < 0: x = 0
if x > WIDTH - rect_width: x = WIDTH - rect_width
# Jump
if (ord('w') in keys) and on_ground:
vel_y = jump_strength
on_ground = False
# Gravity
if not on_ground:
vel_y += gravity * dt
y += vel_y * dt
# Floor collision
if y >= HEIGHT - rect_height:
y = HEIGHT - rect_height
vel_y = 0
on_ground = True
# Draw player rectangle
gl.clear()
rect = gl.createrect()
rect.set_size(rect_width, rect_height)
rect.set_pos(int(x), int(y))
rect.set_background(255,0,0,255)
rect.draw()
gl.present()
gl.cleanup()