Code a basket ball game
- User
Code a basket ball game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Code a basket ball game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
A full-fledged basketball game! That's a big ask, but I'll provide a simplified version of a basketball game using Python and the Pygame library. This game will have the basic features: player movement, ball bouncing, scoring, and a simple AI opponent. Buckle up!
Basketball Game Code
import pygame
import sys
import random
# Game Constants
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 20
PLAYER_SIZE = 50
FPS = 60
# Colors
WHITE = (255, 255, 255)
ORANGE = (255, 165, 0)
BLACK = (0, 0, 0)
class Ball:
def __init__(self):
self.x = WIDTH // 2
self.y = HEIGHT // 2
self.vx = random.choice([-5, 5])
self.vy = -5
self.speed = 5
def move(self):
self.x += self.vx
self.y += self.vy
if self.x - BALL_RADIUS < 0 or self.x + BALL_RADIUS > WIDTH:
self.vx *= -1
if self.y - BALL_RADIUS < 0:
self.vy *= -1
def reset(self):
self.x = WIDTH // 2
self.y = HEIGHT // 2
self.vx = random.choice([-5, 5])
self.vy = -5
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed = 5
self.score = 0
def move_left(self):
self.x -= self.speed
if self.x < 0:
self.x = 0
def move_right(self):
self.x += self.speed
if self.x > WIDTH - PLAYER_SIZE:
self.x = WIDTH - PLAYER_SIZE
class Opponent(Player):
def __init__(self, x, y):
super().__init__(x, y)
self.speed = 3
def move(self, ball):
if ball.x < self.x + PLAYER_SIZE // 2:
self.x -= self.speed
elif ball.x > self.x + PLAYER_SIZE // 2:
self.x += self.speed
if self.x < 0:
self.x = 0
elif self.x > WIDTH - PLAYER_SIZE:
self.x = WIDTH - PLAYER_SIZE
def draw_text(text, font_size, x, y):
font = pygame.font.SysFont('Arial', font_size)
img = font.render(text, True, WHITE)
screen.blit(img, (x, y))
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Basketball Game')
clock = pygame.time.Clock()
ball = Ball()
player = Player(100, HEIGHT - PLAYER_SIZE - 20)
opponent = Opponent(WIDTH - 100 - PLAYER_SIZE, 20)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move_left()
if keys[pygame.K_RIGHT]:
player.move_right()
opponent.move(ball)
ball.move()
# Collision with player
if (ball.y + BALL_RADIUS > player.y and
ball.x > player.x and
ball.x < player.x + PLAYER_SIZE):
ball.vy *= -1
ball.speed += 0.2
# Collision with opponent
if (ball.y - BALL_RADIUS < opponent.y + PLAYER_SIZE and
ball.x > opponent.x and
ball.x < opponent.x + PLAYER_SIZE):
ball.vy *= -1
ball.speed += 0.2
# Scorekeeping
if ball.y > HEIGHT:
opponent.score += 1
ball.reset()
elif ball.y < 0:
player.score += 1
ball.reset()
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, (player.x, player.y, PLAYER_SIZE, 20))
pygame.draw.rect(screen, ORANGE, (opponent.x, opponent.y, PLAYER_SIZE, 20))
pygame.draw.circle(screen, WHITE, (int(ball.x), int(ball.y)), BALL_RADIUS)
pygame.draw.line(screen, WHITE, (0, HEIGHT // 2), (WIDTH, HEIGHT // 2), 1)
draw_text(f"{player.score} - {opponent.score}", 32, WIDTH // 2 - 40, HEIGHT // 2 - 16)
pygame.display.flip()
clock.tick(FPS)
How to Run
pip install pygamebasketball_game.pypython basketball_game.pyGameplay
Features
Limitations
Now, grab a friend (or play solo against the AI) and start hooping! Have fun playing this simplified basketball game.
Future Enhancements
Want to take this game to the next level? Try adding:
mixer module to add bounce, score, and crowd sounds.Sprite class and animate function.network module or a library like socket.Happy coding!
make them html files
Here's the same basketball game, but this time implemented using HTML, CSS, and JavaScript. I've created three files:
index.html: The main game pagestyle.css: Styles for the game layout and visualsscript.js: The game logic and JavaScript codeFolder Structure
basketball-game/
├── index.html
├── style.css
└── script.js
Let's dive into each file:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basketball Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="game-canvas" width="800" height="600"></canvas>
<div id="score-container">
<span id="player-score">0</span> - <span id="opponent-score">0</span>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
background-color: #000;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#game-canvas {
border: 1px solid #fff;
}
#score-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 32px;
color: #fff;
}
#player-score, #opponent-score {
font-weight: bold;
}
script.js
// Game Constants
const WIDTH = 800;
const HEIGHT = 600;
const BALL_RADIUS = 20;
const PLAYER_SIZE = 50;
const FPS = 60;
// Colors
const WHITE = '#fff';
const ORANGE = '#ffa500';
const BLACK = '#000';
class Ball {
constructor() {
this.x = WIDTH / 2;
this.y = HEIGHT / 2;
this.vx = Math.random() < 0.5 ? -5 : 5;
this.vy = -5;
this.speed = 5;
}
move() {
this.x += this.vx;
this.y += this.vy;
if (this.x - BALL_RADIUS < 0 || this.x + BALL_RADIUS > WIDTH) {
this.vx *= -1;
}
if (this.y - BALL_RADIUS < 0) {
this.vy *= -1;
}
}
reset() {
this.x = WIDTH / 2;
this.y = HEIGHT / 2;
this.vx = Math.random() < 0.5 ? -5 : 5;
this.vy = -5;
}
}
class Player {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 5;
this.score = 0;
}
moveLeft() {
this.x -= this.speed;
if (this.x < 0) {
this.x = 0;
}
}
moveRight() {
this.x += this.speed;
if (this.x > WIDTH - PLAYER_SIZE) {
this.x = WIDTH - PLAYER_SIZE;
}
}
}
class Opponent extends Player {
constructor(x, y) {
super(x, y);
this.speed = 3;
}
move(ball) {
if (ball.x < this.x + PLAYER_SIZE / 2) {
this.x -= this.speed;
} else if (ball.x > this.x + PLAYER_SIZE / 2) {
this.x += this.speed;
}
if (this.x < 0) {
this.x = 0;
} else if (this.x > WIDTH - PLAYER_SIZE) {
this.x = WIDTH - PLAYER_SIZE;
}
}
}
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const scoreContainer = document.getElementById('score-container');
const playerScoreElement = document.getElementById('player-score');
const opponentScoreElement = document.getElementById('opponent-score');
const ball = new Ball();
const player = new Player(100, HEIGHT - PLAYER_SIZE - 20);
const opponent = new Opponent(WIDTH - 100 - PLAYER_SIZE, 20);
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
player.moveLeft();
} else if (e.key === 'ArrowRight') {
player.moveRight();
}
});
function draw() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// Draw player and opponent
ctx.fillStyle = WHITE;
ctx.fillRect(player.x, player.y, PLAYER_SIZE, 20);
ctx.fillStyle = ORANGE;
ctx.fillRect(opponent.x, opponent.y, PLAYER_SIZE, 20);
// Draw ball
ctx.fillStyle = WHITE;
ctx.beginPath();
ctx.arc(ball.x, ball.y, BALL_RADIUS, 0, Math.PI * 2);
ctx.fill();
// Draw middle line
ctx.beginPath();
ctx.moveTo(0, HEIGHT / 2);
ctx.lineTo(WIDTH, HEIGHT / 2);
ctx.strokeStyle = WHITE;
ctx.stroke();
// Update score display
playerScoreElement.textContent = player.score;
opponentScoreElement.textContent = opponent.score;
}
function update() {
opponent.move(ball);
ball.move();
// Collision with player
if (ball.y + BALL_RADIUS > player.y &&
ball.x > player.x &&
ball.x < player.x + PLAYER_SIZE) {
ball.vy *= -1;
ball.speed += 0.2;
}
// Collision with opponent
if (ball.y - BALL_RADIUS < opponent.y + PLAYER_SIZE &&
ball.x > opponent.x &&
ball.x < opponent.x + PLAYER_SIZE) {
ball.vy *= -1;
ball.speed += 0.2;
}
// Scorekeeping
if (ball.y > HEIGHT) {
opponent.score++;
ball.reset();
} else if (ball.y < 0) {
player.score++;
ball.reset();
}
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loop();
make it one single html file
Here's the same basketball game, but this time condensed into a single HTML file. I've embedded the CSS and JavaScript code directly into the HTML file using <style> and <script> tags.
basketball_game.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basketball Game</title>
<style>
/* CSS Styles */
body {
background-color: #000;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#game-canvas {
border: 1px solid #fff;
}
#score-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 32px;
color: #fff;
}
#player-score, #opponent-score {
font-weight: bold;
}
</style>
</head>
<body>
<canvas id="game-canvas" width="800" height="600"></canvas>
<div id="score-container">
<span id="player-score">0</span> - <span id="opponent-score">0</span>
</div>
<script>
// JavaScript Code
// Game Constants
const WIDTH = 800;
const HEIGHT = 600;
const BALL_RADIUS = 20;
const PLAYER_SIZE = 50;
const FPS = 60;
// Colors
const WHITE = '#fff';
const ORANGE = '#ffa500';
const BLACK = '#000';
class Ball {
constructor() {
this.x = WIDTH / 2;
this.y = HEIGHT / 2;
this.vx = Math.random() < 0.5 ? -5 : 5;
this.vy = -5;
this.speed = 5;
}
move() {
this.x += this.vx;
this.y += this.vy;
if (this.x - BALL_RADIUS < 0 || this.x + BALL_RADIUS > WIDTH) {
this.vx *= -1;
}
if (this.y - BALL_RADIUS < 0) {
this.vy *= -1;
}
}
reset() {
this.x = WIDTH / 2;
this.y = HEIGHT / 2;
this.vx = Math.random() < 0.5 ? -5 : 5;
this.vy = -5;
}
}
class Player {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 5;
this.score = 0;
}
moveLeft() {
this.x -= this.speed;
if (this.x < 0) {
this.x = 0;
}
}
moveRight() {
this.x += this.speed;
if (this.x > WIDTH - PLAYER_SIZE) {
this.x = WIDTH - PLAYER_SIZE;
}
}
}
class Opponent extends Player {
constructor(x, y) {
super(x, y);
this.speed = 3;
}
move(ball) {
if (ball.x < this.x + PLAYER_SIZE / 2) {
this.x -= this.speed;
} else if (ball.x > this.x + PLAYER_SIZE / 2) {
this.x += this.speed;
}
if (this.x < 0) {
this.x = 0;
} else if (this.x > WIDTH - PLAYER_SIZE) {
this.x = WIDTH - PLAYER_SIZE;
}
}
}
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const scoreContainer = document.getElementById('score-container');
const playerScoreElement = document.getElementById('player-score');
const opponentScoreElement = document.getElementById('opponent-score');
const ball = new Ball();
const player = new Player(100, HEIGHT - PLAYER_SIZE - 20);
const opponent = new Opponent(WIDTH - 100 - PLAYER_SIZE, 20);
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
player.moveLeft();
} else if (e.key === 'ArrowRight') {
player.moveRight();
}
});
function draw() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// Draw player and opponent
ctx.fillStyle = WHITE;
ctx.fillRect(player.x, player.y, PLAYER_SIZE, 20);
ctx.fillStyle = ORANGE;
ctx.fillRect(opponent.x, opponent.y, PLAYER_SIZE, 20);
// Draw ball
ctx.fillStyle = WHITE;
ctx.beginPath();
ctx.arc(ball.x, ball.y, BALL_RADIUS, 0, Math.PI * 2);
ctx.fill();
// Draw middle line
ctx.beginPath();
ctx.moveTo(0, HEIGHT / 2);
ctx.lineTo(WIDTH, HEIGHT / 2);
ctx.strokeStyle = WHITE;
ctx.stroke();
// Update score display
playerScoreElement.textContent = player.score;
opponentScoreElement.textContent = opponent.score;
}
function update() {
opponent.move(ball);
ball.move();
// Collision with player
if (ball.y + BALL_RADIUS > player.y &&
ball.x > player.x &&
ball.x < player.x + PLAYER_SIZE) {
ball.vy *= -1;
ball.speed += 0.2;
}
// Collision with opponent
if (ball.y - BALL_RADIUS < opponent.y + PLAYER_SIZE &&
ball.x > opponent.x &&
ball.x < opponent.x + PLAYER_SIZE) {
ball.vy *= -1;
ball.speed += 0.2;
}
// Scorekeeping
if (ball.y > HEIGHT) {
opponent.score++;
ball.reset();
} else if (ball.y < 0) {
player.score++;
ball.reset();
}
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
How to Run
basketball-game.index.html, style.css, and script.js.index.html in your favorite web browser (e.g., Google Chrome, Mozilla Firefox).Gameplay
Features
requestAnimationFrameLimitations
Now, grab a friend (or play solo against the AI) and start hooping! Have fun playing this simplified basketball game in your browser.
Future Enhancements
Want to take this game to the next level? Try adding:
howler.js to add bounce, score, and crowd sounds.GSAP.socket.io to play against friends online.Happy coding!
That's it! This single HTML file contains everything you need to play the basketball game:
<canvas> element for rendering the game and the <div> for displaying the score.<style> tag to make the game look decent.<script> tag.How to Run
basketball_game.html (or any name you like, as long as it has the .html extension).Gameplay
Have fun playing this simplified basketball game!
Future Enhancements
Want to take this game to the next level? Try adding:
howler.js to add bounce, score, and crowd sounds.GSAP.socket.io to play against friends online.Happy coding!