pong.py

Created by wperez274

Created on August 12, 2022

4.74 KB

not my program

I will improve it soon and make it my own!


#  My new Pong Game :-)

from math import cos,sin,pi
from kandinsky import *
from ion import *
from random import random,randint

#light grey. nice :-)
background_2=(192,192,192)

background_1="cyan"


def menu():

  global Speed,Vraquette, background_1, background_2
  # menu background
  fill_rect(0,0,344,224,background_2)

  draw_string("Choose:",140,20,"black",background_1)
  draw_string("1-PLAYER    2-PLAYER",60,40,"black",background_2)
  
  mode=1
  while not (keydown(KEY_OK) or keydown(KEY_EXE)):
    draw_string("*",40+120*mode,40,"black",background_2)
    
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      mode=1-mode
      draw_string(" ",40+120*(1-mode),40,background_2,background_2)
 
      while keydown(KEY_LEFT) or keydown(KEY_RIGHT):True
  while keydown(KEY_OK) or keydown(KEY_EXE):True
  draw_string("Speed",90,70,"black",background_2)
  draw_string("Slow   Medium   Fast",60,90,"black",background_2)
  Speed=1
  
  while not (keydown(KEY_OK) or keydown(KEY_EXE)):
    draw_string("*",-30+70*Speed+10*(Speed//3),90,"black",background_2)
    
    dv=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
    
    if (keydown(KEY_RIGHT) or keydown(KEY_LEFT)) and dv != 0:
      draw_string(" ",-30+70*Speed+10*(Speed//3),90,background_2,background_2)
      
      Speed=(Speed+dv-1)%3+1
      while keydown(KEY_RIGHT) or keydown(KEY_LEFT):True
  
  while keydown(KEY_OK) or keydown(KEY_EXE):True
  draw_string("Racket",55,120,"black", background_2)
  draw_string("slow   medium   fast",60,140,"black", background_2)
  Vraquette=2
  while not (keydown(KEY_OK) or keydown(KEY_EXE)):
    draw_string("*",-30+70*Vraquette+10*(Vraquette//3),140,"black", background_2)
    
    dr=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
    if (keydown(KEY_RIGHT) or keydown(KEY_LEFT)) and dr != 0:
      draw_string(" ",-30+70*Vraquette+10*(Vraquette//3),140,background_2,background_2)
      
      Vraquette=(Vraquette+dr-1)%3+1
      while keydown(KEY_RIGHT) or keydown(KEY_LEFT):True
  # ajouter mode de rebond : angle relatif ou absolu
  return mode
mode=menu()
fill_rect(0,18,320,1,'purple')
fill_rect(0,204,320,1,'purple')
#raquette 1
x1=5
y1=111

#raquette 2
x2=315
y2=111

#balle
x=160
y=111

def balle(x,y,couleur):
  fill_rect(x-2,y-2,5,5,couleur)

def raquette(x,y,couleur):
  fill_rect(x-1,y-10,3,21,couleur)

score=[0,0]
angle=(pi/6-pi/18)*random()+pi/18
horiz=2*randint(0,1)-1
verti=1

clr1=(96,44,120)
clr2=(104,44,128)

while 1:
  fill_rect(0,19,320,185,(192,192,192))
  x=160
  y=111
  x1=5
  y1=111
  x2=315
  y2=111
  #Speed=2
  V=Speed
  while 1:
    d1 = (keydown(KEY_DOWN)-keydown(KEY_UP))
    y1 = max(min(193,y1+(Speed+Vraquette-1)*d1),29) # y1 +=3*d1
    if mode:
      d2 = (keydown(KEY_RIGHT)-keydown(KEY_LEFT))
    else:
      d2 = min(Speed+Vraquette-1,abs(int(y2-y)))*(2*(y>y2)-1)
    y2 = max(min(193,y2+d2*(1+(Speed+Vraquette-1-1)*mode)),29)#y2 += d2 #3*(2*randint(0,3)-3
    #draw_string("|",x1,y1,['green','red'][(y1-y)**2>100])
    raquette(x1,y1,['black','red'][(y1-y)**2>169])
    if d1 != 0:fill_rect(x1-1,y1-2-13*d1,5,5,background_2)
    raquette(x2,y2,['blue','red'][(y2-y)**2>169])
    if d2 != 0:
      if mode:
        fill_rect(x2-1,y2-2-13*d2,5,5,background_2)
      else:
        fill_rect(x2-1,y2-2-13*(2*(y>y2)-1),5,5,background_2)

    vx=x
    vy=y
    x+=V*cos(angle)*horiz
    y+=V*sin(angle)*verti
    clr1,clr2=clr2,clr1
    balle(int(x),int(y),clr1)
    for i in range(5):
      for j in range(5):
        if get_pixel(int(vx)-2+i,int(vy)-2+j)==clr2:
          set_pixel(int(vx)-2+i,int(vy)-2+j,background_2)
    
    if x>311:
      horiz = -1
      if (y-y2)**2>169:
        draw_string("You Scored!",85,110,"black",background_2)
        score[0]+=1
        break
      else:
        #angle=min(2*pi/5,angle+(y-y2)*pi/180)
        angle=abs(6*(y-y2)*pi/180)
        verti = 2*(y>y2)-1
        V *= 1.1
        if V>1.8:
          V=1.8
        draw_string("Speed : "+str(V)[:4]+"  ",3,205,"black",background_2)
        draw_string("Angle : "+str(int(abs(angle*180/pi))),210,205,"black",background_2)
    if x<9:
      horiz = 1
      if (y-y1)**2>169:
        draw_string("Computer Scores",85,110,"black", background_2)
        score[1]+=1
        break
      else:
        #angle=min(2*pi/5,angle+(y-y1)*pi/180)
        angle=abs(6*(y-y1)*pi/180)
        verti = 2*(y>y1)-1
        V *= 1.01
        draw_string("Speed : "+str(V)[:4]+"  ",3,205,"black",background_2)
        draw_string("Angle : "+str(int(abs(angle*180/pi))),210,205,"black",background_2)
    if y>200 or y<22:
      verti *= -1
      #angle *= -1
    fill_rect(0,204,320,1,'purple')
    fill_rect(0,18,320,1,'purple')
  while not keydown(KEY_OK):
    draw_string(str(score[0]),2,0,"black",background_2)
    draw_string(str(score[1]),310,0,"black",background_2)
    
    draw_string("Press [OK] Key",145,131,"black",background_2)

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.