pong2.py

Created by 4223

Created on March 17, 2026

1.63 KB

pong


from kandinsky import fill_rect, draw_string
from ion import *
from time import sleep

# scherm
WIDTH = 320
HEIGHT = 222

# paddles
p1_y = 90
p2_y = 90
paddle_h = 40
paddle_w = 5

# bal
ball_x = 160
ball_y = 111
ball_dx = 2
ball_dy = 2

# score
score1 = 0
score2 = 0

def draw():
    fill_rect(0,0,WIDTH,HEIGHT,(0,0,0))
    
    # middenlijn
    for i in range(0,HEIGHT,10):
        fill_rect(WIDTH//2, i, 2, 5, (255,255,255))
    
    # paddles
    fill_rect(5, p1_y, paddle_w, paddle_h, (255,255,255))
    fill_rect(WIDTH-10, p2_y, paddle_w, paddle_h, (255,255,255))
    
    # bal
    fill_rect(ball_x, ball_y, 5, 5, (255,255,255))
    
    # score
    draw_string(str(score1), WIDTH//4, 10)
    draw_string(str(score2), 3*WIDTH//4, 10)

while True:
    # controls speler 1 (pijltjes)
    if keydown(KEY_UP):
        p1_y -= 3
    if keydown(KEY_DOWN):
        p1_y += 3
    
    # controls speler 2 (EXE en SHIFT)
    if keydown(KEY_EXE):
        p2_y -= 3
    if keydown(KEY_SHIFT):
        p2_y += 3

    # grenzen paddles
    p1_y = max(0, min(HEIGHT - paddle_h, p1_y))
    p2_y = max(0, min(HEIGHT - paddle_h, p2_y))

    # bal bewegen
    ball_x += ball_dx
    ball_y += ball_dy

    # botsing boven/onder
    if ball_y <= 0 or ball_y >= HEIGHT-5:
        ball_dy *= -1

    # botsing paddles
    if ball_x <= 10 and p1_y < ball_y < p1_y + paddle_h:
        ball_dx *= -1

    if ball_x >= WIDTH-15 and p2_y < ball_y < p2_y + paddle_h:
        ball_dx *= -1

    # scoren
    if ball_x < 0:
        score2 += 1
        ball_x, ball_y = 160,111

    if ball_x > WIDTH:
        score1 += 1
        ball_x, ball_y = 160,111

    draw()
    sleep(0.01)

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.