import pygame
# Initialize pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Move Triangle")
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set up the triangle's initial position
triangle_x = width // 2
triangle_y = height // 2
# Set up the triangle's movement speed
triangle_speed = 5
# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Check for key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_k]:
# Move the triangle to the right when 'K' is pressed
triangle_x += triangle_speed
# Update the screen
screen.fill(BLACK)
pygame.draw.polygon(screen, WHITE, [(triangle_x, triangle_y - 25), (triangle_x - 25, triangle_y + 25), (triangle_x + 25, triangle_y + 25)])
pygame.display.flip()
# Quit the game
pygame.quit()
In this code, we initialize pygame, set up the display, define colors, and set up the triangle's position and movement speed. Inside the game loop, we handle events and check for key presses. If the "K" key is pressed, we update the triangle's position. Finally, we update the screen by filling it with the background color, drawing the triangle, and flipping the display.
User
make as small as possible pygame code for an asteroid game only using import pygame
Assistant
Certainly! Here's a minimalistic version of an asteroid game using only import pygame:
python
import pygame
import random
# Initialize pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroid Game")
# Set up the player
player = pygame.Rect(width // 2, height // 2, 20, 20)
player_speed = 5
# Set up the asteroids
asteroids = []
asteroid_speed = 2
# Game loop
running = True
clock = pygame.time.Clock()
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.x -= player_speed
if keys[pygame.K_RIGHT]:
player.x += player_speed
if keys[pygame.K_UP]:
player.y -= player_speed
if keys[pygame.K_DOWN]:
player.y += player_speed
# Add new asteroids randomly
if random.randint(0, 100) < 5:
asteroid = pygame.Rect(random.randint(0, width), 0, 10, 10)
asteroids.append(asteroid)
# Move and remove asteroids
for asteroid in asteroids:
asteroid.y += asteroid_speed
if asteroid.y > height:
asteroids.remove(asteroid)
# Check for collisions
for asteroid in asteroids:
if player.colliderect(asteroid):
running = False
# Update the screen
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), player)
for asteroid in asteroids:
pygame.draw.rect(screen, (255, 0, 0), asteroid)
pygame.display.flip()
# Limit the frame rate
clock.tick(60)
# Quit the game
pygame.quit()
In this minimalistic version, the code sets up the display, defines the player and asteroid properties, and adds basic player movement. It also randomly generates asteroids, moves them downwards, and removes them when they go off the screen. The code checks for collisions between the player and asteroids, and ends the game if a collision occurs. Finally, it updates the screen, limits the frame rate to