I am french and here i am trying to recreate a pong game as a class project, for the moment i got the base game where the ball have collision and everything, i am trying to add some power up so that when the ball touches a power up then the speed of the ball changes but here it doesnt work, i will later too try to add some kind of ai where i could play alone and i will too add a menu.
Here is my code so please feel free to use it or to help me.
import pyxel
import random
class Balle:
def __init__(self):
self.x=pyxel.width/2
self.y=pyxel.height/2
self.width=6
self.height=6
self.angle=pyxel.rndi(-70,70)+180*pyxel.rndi(0,1)
self.vitesse=7
self.game_over=False
self.score_1 = 0
self.score_2 = 0
def update(self):
if self.game_over==False:
self.x=self.x+self.vitesse*pyxel.cos(self.angle)
self.y=self.y-self.vitesse*pyxel.sin(self.angle)
if self.y - self.width/2<=0 or pyxel.height <= self.y + self.width/2>=0 :
self.angle= -self.angle
if self.x - self.width/2 < 0:
self.score_2 +=1
self.x=pyxel.width/2
self.y=pyxel.height/2
if self.x + self.width/2 > pyxel.width:
self.score_1 += 1
self.x=pyxel.width/2
self.y=pyxel.height/2
def draw(self):
pyxel.circ(self.x,self.y,self.width/2,13)
pyxel.text(60,3,str(self.score_1),7)
pyxel.text(100,3,str(self.score_2),7)
class PowerUp:
def __init__(self):
self.x= random.randint(30, 120)
self.y= random.randint(20, 90)
self.width=8
self.height=10
def draw(self):
pyxel.rect(self.x,self.y,self.width,self.height, random.randint(1,15))
def collision(self,balle):
if balle.x - balle.width/2 <= self.x + self.width \
and balle.x + balle.width/2 >= self.x \
and balle.y + balle.width/2 >= self.y \
and balle.y - balle.width/2 <= self.y+self.height :
self.boule.vitesse *= 1.5
class Joueur1:
def __init__(self):
self.x=20
self.y=pyxel.height/2
self.width=10
self.height=30
self.vitesse=6
def update(self):
if pyxel.btn(pyxel.KEY_Z) and self.y > 0:
self.y=self.y-self.vitesse
elif pyxel.btn(pyxel.KEY_S) and self.y < 90 :
self.y=self.y+self.vitesse
def draw(self):
pyxel.rect(self.x,self.y,self.width,self.height,5)
def colision(self,balle):
if balle.x - balle.width/2 <= self.x + self.width \
and balle.x + balle.width/2 >= self.x \
and balle.y + balle.width/2 >= self.y \
and balle.y - balle.width/2 <= self.y+self.height :
balle.angle = 180-balle.angle
class Joueur2:
def __init__(self):
self.x=130
self.y=pyxel.height/2
self.width=10
self.height=30
self.vitesse=6
def update(self):
if pyxel.btn(pyxel.KEY_UP) and self.y > 0:
self.y=self.y-self.vitesse
elif pyxel.btn(pyxel.KEY_DOWN) and self.y < 90 :
self.y=self.y+self.vitesse
def draw(self):
pyxel.rect(self.x,self.y,self.width,self.height,10)
def colision(self,balle):
if balle.x - balle.width/2 <= self.x + self.width \
and balle.x + balle.width/2 >= self.x \
and balle.y + balle.width/2 >= self.y \
and balle.y - balle.width/2 <= self.y+self.height :
balle.angle = 180-balle.angle
class Game:
def __init__(self):
pyxel.init(160,120,fps=12)
self.player1 = Joueur1()
self.player2 = Joueur2()
self.boule = Balle()
self.powerawp = PowerUp()
pyxel.run(self.update, self.draw)
def update(self):
self.powerawp.update()
self.player1.update()
self.player2.update()
self.boule.update()
self.player1.colision(self.boule)
self.player2.colision(self.boule)
def draw(self):
pyxel.cls(0)
self.powerawp.draw()
pyxel.line(pyxel.width // 2, 0, pyxel.width // 2, pyxel.height, 7)
self.player1.draw()
self.player2.draw()
self.boule.draw()
Game()