I'm learning pygame and followed a tutorial about a pong game.
Everything is from the tutorial except the score system, I implemented.
It does work, but instead that the score is updated, the new score is printed over the previous score.. Which isn't a nice view. How can I solve this?

(I think only the main function is important for this problem, but I'm giving whole the code to be sure)

#!/usr/bin/python

try:
	import sys
	import random
	import math
	import os
	import getopt
	import pygame
	from socket import *
	from pygame.locals import *
except ImportError, err:
	print "couldn't load module. %s" % (err)
	sys.exit(2)

def load_png(name):
	"""Load image and return image object"""
	fullname = os.path.join('data',name)
	try:
		image=pygame.image.load(fullname)
		if image.get_alpha() is None:
			image = image.convert()
		else:
			image = image.convert_alpha()
	except pygame.error, message:
		print 'Cannot load image:', fullname
		raise SystemExit,message
	return image, image.get_rect()

class Ball(pygame.sprite.Sprite):
	def __init__(self,(xy),vector):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = load_png("ball.png")
		screen = pygame.display.get_surface()
		self.area = screen.get_rect()
		self.vector = vector
		self.hit = 0
	
	def update(self):
		newpos = self.calcnewpos(self.rect,self.vector)
		self.rect = newpos
		(angle,z) = self.vector
		
		if not self.area.contains(newpos):
			tl = not self.area.collidepoint(newpos.topleft)
			tr = not self.area.collidepoint(newpos.topright)
			bl = not self.area.collidepoint(newpos.bottomleft)
			br = not self.area.collidepoint(newpos.bottomright)
			if tr and tl or (br and bl):
				angle = -angle
			if tl and bl:
				angle = math.pi - angle
				player2.score+=1
			if tr and br:
				angle = math.pi - angle
				player1.score+=1
		else:
			player1.rect.inflate(-3,-3)
			player2.rect.inflate(-3,-3)
			if self.rect.colliderect(player1.rect) == 1 and not self.hit:
				angle = math.pi - angle
				self.hit = not self.hit
			elif self.rect.colliderect(player2.rect) == 1 and not self.hit:
				angle = math.pi - angle
				self.hit = not self.hit
			elif self.hit:
				self.hit = not self.hit
		self.vector = (angle,z)
	
	def calcnewpos(self,rect,vector):
		(angle,z)=vector
		(dx,dy)=(z*math.cos(angle),z*math.sin(angle))
		return rect.move(dx,dy)

class Bat(pygame.sprite.Sprite):
	def __init__(self, side):
		pygame.sprite.Sprite.__init__(self)
		self.image,self.rect = load_png('bat.png')
		screen = pygame.display.get_surface()
		self.area = screen.get_rect()
		self.side=side
		self.speed=10
		self.score=0
		self.state="still"
		self.reinit()
	
	def reinit(self):
		self.state="still"
		self.movepos=[0,0]
		if self.side=="left":
			self.rect.midleft=self.area.midleft
		elif self.side == "right":
			self.rect.midright = self.area.midright
	
	def update(self):
		newpos = self.rect.move(self.movepos)
		if self.area.contains(newpos):
			self.rect=newpos
		pygame.event.pump()
	
	def moveup(self):
		self.movepos[1] = self.movepos[1] - (self.speed)
		self.state = "moveup"
	
	def movedown(self):
		self.movepos[1] = self.movepos[1] + (self.speed)
		self.state = "movedown"

def main():
	pygame.init()
	screen = pygame.display.set_mode((640,480))
	pygame.display.set_caption('Basic Pong')
	background = pygame.Surface(screen.get_size())
	background = background.convert()
	background.fill((255,255,255))
	global player1
	global player2
	player1 = Bat("left")
	player2 = Bat("right")
	speed=13
	rand = ((0.1 * (random.randint(5,8))))
	ball = Ball((0,0),(0.47,speed))
	playersprites = pygame.sprite.RenderPlain((player1,player2))
	ballsprite = pygame.sprite.RenderPlain(ball)
	screen.blit(background, (0,0))
	pygame.display.flip()
	font=pygame.font.Font(None,50)
	clock = pygame.time.Clock()
	while 1:
		clock.tick(60)
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			elif event.type == KEYDOWN:
				if event.key == K_a:
					player1.moveup()
				if event.key == K_z:
					player1.movedown()
				if event.key == K_UP:
					player2.moveup()
				if event.key == K_DOWN:
					player2.movedown()
			elif event.type == KEYUP:
				if event.key == K_a or event.key == K_z:
					player1.movepos = [0,0]
					player1.state="still"
				if event.key == K_UP or event.key == K_DOWN:
					player2.movepos = [0,0]
					player2.state="still"
		text=font.render(str(player1.score)+" | "+str(player2.score),1,(0,0,0))
		screen.blit(text, (320,10))
		screen.blit(background, ball.rect,ball.rect)
		screen.blit(background, player1.rect, player1.rect)
		screen.blit(background, player2.rect, player2.rect)
		ballsprite.update()
		playersprites.update()
		ballsprite.draw(screen)
		playersprites.draw(screen)
		pygame.display.flip()

if __name__ == '__main__': main()

In lines 151-154, I see you're only blitting the background over top of the paddles and the ball - not over the score.

Add this before line 151:

screen.blit(background, text.get_rect().move(320,10))

While it's more efficient only to blit over things that are live and in play, sometimes it's worth it just to blit the entire background over the field so you start with a fresh drawing surface every time. But do whatever works for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.