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)