xxxxxxxxxx
import pygame as dev , sys , time
dev.init()
dev.mixer.init()
clock = dev.time.Clock()
WIDTH = 600
HEIGHT = 400
score = 0
gameState = ""
window = dev.display.set_mode((WIDTH,HEIGHT))
dev.display.set_caption("cool game")
hitSound = dev.mixer.Sound("hit.wav") #you should have a wav file in project files
ball = dev.Rect(0,0,30,30)
ball.center = (WIDTH/2 , HEIGHT/2)
cpu = dev.Rect(0 , 0 , 20 , 100)
cpu.centery = HEIGHT / 2
player = dev.Rect(0 , 0 , 20 , 100)
player.midright = (WIDTH , HEIGHT/2)
font = dev.font.Font("Karma Future.otf" , 32)#you should have font file with and make sure that you write your font name right
ballSpeedX = 6
ballSpeedY = 6
playerSpeed = 0
cpu_speed = 6
while True:
for event in dev.event.get():
if event.type == dev.QUIT:
sys.exit()
#key events
if event.type == dev.KEYDOWN:
if event.key == dev.K_UP:
playerSpeed = -6
if event.key == dev.K_DOWN:
playerSpeed = 6
if event.type == dev.KEYUP:
if event.key == dev.K_UP:
playerSpeed= 0
if event.key == dev.K_DOWN:
playerSpeed = 0
ball.x += ballSpeedX
ball.y += ballSpeedY
player.y += playerSpeed
cpu.y += cpu_speed
#physics engine
if ball.bottom >= HEIGHT or ball.top <= 0:
ballSpeedY *= -1
if ball.right >= WIDTH or ball.left <= 0:
ballSpeedX *= -1
if player.y <= 0 :
player.y = 0
if player.bottom >= HEIGHT:
player.bottom = HEIGHT
if cpu.y <= 0 :
cpu.y = 0
cpu_speed = 6
if cpu.bottom >= HEIGHT:
cpu.bottom = HEIGHT
cpu_speed = -6
if ball.colliderect(player):
score += 1
ballSpeedX = -6
ballSpeedY = -6
hitSound.play()
if ball.colliderect(cpu):
score += -1
ballSpeedX = 6
ballSpeedY = 6
hitSound.play()
window.fill("black")
wintext = font.render(gameState ,False , 'white' )
txt = font.render("score:"+str(score) ,False , 'white')
#render stuff
dev.draw.aaline(window , "white" , (WIDTH / 2 , 0) , (WIDTH /2 ,HEIGHT ))
dev.draw.ellipse(window , "white" , ball)
dev.draw.rect(window , "white" , cpu)
dev.draw.rect(window , "white" , player)
window.blit(txt , (WIDTH / 2 - 50 , 50))
window.blit(wintext , (WIDTH / 2 -50, HEIGHT /2 - 50))
if(score == 5):
score = 0
gameState = "You Win"
#win or lose state
if(score == -5):
score = 0
gameState = "You Lose"
if(gameState == "You Win" or gameState == "You Lose"):
time.sleep(0.2)
sys.exit()
dev.display.update()
clock.tick(60)
dev.quit()
xxxxxxxxxx
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Game variables
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
BALL_SIZE = 15
PADDLE_SPEED = 5
BALL_SPEED_X = 5
BALL_SPEED_Y = 5
SCORE_MAX = 5
# Paddles
player1 = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
player2 = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
# Ball
ball = pygame.Rect(WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2, BALL_SIZE, BALL_SIZE)
ball_speed_x = BALL_SPEED_X * random.choice((1, -1))
ball_speed_y = BALL_SPEED_Y * random.choice((1, -1))
# Score
score_player1 = 0
score_player2 = 0
font = pygame.font.Font(None, 36)
# Main game loop
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and player1.top > 0:
player1.y -= PADDLE_SPEED
if keys[pygame.K_s] and player1.bottom < HEIGHT:
player1.y += PADDLE_SPEED
if keys[pygame.K_UP] and player2.top > 0:
player2.y -= PADDLE_SPEED
if keys[pygame.K_DOWN] and player2.bottom < HEIGHT:
player2.y += PADDLE_SPEED
# Move ball
ball.x += ball_speed_x
ball.y += ball_speed_y
# Ball collisions with walls
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y *= -1
if ball.left <= 0:
score_player2 += 1
ball_speed_x *= -1
ball.x = WIDTH // 2
ball.y = HEIGHT // 2
if ball.right >= WIDTH:
score_player1 += 1
ball_speed_x *= -1
ball.x = WIDTH // 2
ball.y = HEIGHT // 2
# Ball collisions with paddles
if ball.colliderect(player1) or ball.colliderect(player2):
ball_speed_x *= -1
# Draw everything
WIN.fill(BLACK)
pygame.draw.rect(WIN, WHITE, player1)
pygame.draw.rect(WIN, WHITE, player2)
pygame.draw.ellipse(WIN, WHITE, ball)
score_text = font.render(f"{score_player1} - {score_player2}", True, WHITE)
WIN.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))
pygame.display.flip()
clock.tick(60)
# Check for game end
if score_player1 >= SCORE_MAX or score_player2 >= SCORE_MAX:
running = False
# Game over
WIN.fill(BLACK)
winner_text = font.render("Player 1 Wins!" if score_player1 >= SCORE_MAX else "Player 2 Wins!", True, WHITE)
WIN.blit(winner_text, (WIDTH // 2 - winner_text.get_width() // 2, HEIGHT // 2 - 20))
pygame.display.flip()
# Wait for a while before quitting
pygame.time.delay(2000)
pygame.quit()