autopong.py

Created by schraf

Created on February 01, 2024

1.37 KB

Playlist NUMWORKS de ma chaine Youtube

Version TikTok

Version en JavaScript de Maxime Richard

Vous pouvez effacer la dernière ligne “sleep(0.01)” pour que l’animation aille beaucoup plus vite sur votre calculatrice.


from kandinsky import *
from random import randint
from time import sleep

bricks = []

def drawBricks():
  for c in range(20):
   bricks.append([])
   for r in range(14):
    bricks[c].append('purple' if c < 10 else 'orange')
    fill_rect(c * 16, r * 16, 16, 16, bricks[c][r])

class balle:
 def __init__(self,x,y,coul):
  self.x,self.y = x,y
  self.coul = coul
  self.xSpeed = 4
  self.ySpeed = 4
 def drawBall(self):
  for c in [-1,0,1]:
    x = self.x // 16 + c
    if 20 > x >= 0:
     for r in [-1,0,1]:
      y = self.y // 16 + r
      if 14 > y >= 0: fill_rect(x * 16, y * 16, 16, 16, bricks[x][y])
  fill_rect(self.x, self.y, 16, 16, self.coul)
 def avance(self):
  self.x += self.xSpeed
  self.y += self.ySpeed
  if self.x > 304 or self.x < 0: self.xSpeed *= -1
  if self.y > 208 or self.y < 0: self.ySpeed *= -1  
 def collision(self):
  for c in range(20):
    for r in range(14):
     currentBrick = bricks[c][r]
     x, y = c * 16, r * 16
     if x + 8 > self.x > x - 8 and y + 8 > self.y > y - 8 and currentBrick == self.coul:
      bricks[c][r] = 'purple' if currentBrick == 'orange' else 'orange'
      self.xSpeed *= -1

ball1 = balle(180, 4 * randint(1, 50), 'purple')
ball2 = balle(32, 4 * randint(1, 50), 'orange')
drawBricks()

while True:
  ball1.drawBall()
  ball2.drawBall()
  ball1.collision()
  ball2.collision()
  ball1.avance()
  ball2.avance()
  sleep(.01)

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.