Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)
OpenAssistant Conversations (oasst2)
Can you write a code in python for a simple racing game
In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
- User
Can you write a code in python for a simple racing game
- Assistant
import pygame # Initialize pygame pygame.init() # Create a screen with width 800 and height 600 screen = pygame.display.set_mode((800, 600)) # Set the title and icon of the window pygame.display.set_caption("Simple Racing Game") icon = pygame.image.load("car.png") pygame.display.set_icon(icon) # Load the background image background = pygame.image.load("road.png") # Load the car image and set its initial position and speed car = pygame.image.load("car.png") car_x = 400 car_y = 500 car_speed = 5 # Define a function to handle keyboard input def handle_input(): # Get the current state of the keyboard keys keys = pygame.key.get_pressed() # Declare global variables to access them inside the function global car_x, car_y, car_speed # If left arrow key is pressed, move the car to the left if keys[pygame.K_LEFT]: car_x -= car_speed # If right arrow key is pressed, move the car to the right if keys[pygame.K_RIGHT]: car_x += car_speed # If up arrow key is pressed, increase the car speed if keys[pygame.K_UP]: car_speed += 0.1 # If down arrow key is pressed, decrease the car speed if keys[pygame.K_DOWN]: car_speed -= 0.1 # Limit the car speed to a minimum of 0 and a maximum of 10 if car_speed < 0: car_speed = 0 if car_speed > 10: car_speed = 10 # Limit the car position to stay within the screen boundaries if car_x < 0: car_x = 0 if car_x > 736: car_x = 736 # Define a function to draw the game objects on the screen def draw(): # Fill the screen with black color screen.fill((0, 0, 0)) # Draw the background image at position (0, 0) screen.blit(background, (0, 0)) # Draw the car image at its current position screen.blit(car, (car_x, car_y)) # Create a variable to store whether the game is running or not running = True # Start a game loop that runs until the user quits while running: # Handle the events that occur in the game window for event in pygame.event.get(): # If the user clicks the close button, quit the game if event.type == pygame.QUIT: running = False # Handle the keyboard input from the user handle_input() # Draw the game objects on the screen draw() # Update the display with the changes made pygame.display.update()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.