brawl.py

Created by 4223

Created on March 17, 2026

2.11 KB

schieten


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

WIDTH = 320
HEIGHT = 222

# spelers
p1_x, p1_y = 50, 100
p2_x, p2_y = 250, 100

hp1 = 5
hp2 = 5

bullets = []

def draw():
    fill_rect(0,0,WIDTH,HEIGHT,(0,0,0))
    
    # spelers
    fill_rect(p1_x, p1_y, 10, 10, (0,255,0))
    fill_rect(p2_x, p2_y, 10, 10, (0,0,255))
    
    # kogels
    for b in bullets:
        fill_rect(b[0], b[1], 4, 4, (255,255,0))
    
    # hp
    draw_string("P1 HP:"+str(hp1), 5, 5)
    draw_string("P2 HP:"+str(hp2), 200, 5)

def shoot(x, y, direction):
    bullets.append([x, y, direction])

def game_over(text):
    fill_rect(0,0,WIDTH,HEIGHT,(0,0,0))
    draw_string(text, 100, 100)
    draw_string("OK = restart", 90, 130)
    
    while not keydown(KEY_OK):
        pass

while True:
    
    # beweging P1
    if keydown(KEY_UP): p1_y -= 2
    if keydown(KEY_DOWN): p1_y += 2
    if keydown(KEY_LEFT): p1_x -= 2
    if keydown(KEY_RIGHT): p1_x += 2
    
    # schieten P1
    if keydown(KEY_OK):
        shoot(p1_x+10, p1_y+5, 1)
    
    # beweging P2
    if keydown(KEY_SHIFT): p2_y -= 2
    if keydown(KEY_ALPHA): p2_y += 2
    if keydown(KEY_LEFTPARENTHESIS): p2_x -= 2
    if keydown(KEY_RIGHTPARENTHESIS): p2_x += 2
    
    # schieten P2
    if keydown(KEY_EXE):
        shoot(p2_x, p2_y+5, -1)

    # update kogels
    for b in bullets[:]:
        b[0] += b[2] * 4
        
        # hit P1
        if abs(b[0]-p1_x) < 8 and abs(b[1]-p1_y) < 8 and b[2] < 0:
            hp1 -= 1
            bullets.remove(b)
        
        # hit P2
        elif abs(b[0]-p2_x) < 8 and abs(b[1]-p2_y) < 8 and b[2] > 0:
            hp2 -= 1
            bullets.remove(b)
        
        # buiten scherm
        elif b[0] < 0 or b[0] > WIDTH:
            bullets.remove(b)

    # grenzen
    p1_x = max(0, min(WIDTH-10, p1_x))
    p1_y = max(0, min(HEIGHT-10, p1_y))
    p2_x = max(0, min(WIDTH-10, p2_x))
    p2_y = max(0, min(HEIGHT-10, p2_y))

    # win check
    if hp1 <= 0:
        game_over("P2 WINS!")
        hp1 = hp2 = 5
    
    if hp2 <= 0:
        game_over("P1 WINS!")
        hp1 = hp2 = 5

    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.