voetbal
from kandinsky import * from ion import * from time import sleep from random import randint WIDTH, HEIGHT = 320, 222 GREEN = color(0,150,0) WHITE = color(255,255,255) BLACK = color(0,0,0) BLUE = color(0,0,255) RED = color(255,0,0) # Speelveld GOAL_TOP = 80 GOAL_BOTTOM = 140 GOAL_X = WIDTH - 20 # Speler player_x = 40 player_y = HEIGHT // 2 # Bal ball_x = 60 ball_y = HEIGHT // 2 ball_dx = 0 ball_dy = 0 # Keeper keeper_y = HEIGHT // 2 - 10 # Score score = 0 def draw_field(): fill_rect(0,0,WIDTH,HEIGHT,GREEN) # Doel fill_rect(GOAL_X,GOAL_TOP,10,GOAL_BOTTOM-GOAL_TOP,WHITE) # Middenlijn fill_rect(WIDTH//2,0,2,HEIGHT,WHITE) # Score draw_string("Score: "+str(score),10,10,WHITE,GREEN) def draw_player(): fill_rect(player_x,player_y-10,10,20,BLUE) def draw_ball(): fill_rect(ball_x,ball_y,8,8,WHITE) def draw_keeper(): fill_rect(GOAL_X-10,keeper_y,10,20,RED) while True: # Besturing speler if keydown(KEY_UP) and player_y>10: player_y -= 3 if keydown(KEY_DOWN) and player_y<HEIGHT-10: player_y += 3 if keydown(KEY_RIGHT) and player_x<WIDTH-40: player_x += 3 if keydown(KEY_LEFT) and player_x>10: player_x -= 3 # Schop de bal als je dichtbij bent if abs(player_x-ball_x)<10 and abs(player_y-ball_y)<10 and (keydown(KEY_OK) or keydown(KEY_EXE)): ball_dx = 6 ball_dy = randint(-2,2) # Beweeg de bal ball_x += ball_dx ball_y += ball_dy ball_dx *= 0.9 # vertraagt bal ball_dy *= 0.9 # Keeper beweegt random (maar in goal zone) if keeper_y+10 < ball_y: keeper_y += 2 elif keeper_y > ball_y: keeper_y -= 2 # Botsing bal met keeper if GOAL_X-20<ball_x<GOAL_X-10 and keeper_y<ball_y<keeper_y+20: ball_dx = -4 # Botsing bal met boven/onder if ball_y<=0 or ball_y>=HEIGHT-8: ball_dy = -ball_dy # Scoor! if ball_x >= GOAL_X and GOAL_TOP<ball_y<GOAL_BOTTOM: score += 1 ball_x, ball_y = 60, HEIGHT//2 ball_dx, ball_dy = 0, 0 # Herteken scherm draw_field() draw_player() draw_ball() draw_keeper() sleep(0.03)