pong.py

Created by 4223

Created on August 28, 2025

1.16 KB

pong


from kandinsky import *
from ion import *
from time import sleep

WIDTH, HEIGHT = 320, 222
WHITE = color(255,255,255)
BLACK = color(0,0,0)

# Paddles en bal
p1_y, p2_y = HEIGHT//2 - 20, HEIGHT//2 - 20
ball_x, ball_y = WIDTH//2, HEIGHT//2
ball_dx, ball_dy = 3, 3

def draw():
    fill_rect(0,0,WIDTH,HEIGHT,BLACK)
    fill_rect(10,p1_y,5,40,WHITE)
    fill_rect(WIDTH-15,p2_y,5,40,WHITE)
    fill_rect(ball_x,ball_y,8,8,WHITE)

while True:
    # Besturing speler 1
    if keydown(KEY_UP) and p1_y>0:
        p1_y -= 5
    if keydown(KEY_DOWN) and p1_y<HEIGHT-40:
        p1_y += 5

    # AI tegenstander (CPU)
    if ball_y > p2_y+20:
        p2_y += 3
    elif ball_y < p2_y+20:
        p2_y -= 3

    # Bal bewegen
    ball_x += ball_dx
    ball_y += ball_dy

    # Botsing boven/onder
    if ball_y<=0 or ball_y>=HEIGHT-8:
        ball_dy = -ball_dy

    # Botsing paddles
    if 10<=ball_x<=15 and p1_y<=ball_y<=p1_y+40:
        ball_dx = -ball_dx
    if WIDTH-23<=ball_x<=WIDTH-15 and p2_y<=ball_y<=p2_y+40:
        ball_dx = -ball_dx

    # Scoren (reset bal)
    if ball_x<0 or ball_x>WIDTH:
        ball_x, ball_y = WIDTH//2, HEIGHT//2

    draw()
    sleep(0.02)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.