Untitled
unknown
plain_text
2 years ago
3.6 kB
8
Indexable
Ethan Dawson <ethanmdawson475@gmail.com>
12:39 PM (0 minutes ago)
to me
# File: two_player_soccer.py
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('2 Player Soccer Game')
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 128, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Player dimensions and positions
PLAYER_WIDTH, PLAYER_HEIGHT = 20, 20
player1_pos = [50, HEIGHT // 2]
player2_pos = [WIDTH - 70, HEIGHT // 2]
# Ball dimensions and position
BALL_RADIUS = 15
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel = [0, 0]
# Speed and velocity
PLAYER_SPEED = 5
BALL_SPEED = 3
# Scores
score1 = 0
score2 = 0
# Fonts
font = pygame.font.Font(None, 36)
def draw_field():
SCREEN.fill(GREEN)
pygame.draw.line(SCREEN, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT), 5)
pygame.draw.rect(SCREEN, WHITE, (0, HEIGHT // 2 - 100, 10, 200))
pygame.draw.rect(SCREEN, WHITE, (WIDTH - 10, HEIGHT // 2 - 100, 10, 200))
def draw_players_and_ball():
pygame.draw.rect(SCREEN, RED, (*player1_pos, PLAYER_WIDTH, PLAYER_HEIGHT))
pygame.draw.rect(SCREEN, BLUE, (*player2_pos, PLAYER_WIDTH, PLAYER_HEIGHT))
pygame.draw.circle(SCREEN, WHITE, ball_pos, BALL_RADIUS)
def move_players(keys):
if keys[pygame.K_w]:
player1_pos[1] -= PLAYER_SPEED
if keys[pygame.K_s]:
player1_pos[1] += PLAYER_SPEED
if keys[pygame.K_a]:
player1_pos[0] -= PLAYER_SPEED
if keys[pygame.K_d]:
player1_pos[0] += PLAYER_SPEED
if keys[pygame.K_UP]:
player2_pos[1] -= PLAYER_SPEED
if keys[pygame.K_DOWN]:
player2_pos[1] += PLAYER_SPEED
if keys[pygame.K_LEFT]:
player2_pos[0] -= PLAYER_SPEED
if keys[pygame.K_RIGHT]:
player2_pos[0] += PLAYER_SPEED
def move_ball():
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# Ball collision with walls
if ball_pos[1] - BALL_RADIUS <= 0 or ball_pos[1] + BALL_RADIUS >= HEIGHT:
ball_vel[1] = -ball_vel[1]
# Ball collision with players
if player1_pos[0] < ball_pos[0] < player1_pos[0] + PLAYER_WIDTH and player1_pos[1] < ball_pos[1] < player1_pos[1] + PLAYER_HEIGHT:
ball_vel[0] = BALL_SPEED
if player2_pos[0] < ball_pos[0] < player2_pos[0] + PLAYER_WIDTH and player2_pos[1] < ball_pos[1] < player2_pos[1] + PLAYER_HEIGHT:
ball_vel[0] = -BALL_SPEED
def check_goal():
global score1, score2, ball_pos, ball_vel
if ball_pos[0] - BALL_RADIUS <= 0:
score2 += 1
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel = [0, 0]
if ball_pos[0] + BALL_RADIUS >= WIDTH:
score1 += 1
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel = [0, 0]
def draw_score():
score_text = font.render(f"Player 1: {score1} - Player 2: {score2}", True, WHITE)
SCREEN.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))
def game_loop():
global ball_vel
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
move_players(keys)
if keys[pygame.K_SPACE] and ball_vel == [0, 0]:
ball_vel = [BALL_SPEED, BALL_SPEED]
move_ball()
check_goal()
draw_field()
draw_players_and_ball()
draw_score()
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
game_loop()
Editor is loading...
Leave a Comment