mypong.py

Created by yaya-cout

Created on June 18, 2022

2.71 KB


from math import *
from ion import *
from kandinsky import *
import random
import time

def setup():
  global gauche,speed,blank,barre,j2,ball,x,y,ballx,bally,points,play,multi,j2x,j2y,iavar
  gauche  = True
  speed   = 2
  blank   = color(0,0,0)
  barre   = color(250,250,250)
  j2      = color(250,250,250)
  ball    = color(127,127,127)
  x       = 0
  y       = random.randint(0,200)
  j2x     = 300
  j2y     = random.randint(0,200)
  ballx   = 300
  bally   = random.randint(0,200)
  points  = 0
  play    = True
  joueurs = input("Nbr de joueurs (1 / 2) : ")
  iavar=ia()
  if joueurs == "2":
    multi=True
  else:
    multi=False
    
def circle(x,y,color,taille):
  fill_rect(x,y,taille,taille,color)
  return
  for xl in range(x-taille,x+taille):
    for yl in range(y-taille,y+taille):
      set_pixel(xl,yl,color)

def color_get_zone(x,y,color,taille):
  for xl in range(x,x+taille):
    for yl in range(y,y+taille):
      if get_pixel(xl,yl) == color:
        return True
  return False

def move():
  fill_rect(0,0,350,250,blank)
  draw_string("Pong",140,0)
  circle(x,y,barre,20)
  if multi:
    circle(j2x,j2y,j2,20)
  circle(ballx,bally,ball,20)

def ballmove():
  global ballx, bally, points,speed,gauche
  if gauche:
    ballx-=speed
  else:
    ballx+=speed
  if color_get_zone(ballx,bally,barre,20): 
    points+=1
    speed+=1
    if gauche:
      gauche=False
    else:
      gauche=True
    #ballx=1000
    #if multi:
    bally=random.randint(0,200)
  if gauche==False and ballx > 300:
      if multi:
        draw_string("Perdu, "+str(points)+" points",90,100)
        print("Perdu, "+str(points)+" points")
        return "break"
      else:
        gauche=True
        bally=random.randint(0,200)  
  if ballx < 0:
    draw_string("Perdu, "+str(points)+" points",90,100)
    print("Perdu, "+str(points)+" points")
    return "break"

class ia():
  def __init__(self):
    self.iax=300
    self.iay=random.randint(0,200)
  def iamove(self):
    print(bally)
    print(self.iay)
    if bally<self.iay:
      self.iay=self.iay-speed
    if bally>self.iay:
      self.iay=self.iay+speed
    circle(self.iax,self.iay,barre,20)

setup()
ballmove()
move()

while True:
  while True:
    if play:
      value = ballmove()
      if value == "break":
        break
    #if True:
      if keydown(KEY_UP):
        y-=speed
      if keydown(KEY_DOWN):
        y+=speed
      if keydown(KEY_HOME):
        j2y-=speed
      if keydown(KEY_ONOFF):
        j2y+=speed
      move()
      if multi==False:
        iavar.iamove()
    if keydown(KEY_OK):
      if play:
        play=False
        draw_string("Pause",135,100)
        time.sleep(.5)
      else:
        play=True
        move()
        time.sleep(.5)
  #input("OK pour continuer")
  setup()

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.