ballsgpt.py

Created by alexandre-merle

Created on March 27, 2025

1.05 KB


import kandinsky as k
import time
import math

# Paramètres
width, height = 320, 240  # Dimensions de l'écran NumWorks
gravity = 0.5  # Gravité
bounce_factor = 0.7  # Facteur de rebond

class Ball:
    def __init__(self, x, y, radius, color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.vx = 0
        self.vy = 0

    def update(self):
        self.vy += gravity  # Appliquer la gravité
        self.y += self.vy

        # Collision avec le sol
        if self.y + self.radius > height:
            self.y = height - self.radius
            self.vy = -self.vy * bounce_factor

    def draw(self):
        k.fill_rect(int(self.x), int(self.y), self.radius,self.radius, self.color)

# Initialisation
ball = Ball(width // 2, 50, 10, (255, 0, 0))

# Boucle principale
def main():
    while True:
        k.fill_rect(0, 0, width, height, (255, 255, 255))  # Effacer l'écran
        ball.update()
        ball.draw()
        time.sleep(0.05)  # Pause pour animation

# Lancer la simulation
main()

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.