gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
can you give me a simple game code
- User
can you give me a simple game code
- Assistant
Sure, I can provide you with a simple Python game using the Pygame library. This example will create a basic game where you can move a square around the window using the arrow keys.
First, make sure you have Pygame installed. You can install it via pip if you haven't already:
pip install pygameHere's a simple code for the game:
import pygame import sys # Initialize Pygame pygame.init() # Constants WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600 WHITE = (255, 255, 255) RED = (255, 0, 0) SQUARE_SIZE = 50 # Create the window window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption('Simple Pygame Example') # Initial position of the square x, y = WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 speed = 5 # Game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Get keys pressed keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: x -= speed if keys[pygame.K_RIGHT]: x += speed if keys[pygame.K_UP]: y -= speed if keys[pygame.K_DOWN]: y += speed # Fill the window with the white color window.fill(WHITE) # Draw the square pygame.draw.rect(window, RED, (x, y, SQUARE_SIZE, SQUARE_SIZE)) # Update the display pygame.display.flip() # Cap the frame rate pygame.time.Clock().tick(60)