shootballgpt.py

Created by alexandre-merle

Created on March 27, 2025

4 KB


from math import sin, cos, sqrt
from random import randint
from kandinsky import fill_rect, draw_string
from ion import *
from time import sleep

def ball(x, y, color):
    fill_rect(x-3, y-2, 6, 4, color)
    fill_rect(x-2, y-3, 4, 6, color)

def draw_circle(xc, yc, number, decal, list_ball, color_ball):
    draw_string(number, 160-(len(number)*5), 100, (255,)*3, (0,)*3)
    for i, angle in enumerate(list_ball):
        x = xc - round(sin((angle+decal)/20) * 70)
        y = yc - round(cos((angle+decal)/20) * 70)
        ball(x, y, color_ball[i])

def transition():
    for color in [(255, 180, 0), (0, 0, 0)]:
        for x in range(0, 320, 5):
            fill_rect(x, 0, 5, 222, color)
            sleep(0.01)

def game_loop(levels, lvl=0, multiplayer=False):
    decal, balls_p1, balls_p2, points1, points2 = 0, [], [], 0, 0
    
    while lvl < len(levels):
        list_ball, color_ball, remaining = levels[lvl]
        while remaining > 0:
            fill_rect(0, 0, 320, 222, (0,)*3)
            decal = (decal + 1) % 125
            draw_circle(160, 111, str(lvl+1), decal, list_ball, color_ball)
            
            if multiplayer:
                fill_rect(0, 105, 20, 12, (200, 0, 0))
                fill_rect(300, 105, 20, 12, (0, 0, 200))
                draw_string(str(points2), 20, 0, (255,)*3, (0,)*3)
                draw_string(str(points1), 280, 0, (255,)*3, (0,)*3)
            
            if keydown(KEY_OK):
                if not balls_p1 or balls_p1[-1] < 290:
                    balls_p1.append(305)
            if multiplayer and keydown(KEY_EXP):
                if not balls_p2 or balls_p2[-1] > 30:
                    balls_p2.append(15)
            
            balls_p1 = update_balls(balls_p1, list_ball, color_ball, (0, 0, 255), -3, 305, remaining)
            if multiplayer:
                balls_p2 = update_balls(balls_p2, list_ball, color_ball, (255, 0, 0), 3, 15, remaining)
            
            sleep(0.05)
        
        transition()
        lvl += 1

def update_balls(balls, list_ball, color_ball, player_color, speed, reset_pos, remaining):
    new_balls = []
    for ball_x in balls:
        ball(ball_x, 111, player_color)
        ball_x += speed
        for i in range(len(list_ball)):
            x = 160 - round(sin((list_ball[i])/20) * 70)
            y = 111 - round(cos((list_ball[i])/20) * 70)
            if sqrt((ball_x-x)**2 + (111-y)**2) <= 6:
                if color_ball[i] == player_color:
                    del list_ball[i]
                    del color_ball[i]
                    remaining -= 1
                ball_x = reset_pos
                break
        if 0 < ball_x < 320:
            new_balls.append(ball_x)
    return new_balls

def menu():
    sleep(0.1)
    i, select = 0, 0
    choices = ["Solo", "Duo", "Exit"]
    
    while True:
        i += 1
        fill_rect(0, 0, 320, 222, (0,)*3)
        draw_circle(160, 111, "", i, [0, 124/6, 124*2/6, 124*3/6, 124*4/6, 124*5/6], [(255, 0, 0), (0, 0, 255), (0, 255, 0)]*2)
        draw_string("ShootBall", 115, 5, (255,)*3, (0,)*3)
        
        for j, choice in enumerate(choices):
            color = (255, 180, 0) if select == j else (255,)*3
            draw_string(">" + choice if select == j else choice, 5, 70 + (j * 30), color, (0,)*3)
        
        if keydown(KEY_OK):
            transition()
            if select == 0:
                game_loop([
                    [[0, 125/3, 250/3], [(0, 0, 255)]*3, 3],
                    [[0, 125/4, 250/4, -125/4], [(0, 0, 255), (0, 255, 0), (0, 255, 0), (0, 255, 0)], 3]
                ])
            elif select == 1:
                game_loop([
                    [[0, 125/2, 125/4, -125/4], [(0, 0, 255), (255, 0, 0), (0, 0, 255), (255, 0, 0)], 2]
                ], multiplayer=True)
            else:
                break
        
        if keydown(KEY_UP):
            select = (select - 1) % len(choices)
            sleep(0.05)
        elif keydown(KEY_DOWN):
            select = (select + 1) % len(choices)
            sleep(0.05)
        sleep(0.05)

menu()

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.