Advertisement Jump to content
Sign in to follow this  
vinchi9

Code review - Pong

This topic is 1784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm beginnner game developer and I've just completed a game PONG. Can you review the code and tell me what I should do better, please?

I am working with Python 2.7 and PyGame 1.9.1

Thank you and sorry about my english smile.png

import pygame, sys, time
from pygame.locals import *

pygame.init()

windowSize = (800,500)
SURFACE = pygame.display.set_mode(windowSize, RESIZABLE,32)

class ball:
	def __init__(self,position,direction="SE",speed=1,ballRadius=10):
		self.originalPos = (position[0],position[1])
		self.pos = position
		self.originalDirection = direction
		self.direction = direction
		self.originalSpeed = speed
		self.speed = speed
		self.ballRadius = ballRadius

	def move(self):
		global windowSize, player1, player2, bumpSound
		for i in range(self.speed):
			if self.direction == "SE":
				if self.pos[1]+1+self.ballRadius >= windowSize[1]:
					self.direction = "NE"
					bumpSound.play()
					break
				elif self.pos[0]+1+self.ballRadius >= windowSize[0] or (self.pos[0]+1+self.ballRadius == player2.padPos[0] and self.pos[1]+1+self.ballRadius > player2.padPos[1] and self.pos[1]+1-self.ballRadius < player2.padPos[1]+player2.padHeight):
					self.direction = "SW"
					bumpSound.play()
					break
				else:
					self.pos[0] += 1
					self.pos[1] += 1

			elif self.direction == "NE":
				if self.pos[1]-1-self.ballRadius <= 0:
					self.direction = "SE"
					bumpSound.play()
					break
				elif self.pos[0]+1+self.ballRadius >= windowSize[0] or (self.pos[0]+1+self.ballRadius == player2.padPos[0] and self.pos[1]+1+self.ballRadius > player2.padPos[1] and self.pos[1]+1-self.ballRadius < player2.padPos[1]+player2.padHeight):
					self.direction = "NW"
					bumpSound.play()
					break
				else:
					self.pos[0] += 1
					self.pos[1] -= 1

			elif self.direction == "NW":
				if self.pos[1]-1-self.ballRadius <= 0:
					self.direction = "SW"
					bumpSound.play()
					break
				elif self.pos[0]-1-self.ballRadius <= 0 or (self.pos[0]-1-self.ballRadius == player1.padPos[0]+player1.padWidth and self.pos[1]-1+self.ballRadius > player1.padPos[1] and self.pos[1]-1-self.ballRadius < player1.padPos[1]+player1.padHeight):
					self.direction = "NE"
					bumpSound.play()
					break
				else:
					self.pos[0] -= 1
					self.pos[1] -= 1

			elif self.direction == "SW":
				if self.pos[1]+1+self.ballRadius >= windowSize[1]:
					self.direction = "NW"
					bumpSound.play()
					break
				elif self.pos[0]-1-self.ballRadius <= 0 or (self.pos[0]-1-self.ballRadius == player1.padPos[0]+player1.padWidth and self.pos[1]-1+self.ballRadius > player1.padPos[1] and self.pos[1]-1-self.ballRadius < player1.padPos[1]+player1.padHeight):
					self.direction = "SE"
					bumpSound.play()
					break
				else:
					self.pos[0] -= 1
					self.pos[1] += 1

			if player1.isInGoal(self.pos) or player2.isInGoal(self.pos):
				global frameID
				time.sleep(1)
				self.pos = [self.originalPos[0],self.originalPos[1]]
				self.direction = self.originalDirection
				self.speed = self.originalSpeed
				frameID = 0
				break

	def render(self):
		global SURFACE
		pygame.draw.rect(SURFACE, (255,255,255), (self.pos[0]-self.ballRadius, self.pos[1]-self.ballRadius, self.ballRadius*2, self.ballRadius*2))

class player:
	def __init__(self,padPos,goalPos):
		global windowSize
		self.padPos = padPos
		self.goalPos = goalPos
		self.goalSize = [30,windowSize[1]]
		self.loses = 0
		self.moveSpeed = 8
		self.padHeight, self.padWidth = 100,30

	def isInGoal(self,coords):
		if coords[0] > self.goalPos[0] and coords[0] < self.goalPos[0]+self.goalSize[0] and coords[1] > self.goalPos[1] and coords[1] < self.goalPos[1]+self.goalSize[1]:
			self.loses += 1
			return True
		else:
			return False

	def move(self,direction):
		global windowSize
		for i in range(self.moveSpeed):
			if direction == "up" and self.padPos[1]-1 > 0:
				self.padPos[1] -= 1
			elif direction == "down" and self.padPos[1]+1+self.padHeight < windowSize[1]:
				self.padPos[1] += 1

	def render(self):
		global SURFACE
		pygame.draw.rect(SURFACE, (255,255,255), (self.padPos[0], self.padPos[1], self.padWidth, self.padHeight))

class scoreBoard:
	def __init__(self,position):
		self.pos = position
		self.font = pygame.font.SysFont("Verdana", 50)

	def render(self):
		global SURFACE, player1, player2, windowSize
		pygame.draw.rect(SURFACE, (255,255,255), (self.pos[0]-5, 0, 10, windowSize[1]))
		SURFACE.blit(self.font.render(str(player2.loses),1,(255,255,255)),[self.pos[0]-80,self.pos[1]])
		SURFACE.blit(self.font.render(str(player1.loses),1,(255,255,255)),[self.pos[0]+50,self.pos[1]])



FPS = 60
fpsClock = pygame.time.Clock()

pygame.mixer.init()
bumpSound = pygame.mixer.Sound("BIP.ogg")
bumpSound.set_volume(1.0)

ball = ball([windowSize[0]/2,0],speed=3,ballRadius=8,direction="SE")
player1 = player([20,windowSize[1]/2],[25,0])
player2 = player([windowSize[0]-50,windowSize[1]/2],[windowSize[0]-57,0])
scoreBoard = scoreBoard([windowSize[0]/2,0])

SURFACE.fill((0,0,0))
player1Move, player2Move = False, False
frameID = 0

while True:
	frameID += 1
	if frameID == FPS*ball.speed*3:
		frameID = 0
		ball.speed += 1
	SURFACE.fill((0,0,0))
	scoreBoard.render()
	ball.move()
	ball.render()
	player1.render()
	player2.render()

	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()
		elif event.type == KEYDOWN:
			if pygame.key.name(event.key) == "up":
				player2Move = "up"
			elif pygame.key.name(event.key) == "down":
				player2Move = "down"
			elif pygame.key.name(event.key) == "d":
				player1Move = "up"
			elif pygame.key.name(event.key) == "c":
				player1Move = "down"
		elif event.type == KEYUP:
			if pygame.key.name(event.key) == "up":
				player2Move = False
			elif pygame.key.name(event.key) == "down":
				player2Move = False
			elif pygame.key.name(event.key) == "d":
				player1Move = False
			elif pygame.key.name(event.key) == "c":
				player1Move = False

	if player1Move:
		player1.move(player1Move)
	if player2Move:
		player2.move(player2Move)

	pygame.display.update()
	fpsClock.tick(FPS)

Share this post


Link to post
Share on other sites
Advertisement

You'd probably be better off having a ball.velocity with separate x and y components (initially +1, +1), rather than the self.direction string.  This will greatly simplify your code in ball.move.

 

Also, it's maybe not essential in a small program like this, but some comments would be nice.

Edited by Geoffrey

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!