Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Game Scoring
#1
I am trying to write a small game like Pong but I can't seem to be able to correctly adjust the scoring system. There are several problems in the scoring, one of which is the fact that player1 keeps scoring when the ball hits the paddle? Moreover, the other problem is that the scoring is in an 'infinite loop' and won't stop.

I have highlighted the 'problem' code in blue.

Code:
import pygame, sys
import random
import math
pygame.init()

#Screen Width/Height
screen = pygame.display.set_mode((800,600))

#Background Image
background_image = pygame.image.load("tennis.jpg").convert()

#Score Font
pygame.font.init()
font = pygame.font.Font("Bombing.TTF", 50)

#Graphics Font
graphicsScore = "Game Score!"
graphicsFont = pygame.font.Font("Bombing.TTF", 50)

#Paddle Player1
paddle_player1 = pygame.Rect(5,50,5,60)
player1Score = 0

#Paddle Player2
paddle_player2 = pygame.Rect(790,50,5,60)
player2Score = 0

#Ball
ball = pygame.Rect(300,200,20,20)
ballAngle = math.radians(0)
ballSpeed = 10
ballDirection = -1

#frame
clock = pygame.time.Clock()

#Process Player Input
while True:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           pygame.quit()
           sys.exit()

           timeChange = pygame.time.get_ticks() - totalTime
           totalTime = pygame.time.get_ticks()
           
       player1_up = pygame.key.get_pressed()[pygame.K_w] 
       player1_down = pygame.key.get_pressed()[pygame.K_s]

       player2_up = pygame.key.get_pressed()[pygame.K_UP]
       player2_down = pygame.key.get_pressed()[pygame.K_DOWN]
       
#Updating Game State Logic
       if player1_up:
           paddle_player1.y += -50 
       if player1_down:
           paddle_player1.y += 50 
       if player2_up:
           paddle_player2.y += -50 
       if player2_down:
           paddle_player2.y += 50 

       if paddle_player1.y < 0:
           paddle_player1.y = 0
       if paddle_player2.y < 0:
           paddle_player2.y = 0

       if paddle_player1.y > screen.get_height() - paddle_player1.height:
           paddle_player1.y = screen.get_height() - paddle_player1.height
       if paddle_player2.y > screen.get_height() - paddle_player2.height:
           paddle_player2.y = screen.get_height() - paddle_player2.height

#Update Ball
   ball.x += ballDirection * ballSpeed * math.cos(ballAngle)
   ball.y += ballDirection * ballSpeed * math.sin(ballAngle)
  if ball.x < 0 or ball.x > 800:
       ballSpeed *= 1
       if ball.x > 40:
           player1Score += 1
       if ball.x < 0:
           player2Score += 1
   #if player1Score >= 11 or player2Score >= 11:
              #player1Score = 0
              #player2Score = 0

#Paddle Collision
   if ball.colliderect(paddle_player1):
           if ball.x < 0:
               ballDirection *= -1
               pong = pygame.mixer.music.load('serve.wav')
               pygame.mixer.music.play()
   if ball.colliderect(paddle_player2):
           if ball.x > 40:
               ballDirection *= -1
               pong = pygame.mixer.music.load('serve.wav')
               pygame.mixer.music.play()
           

#Rendering
   screen.blit(background_image, [0,0])
   screen.blit(font.render(str(player1Score), 1, (178,34,34)),(200, 25))
   screen.blit(font.render(str(player2Score), 1, (178,24,24)),(600, 25))
   screen.blit(font.render(str(graphicsScore), 1, (245,255,250)), (330, 25))
   pygame.draw.rect(screen,(0,0,0), paddle_player1)
   pygame.draw.rect(screen,(0,0,0), paddle_player2)
   pygame.draw.rect(screen,(0,255,0), ball)
   
   clock.tick(50)
   pygame.display.flip()
Reply
#2
Quote:metulburr wrote 2 hours ago:
put code in code tags, removed formatting of blue font -> line 76

Thank-you metulburr.
Reply
#3
I have now found a solution to the problems above (first thread), moreover, I have a new 'small' problem that needs to be fixed. The problem is as followed, after the ball has been reset, the ball seems to go through the paddle of both players.

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020