pong.py

Created by 4244

Created on August 28, 2025

1.4 KB

fe


# Type your text hereimport time, random
from kandinsky import fill_rect, draw_string
from ion import keydown

W, H = 160, 120
ball_x, ball_y = W//2, H//2
dx, dy = 2, 1
p1_y, p2_y = H//2-10, H//2-10
paddle_h = 20
score1, score2 = 0, 0

def draw():
    fill_rect(0,0,W,H,(0,0,0))
    fill_rect(5,p1_y,3,paddle_h,(255,255,255))
    fill_rect(W-8,p2_y,3,paddle_h,(255,255,255))
    fill_rect(ball_x,ball_y,3,3,(255,255,255))
    draw_string(f"{score1}", W//2-20, 5, (255,255,255), (0,0,0))
    draw_string(f"{score2}", W//2+10, 5, (255,255,255), (0,0,0))

while True:
    # besturing speler links (EXE=31, UP=1, DOWN=2 bij NumWorks)
    if keydown(1) and p1_y>0:       # pijltje omhoog
        p1_y -= 2
    if keydown(2) and p1_y<H-paddle_h: # pijltje omlaag
        p1_y += 2

    # besturing speler rechts (use 3/4 of A/B keys afhankelijk model)
    if keydown(3) and p2_y>0:
        p2_y -= 2
    if keydown(4) and p2_y<H-paddle_h:
        p2_y += 2

    # bal bewegen
    ball_x += dx
    ball_y += dy

    # botsing boven/onder
    if ball_y<=0 or ball_y>=H-3: dy = -dy

    # botsing paddles
    if 5<=ball_x<=8 and p1_y<=ball_y<=p1_y+paddle_h:
        dx = -dx
    if W-11<=ball_x<=W-8 and p2_y<=ball_y<=p2_y+paddle_h:
        dx = -dx

    # scoren
    if ball_x<0:
        score2+=1; ball_x,ball_y=W//2,H//2; dx=2
    if ball_x>W:
        score1+=1; ball_x,ball_y=W//2,H//2; dx=-2

    draw()
    time.sleep(0.03)

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.