create a haxball type of game in python using pygame library
Assistant
Haxball Game in Python using Pygame
Game Overview
Haxball is a 2D sports game where players control a triangular formation of three players, each with the ability to move around the screen and collide with the ball. The objective of the game is to score goals by hitting the ball into the opponent's goal.
Game Requirements
Python 3.x
Pygame library
Game Code
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
# Import required modules
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 20
PLAYER_SIZE = 50
PLAYER_SPEED = 5
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Define some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Define player class
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.speed_x = 0
self.speed_y = 0
def move(self):
self.x += self.speed_x
self.y += self.speed_y
# Boundary checking
if self.x < 0:
self.x = 0
elif self.x > WIDTH - PLAYER_SIZE:
self.x = WIDTH - PLAYER_SIZE
if self.y < 0:
self.y = 0
elif self.y > HEIGHT - PLAYER_SIZE:
self.y = HEIGHT - PLAYER_SIZE
def draw(self):
pygame.draw.polygon(screen, self.color, [(self.x, self.y - PLAYER_SIZE), (self.x, self.y + PLAYER_SIZE), (self.x + PLAYER_SIZE, self.y)])
# Define ball class
class Ball:
def __init__(self):
self.x = WIDTH // 2
self.y = HEIGHT // 2
self.speed_x = random.choice([-5, 5])
self.speed_y = random.choice([-5, 5])
def move(self):
self.x += self.speed_x
self.y += self.speed_y
# Collision with walls
if self.x < 0 or self.x > WIDTH - BALL_RADIUS:
self.speed_x *= -1
if self.y < 0 or self.y > HEIGHT - BALL_RADIUS:
self.speed_y *= -1
def draw(self):
pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), BALL_RADIUS)
# Initialize players and ball
player1 = Player(100, 100, RED)
player2 = Player(700, 100, BLUE)
ball = Ball()
# Game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Get key presses
keys = pygame.key.get_pressed()
# Move players
if keys[pygame.K_w]:
player1.speed_y = -PLAYER_SPEED
elif keys[pygame.K_s]:
player1.speed_y = PLAYER_SPEED
else:
player1.speed_y = 0
if keys[pygame.K_i]:
player2.speed_y = -PLAYER_SPEED
elif keys[pygame.K_k]:
player2.speed_y = PLAYER_SPEED
else:
player2.speed_y = 0
if keys[pygame.K_a]:
player1.speed_x = -PLAYER_SPEED
elif keys[pygame.K_d]:
player1.speed_x = PLAYER_SPEED
else:
player1.speed_x = 0
if keys[pygame.K_j]:
player2.speed_x = -PLAYER_SPEED
elif keys[pygame.K_l]:
player2.speed_x = PLAYER_SPEED
else:
player2.speed_x = 0
# Move players and ball
player1.move()
player2.move()
ball.move()
# Collision detection
if (player1.x < ball.x + BALL_RADIUS and
player1.x + PLAYER_SIZE > ball.x - BALL_RADIUS and
player1.y < ball.y + BALL_RADIUS and
player1.y + PLAYER_SIZE > ball.y - BALL_RADIUS):
ball.speed_x *= -1
if (player2.x < ball.x + BALL_RADIUS and
player2.x + PLAYER_SIZE > ball.x - BALL_RADIUS and
player2.y < ball.y + BALL_RADIUS and
player2.y + PLAYER_SIZE > ball.y - BALL_RADIUS):
ball.speed_x *= -1
# Draw everything
screen.fill((0, 0, 0))
player1.draw()
player2.draw()
ball.draw()
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(60)
How to Play
Run the code using python game.py (assuming you saved the code in a file named game.py).
Use the W and S keys to move the red player up and down, and the A and D keys to move it left and right.
Use the I and K keys to move the blue player up and down, and the J and L keys to move it left and right.
The objective is to hit the ball into the opponent's goal. The ball will bounce off the edges of the screen and the players.
The game will continue until you close the window.
Note
This is a basic implementation of Haxball and can be improved in many ways, such as adding scoring, AI opponents, and smoother animations.
“create a haxball type of game in python using pygame library.” Conversation with llama-3.1-8b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850fy1he7f51tk9pkmyrs20