For my Dofe Gold i have created a game of pong in pygame, as this was my first time using pygame i did not use sprites as this didnt occur to me. I am now wanting a solution which will allow me to resolve my problem without entirely rewriting my code with sprites. Note: i want this to still be my code so i will not accept a rewritten solution by someone else as this will take away any sense of accomplishment. Many thanks in advance. my code:
import pygame
import random
global vel
run = True
def pong():
global run
collision = 0
pygame.init()
screen = (600, 600)
window = pygame.display.set_mode((screen))
pygame.display.set_caption("Pong")
x = 300
y = 590
coords = (300, 150)
width = 175
height = 10
vel = 10 - selection
velx = 10
vely = 10
while run == True:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x>0:
x -= vel
elif keys[pygame.K_RIGHT] and x<600-width:
x += vel
if event.type == pygame.MOUSEBUTTONUP:
pygame.quit()
quit()
paddlecoords = (x, y, width, height)
window.fill((255, 255, 255))
ball = pygame.draw.circle(window, (255,0,255), coords,(35), (0))
paddle = pygame.draw.rect(window, (0, 0, 0), paddlecoords)
pygame.display.update()
coords=((int(coords[0])+velx), (int(coords[1])+vely))
if coords[0]>600-35:
velx = -velx
elif coords[0]<35:
velx = -velx
elif coords[1]<35:
vely = -vely
elif coords[1]>600-35:
vely = -vely
selection =input("Do you want to play\n1)easy\n2)medium\n3)hard\n4)impossible?\n")
if selection.isdigit():
if 0 < int(selection) < 5:
selection = int(selection)
selection = (selection-1)*2
else:
print("must be between 1 and 4")
else:
print("number must be an integer")
quit()
pong()