Pong in Pygame won’t count score

Tags: ,

This is what I’ve got set-up, and I don’t want to be hand-fed the code as this is for a school project. I am still working on collision but I am not sure why the score being displayed isn’t being updated. I tried to set up the score update as a Dot method and then implement that method into the Game class then use the update method to try and update the displayed scores to show the new scores in the event that the ball hits either edge.

# Pong version 2
# Program that displays two immovable paddles and a single ball that bounces around
# the displayed window
# Utilizes str, int, function, Game, Paddle, Ball, module, bool, tuple types
# Includes assingment, expression and import statements

import pygame
import random

def main():
   # initialize all pygame modules (some need initialization)
   window_height = 700
   window_length = 1000
   # create a pygame display window
   pygame.display.set_mode((window_length, window_height))
   # set the title of the display window
   pygame.display.set_caption('Pong 2')   
   # get the display surface
   w_surface = pygame.display.get_surface() 
   # create a game object
   game = Game(w_surface)
   # start the main game loop by calling the play method on the game object
   # quit pygame and clean up the pygame window

class Game:
   # An object in this class represents a complete game.

   def __init__(self, surface):
      # Initialize a Game.
      # - self is the Game to initialize
      # - surface is the display window surface object

      # === objects that are part of every game that we will discuss
      self.surface = surface
      self.bg_color = pygame.Color('black')
      self.FPS = 60
      self.clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True
      self.distance_edge = 100

      # === game specific objects
      self.small_dot = Dot('white', 10, [450, 350], [3, 1], self.surface)
      self.player1 = Paddle(self.surface, -1, [self.distance_edge, self.surface.get_height()/2 - 40,10,80])
      self.player2 = Paddle(self.surface, -1, [self.surface.get_width() - self.distance_edge - 10, self.surface.get_height()/2 - 40,10,80])
      self.score1 = 0
      self.score2 = 0    

   def play(self):
      # Play the game until the player presses the close box.
      # - self is the Game that should be continued or not.

      while not self.close_clicked:  # until player clicks close box
         # play frame
         if self.continue_game:
         self.clock.tick(self.FPS) # run at most with FPS Frames Per Second 

   def handle_events(self):
      # Handle each user event by changing the game state appropriately.
      # - self is the Game whose events will be handled

      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True

   def draw(self):
      # Draw all game objects.
      # - self is the Game to draw

      self.surface.fill(self.bg_color) # clear the display surface first
      pygame.display.update() # make the updated surface appear on the display

   def display_scores(self):
      # in white on the window's background.
      # - self is the Game to draw for.

      score_font = pygame.font.Font(None, 74)
      score_text = score_font.render(str(self.score1), 1, pygame.Color('white'))
      self.surface.blit(score_text, (0,0))
      score_text = score_font.render(str(self.score2), 1, pygame.Color('white'))
      self.surface.blit(score_text, (950,0))

   def update(self):
      # Update the game objects for the next frame.
      # - self is the Game to update

      self.score1 = self.small_dot.score1_update(self.score1)
      self.score2 = self.small_dot.score2_update(self.score2)

class Paddle:

   def __init__(self, surface, colour, dimensions):

      self.surface = surface
      self.colour = colour
      self.dimensions = dimensions

   def draw(self):
      pygame.draw.rect(self.surface, self.colour, self.dimensions)

class Dot:
   # An object in this class represents a Dot that moves

   def __init__(self, dot_color, dot_radius, dot_center, dot_velocity, surface):
      # Initialize a Dot.
      # - self is the Dot to initialize
      # - color is the pygame.Color of the dot
      # - center is a list containing the x and y int
      #   coords of the center of the dot
      # - radius is the int pixel radius of the dot
      # - velocity is a list containing the x and y components
      # - surface is the window's pygame.Surface object

      self.color = pygame.Color(dot_color)
      self.radius = dot_radius
      self.center = dot_center
      self.velocity = dot_velocity
      self.surface = surface

   def move(self):
      # Change the location of the Dot by adding the corresponding 
      # speed values to the x and y coordinate of its center
      # - self is the Dot
      size = self.surface.get_size()

      for point in range(len(size)):
         self.center[point] = (self.center[point] + self.velocity[point])
         left_top_bounce = (self.center[point] - self.radius) < 0 
         right_bottom_bounce = (self.center[point] + self.radius) > size[point]
         if left_top_bounce or right_bottom_bounce:
            self.velocity[point] *= -1

   def draw(self):
      # Draw the dot on the surface
      # - self is the Dot

      pygame.draw.circle(self.surface, self.color, self.center, self.radius)   

   def score1_update(self, score1):

      size = self.surface.get_size()
      for point in range(len(size)):
         self.center[point] = (self.center[point] + self.velocity[point])      
         if self.center[point] >= 990:
            score1 += 1
      return score1

   def score2_update(self, score2):

      size = self.surface.get_size()
      for point in range(len(size)):
         self.center[point] = (self.center[point] + self.velocity[point])      
         if self.center[0] <= 6:
            score2 += 1
      return score2

   def collision(self):



Look at your Game.update() method. Right now it looks like this:

def update(self):
  # Update the game objects for the next frame.
  # - self is the Game to update


Those last two lines are just statements. You’re not changing the values of those scores using assignment, addition, subtraction or anything else.

If I were you I’d consider using your score1_update() and score2_update() methods in here. Of course, since those methods both start from zero, you’ll need to be sure to add their results to the scores, not just assign the scores to the results of those methods.

Source: stackoverflow