Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scoring issues in pong
#1
I have tinkered with this for too long every time I run the code it doesn't update the value of my variables that control score. any help is much appreciated.
import sys
import pygame
import os

pygame.init()


#window and scoring
win_w = 600
win_h = 500
white= (255, 255, 255)
bg = pygame.image.load("BG.png")

win = pygame.display.set_mode((win_w,win_h))


pygame.display.set_caption("pong?")


win.blit(pygame.image.load('BG.png'),(0, 0))
##scoring
font = pygame.font.SysFont("comicsansms", 30)
score1 = 0
pygame.display.flip()

score2 = 0
text1 = font.render(str(score1) , True, (255, 255, 255))
text2 = font.render(str(score2) , True, (255, 255, 255))


pygame.display.flip()

#paddles
xp1 = 575
yp1 = 200
xp2 = 10
yp2 = 200
#ball
x1 = 305
y1 = 245
ball = [x1, y1]
xvel = 1
yvel = 1


while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()



#keys and paddle movement
            
    keys = pygame.key.get_pressed()

    if keys[pygame.K_UP] and yp1 > 0 :
        yp1 -= 1.5     
    if keys[pygame.K_DOWN] and yp1 < 435:
        yp1 += 1.5
    if keys[pygame.K_w] and yp2 > 0:
        yp2 -= 1.5
    if keys[pygame.K_s] and yp2 < 435:
        yp2 += 1.5


            
#ball movement

    if x1 > 600:
        score1 += 1
        x1 = 305
        y1 = 245
        xvel *= -1
        yvel *= 1

        

    if x1 < 0:
        score2 += 1
        x1 = 305
        y1 = 245
        xvel *= -1
        yvel *= 1
        
    
    if y1 >= 490:
        yvel *= -1

    if y1 <= 10:
        yvel *= -1

#paddle collison

        
    if x1 >= 560 and (y1 < yp1 +65 and y1 > yp1):
        x1 == 559
        xvel *= -1
        yvel *= 1


    if x1 <= 25 and (y1 < yp2 +65 and y1 > yp2):
        x1 == 26
        xvel *= -1
        yvel *= 1

    win.blit(pygame.image.load('BG.png'),(0, 0))
    win.blit(text1,
        (20 - text1.get_width() // 2, 20 - text1.get_height() // 2))
    win.blit(text2,
        (580 - text2.get_width() // 2, 20 - text2.get_height() // 2))

#score

#fuck the score till later
    
#paddles
    pygame.draw.rect(win, (255, 255, 255), (xp1, yp1, 15, 65))       
    pygame.draw.rect(win, (255, 255, 255), (xp2, yp2, 15, 65))
#ball
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, 15, 15))

    pygame.display.update()

    x1 += xvel

    y1 += yvel   
Reply
#2
You should read through my series of tutorials. You have bottlenecks (loading your images in the main game loop)
Quote:
    win.blit(pygame.image.load('BG.png'),(0, 0))

and magic numbers all over the place.
Quote:
#ball movement
 
    if x1 > 600:
        score1 += 1
        x1 = 305
        y1 = 245
        xvel *= -1
        yvel *= 1
 
         
 
    if x1 < 0:
        score2 += 1
        x1 = 305
        y1 = 245
        xvel *= -1
        yvel *= 1
         
     
    if y1 >= 490:
        yvel *= -1
 
    if y1 <= 10:
        yvel *= -1
 
#paddle collison
 
         
    if x1 >= 560 and (y1 < yp1 +65 and y1 > yp1):
        x1 == 559
        xvel *= -1
        yvel *= 1
 
 
    if x1 <= 25 and (y1 < yp2 +65 and y1 > yp2):
        x1 == 26
        xvel *= -1
        yvel *= 1

You are not using pygame rects for position and drawing at all making it spaghetti code. So its makes it more time consuming to follow flow, in which time i do not have.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send a pong on websocket-client tomtom 0 3,606 Aug-15-2022, 05:58 AM
Last Post: tomtom
  Problem with my pong game code Than999 8 3,826 May-15-2022, 06:40 AM
Last Post: deanhystad
  Pong game buss0140 7 4,059 Dec-27-2020, 07:04 AM
Last Post: ndc85430
  PING PONG GAME akea 0 5,675 May-08-2019, 04:30 PM
Last Post: akea
  Python project "pong" without any makefile sylas 5 4,962 Nov-28-2017, 05:55 PM
Last Post: Larz60+
  ping and pong run both well sylas 1 3,159 Sep-24-2017, 05:14 PM
Last Post: sylas

Forum Jump:

User Panel Messages

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