Files
Stock-Game/Jan IT-Club Thing/main.py

172 lines
4.5 KiB
Python

import pygame
import random
import os
import sys
import time
pygame.init()
screen = pygame.display.set_mode((1920, 1080))
pygame.display.set_caption("pong")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 55)
WHITE = (255, 255, 255)
pongSizeX, pongSizeY = font.size("pong")
x = screen.get_width() // 2
y = screen.get_height() // 2
pY = screen.get_height() // 2
pX = 1920 - 50
aiY = screen.get_height() // 2
playerPoints = 0
aiPoints = 0
if random.randint(0, 1) == 0:
xbool = True
else:
xbool = False
if random.randint(0, 1) == 0:
ybool = True
else:
ybool = False
rect_width = 10
rect_height = 10
menu = True
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
text = font.render("Press SPACE to start or Press D to select Ai Diffclty", True, WHITE)
screen.blit(text, ((screen.get_width() // 2) - (text.get_width() // 2), (screen.get_height() // 2) - (text.get_height() // 2)))
pygame.display.flip()
if pygame.key.get_pressed()[pygame.K_SPACE]:
menu = False
elif pygame.key.get_pressed()[pygame.K_d]:
diffMenu = True
while diffMenu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
text = font.render("Press 1 for Easy, 2 for Medium, 3 for Hard", True, WHITE)
screen.blit(text, ((screen.get_width() // 2) - (text.get_width() // 2), (screen.get_height() // 2) - (text.get_height() // 2)))
pygame.display.flip()
if pygame.key.get_pressed()[pygame.K_1]:
aiSpeed = 0.3
diffMenu = False
elif pygame.key.get_pressed()[pygame.K_2]:
aiSpeed = 0.4
diffMenu = False
elif pygame.key.get_pressed()[pygame.K_3]:
aiSpeed = 0.7
diffMenu = False
clock.tick(60)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
text = font.render("pong", True, WHITE)
pPointsText = font.render(str(playerPoints), True, WHITE)
aiPointsText = font.render(str(aiPoints), True, WHITE)
screen.blit(pPointsText, (screen.get_width() - pPointsText.get_width() - 20, 0))
screen.blit(aiPointsText, (20, 0))
screen.blit(text, ((screen.get_width() // 2) - pongSizeX, 0))
player_paddle = pygame.Rect(pX, pY, 20, 100)
ai_paddle = pygame.Rect(30, aiY, 20, 100)
ball = pygame.Rect(x, y, rect_width, rect_height)
pygame.draw.rect(screen, WHITE, ball)
pygame.draw.rect(screen, WHITE, player_paddle)
pygame.draw.rect(screen, WHITE, ai_paddle)
pygame.display.flip()
# Ball movement logic and player/AI paddle movement
if x > (screen.get_width() - rect_width) or ball.colliderect(player_paddle):
xbool = True
elif x < 0 or ball.colliderect(ai_paddle):
xbool = False
if xbool:
x = x - 0.5
else:
x = x + 0.5
if y > (screen.get_height() - rect_height) or ybool:
y = y - 0.5
ybool = True
else:
y = y + 0.5
ybool = False
if y < 0:
y = 0
ybool = False
if x < 0:
x = 0
xbool = False
if ai_paddle.centery < ball.centery:
aiY = aiY + aiSpeed
elif ai_paddle.centery > ball.centery:
aiY = aiY - aiSpeed
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
elif pygame.key.get_pressed()[pygame.K_DOWN] and pY + 100 < screen.get_height() - 10:
pY += 1
elif pygame.key.get_pressed()[pygame.K_UP] and pY - 10 > 0:
pY -= 1
# points logic
if x <= 0:
playerPoints += 1
x = screen.get_width() // 2
y = screen.get_height() // 2
if random.randint(0, 1) == 0:
xbool = True
else:
xbool = False
if random.randint(0, 1) == 0:
ybool = True
else:
ybool = False
elif x >= screen.get_width() - 30:
aiPoints += 1
x = screen.get_width() // 2
y = screen.get_height() // 2
if random.randint(0, 1) == 0:
xbool = True
else:
xbool = False
if random.randint(0, 1) == 0:
ybool = True
else:
ybool = False