Home > front end >  Pong Game From Python for absolute beginners
Pong Game From Python for absolute beginners

Time:06-13

i am running into two error going two routes the ball is find works great but the score text wont work, every time i implement it it throws the two errors.

from superwires import games, color
from random import *

"""Main application file"""
 
#the screen width and height
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
points = 0
score = 0

"""Paddle object that is controlled by the player. Moves up and down. Collides with the ball. Points are added when ball is collided with"""
class Paddle(games.Sprite):
    HEIGHT = 480/20
    paddle_image = games.load_image('paddle.png', transparent=True)
    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.paddle_image,
                                  x = 40,
                                  y = 50,
                                  is_collideable=True)
        self.score = games.Text(value = 0,
                           size = 30, 
                           color = color.green,
                           x = SCREEN_WIDTH - 25,
                           y = SCREEN_HEIGHT - 431)
        games.screen.add(self.score)
        games.screen.mainloop()
        
    """ updates Position based on mouse Position"""
    def update(self):
     self.y = games.mouse.y
     self.Check_Collision()
             
    def Check_Collision(self):
        for PongBall in self.overlapping_sprites:
             PongBall.handle_collide()
           



    



"""actual ball in play """
class PongBall(games.Sprite):
  
    
   #velocity variables
    vel_x = 5
    vel_y = 0

    #pos of boundry to ball pos
    lower_bounds_check = 0
    upper_bounds_check = 0 
   
  


    
    #checks if paddle gets collided with
    def handle_collide(self):
        if((SCREEN_WIDTH/2) < self.x):
            PongBall.vel_x = randint(-10,-5)
            PongBall.vel_y = randint(-10,-5)
        elif((SCREEN_WIDTH/2) > self.x):
            PongBall.vel_x = randint(5,10)
            PongBall.vel_y = randint(5,10)
            
                
        

            
    #checks if it hit the bottom; bool function
    def check_boundry_lower(self):
        PongBall.lower_bounds_check = (SCREEN_HEIGHT/2)
        return ((self.y - PongBall.upper_bounds_check) > PongBall.lower_bounds_check)
            
   #checks if it hits the top; bool function
    def check_boundry_upper(self):
        PongBall.upper_bounds_check = (SCREEN_HEIGHT/2)
        return ((self.y   PongBall.upper_bounds_check) < PongBall.upper_bounds_check)


    #Resets the velocity based on boundry collision
    def boundry_update(self):
        if(self.check_boundry_upper()):
            PongBall.vel_y = randint(5,10)
            return
        elif(self.check_boundry_lower()):
            PongBall.vel_y = randint(-10,-5)
            return

    
        

    def update(self):
        self.boundry_update()
        self.x  = PongBall.vel_x
        self.y  = PongBall.vel_y

    
   
   
"""
Entry point Function
"""


# Create background, paddle, and ball. Then call the game mainloop.

# crate the window
games.init(SCREEN_WIDTH, SCREEN_HEIGHT, 50, virtual = False)
    

#sets/loads background
background = games.load_image(f"background.png", transparent=True)
games.screen.background = background 

# paddle image
paddle_image = games.load_image(f"paddle.png", transparent = True)

#PingPong ball
PingPongBall = games.load_image(f"Ball.png", transparent = True)
   

#paddle Right and left
paddle_1 = Paddle()
paddle_2 = Paddle()

#pingball ball instance
ball = PongBall(image = PingPongBall, x = 150, y = 250, is_collideable=True)

  
#adding all three sprites to screen 
games.screen.add(ball)
games.screen.add(paddle_1)
games.screen.add(paddle_2)


#each instance update function
ball.update()
paddle_1.update()
paddle_2.update()

#adding text





#main loop
games.screen.mainloop()

i get this error;

 Traceback (most recent call last):
  File "C:\Users\gamer\Desktop\PingPong\main.py", line 15, in <module>
    class Paddle(games.Sprite):
  File "C:\Users\gamer\Desktop\PingPong\main.py", line 17, in Paddle
    paddle_image = games.load_image('paddle.png', transparent=True)
  File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 836, in load_image
    if not screen.virtual:
AttributeError: 'NoneType' object has no attribute 'virtual'

 

Please help i cant seem to escape the error even if i dont pass through the constructor i get text object doesnt not have handle_collide() attribute -Thanks guys

new error:

Traceback (most recent call last):
  File "C:\Users\gamer\Desktop\PingPong\main.py", line 153, in <module>
    games.screen.mainloop()
  File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 783, in mainloop
    sprite._process_sprite()
  File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 440, in _process_sprite
    self.update()
  File "C:\Users\gamer\Desktop\PingPong\main.py", line 40, in update
    self.Check_Collision()
  File "C:\Users\gamer\Desktop\PingPong\main.py", line 36, in Check_Collision
    PongBall.handle_collide()
AttributeError: 'Text' object has no attribute 'handle_collide'

CodePudding user response:

You need to have the games.load in the __init__ function so as not to call it before the environment is set up.

Replace this:

class Paddle(games.Sprite):
    HEIGHT = 480/20
    paddle_image = games.load_image('paddle.png', transparent=True)

    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.paddle_image,
                                  x = 40,
                                  y = 50,
                                  is_collideable=True)

with this:

class Paddle(games.Sprite):
        HEIGHT = 480/20
        
        def __init__(self):
            self.paddle_image = games.load_image('paddle.png', transparent=True)

            super(Paddle, self).__init__(image = self.paddle_image,
                                      x = 40,
                                      y = 50,
                                      is_collideable=True)
  • Related