Je hebt het voorlopig einde van de cursus bereikt.
Proficiat.
import pygame
from time import sleep
clock = pygame.time.Clock()
fps =50
afmetingen = (800,600)
achtergrondkleur = [51,204,142]
screen = pygame.display.set_mode(afmetingen)
pygame.display.set_caption('Fysica simulatie')
screen.fill(achtergrondkleur)
x = 30
y = 30
kleur_cirkel = [0,0,255]
pos_cirkel = [x,y]
strl_cirkel = 15
dikte_lijn = 8
pygame.draw.circle(screen, kleur_cirkel, pos_cirkel, strl_cirkel,
dikte_lijn)
dx = 3
dy = 4
# balkje
h = 100
kleur_rect = [0,0,255]
#speler
pos_speler= 300-h/2
# computer
pos_comp = 300-h/2
pygame.draw.rect(screen,kleur_rect,[10,pos_speler,100,h])
pygame.draw.rect(screen,kleur_rect,[700,pos_comp,100,h])
pygame.display.flip()
running = True
while running:
pos_cirkel = [x,y]
screen.fill(achtergrondkleur)
pygame.draw.circle(screen, kleur_cirkel, pos_cirkel, strl_cirkel,
dikte_lijn)
pygame.draw.rect(screen,kleur_rect,[10,pos_speler,10,100])
pygame.draw.rect(screen,kleur_rect,[780,pos_comp,10,100])
pygame.display.flip()
sleep(0.01)
x=x+dx
y=y+dy
if x>=800 or x<=0:
running = False
if (x<=35 and y>=pos_speler and x<pos_speler+100) or (x>=765 and y>=pos_comp and y<pos_comp+100):
dx=-dx
if y<=strl_cirkel or y>=afmetingen[1]-strl_cirkel:
dy=-dy
clock.tick(fps)
for event in pygame.event.get():
# Check for the quit event
if event.type == pygame.QUIT:
# Quit the game
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and pos_speler>0:
pos_speler=pos_speler-5
if event.key == pygame.K_DOWN and pos_speler<500:
pos_speler=pos_speler+5
pygame.quit()