Sign in to follow this  
Zed McJack

My first game MyPong, finished at last

Recommended Posts

Zed McJack    349
Ok guys, it lasted more than 2 moths, but at last I finished my first game.
It was actually finished month ago but I lost the endurance to sit and split the game in several files. So I did it today and now its here for review.

I am not new to programming, but I am new to Python and game development. So I would like to hear whats ok and whats not ok and how to do it right.

The game was written in my language and I translated variables, methods and class names to English an hour ago so you might notice some weird expressions.

I started to pass object properties that I needed in class methods but switched to passing whole object, so you will see the difference. I am not sure is proper way to do it and is it efficient, but..

The game intentionally does not use sprites and images as I wanted to see is it possible to make this simple game without using images.

Thanks

PS For some reason I am not allowed to upload files so here is the code.

File: MyPong.py
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
from Pad import Pad
from Ball import Ball
from Text import Text
from Game import Game
				
#Creating instances
game = Game()
my_pad = Pad(0, 0, 100, 20, [255, 0, 255])
my_pad.rect.topleft = (100,430)
ai_pad = Pad(0, 0, 100, 20, [255, 100, 100])
ai_pad.rect.topleft=(0, 30)
ball = Ball(10,[0, 100, 50])
text_pause = Text("PAUSE", (255,255,255), None, game.background.get_rect().center, 100)
my_result = Text("Player: %s" % my_pad.points, (150,255,0), (0, 0), (50,50), 30)
ai_result = Text("AI: %s" % ai_pad.points, (150,255,0), (game.width-50, 0), (50,50), 30)

while game.all_ok:
	game.update(my_pad, ai_pad, ball)
	
	if game.pause:
		if game.game_over:
			text_pause = Text("GAME OVER - The winner is: %s" % game.winner, (255,255,255), None, game.background.get_rect().center, 50)
		game.screen.blit(text_pause.text, text_pause.poz)
	else:
		my_pad.calculate_position(game.width)
		ai_pad.calculate_ai(ball.rect.center, game.width)
		ball.calculate_position(game, my_pad, ai_pad, my_result, ai_result)
		my_pad.rect.left+=my_pad.movement
		game.background.fill(game.background_color)
		game.screen.blit(game.background, game.background_rect)	
		game.screen.blit(my_result.text, my_result.poz)
		game.screen.blit(ai_result.text, ai_result.poz)
		game.screen.blit(my_pad.picture, my_pad.rect)
		game.screen.blit(ai_pad.picture, ai_pad.rect)
		game.screen.blit(ball.picture, ball.rect)
		game.clock.tick(game.refresh_rate)

	
	

	pygame.display.flip()

File: Pad.py
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *

class Pad(object):
	def __init__(self, x, y, width, height, color):
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.ball_speed = 3
		self.pad_direction = 0
		self.movement = 0
		self.points = 0
		self.color = color
		self.picture, self.rect = self.draw_rectangle()

	def draw_rectangle(self):
		rectangle = pygame.Rect(self.x, self.y, self.width, self.height)
		picture = pygame.Surface(( self.width, self.height))
		pygame.draw.rect(picture, self.color , rectangle)
		return picture , picture.get_rect()
	def calculate_position(self, width):
		#Calculate pad position if its in the screen
		if self.rect.left>=0 and self.rect.left+self.rect.width<=width:
			self.movement = self.ball_speed * self.pad_direction
		#If ball is out of the left border
		elif self.rect.left < 0:
			self.rect.left = 0
			self.movement = 0
			self.pad_direction = 0
		#If ball is out of the right border
		elif self.rect.left+self.rect.width>width:
			self.rect.left = width - self.rect.width
			self.movement = 0
			self.pad_direction = 0
	def calculate_ai(self, ball_position, width):
		diff = abs(ball_position[0]-self.rect.center[0])
		if self.rect.center[0] > ball_position[0]: 
			if self.rect.left < 0:
				self.rect.left = 0
			else:
				self.rect.centerx -=3
		else:
			if self.rect.right > width:
				self.rect.right = width
			self.rect.centerx +=3

File: Ball.py
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
from random import randint

class Ball(object):
	def __init__(self, radius, color):
		self.directionX = -1
		self.directionY = -1
		self.positionX=randint(100,540)
		self.positionY=randint(100,380)
		self.radius = radius
		self.color = color
		self.ball_speed = 4
		self.picture, self.rect = self.draw_circle(self.radius, self.color)
		self.picture.set_colorkey((0,0,0))
		#Initial positioning is random
		self.rect.center = self.positionX, self.positionY
	
	def draw_circle(self, radius, color):
		size = radius*2
		picture = pygame.Surface((size, size))
		pygame.draw.circle(picture, color, (radius, radius), radius)
		return picture, picture.get_rect()
		
	def calculate_position(self, game, my_pad, ai_pad, my_result, ai_result):
		if not game.goal:
			if self.rect.top <= 0:
				my_pad.points +=1
				my_result.text = my_result.font.render("Player: %s" % my_pad.points, 1, (150,255,0))
				game.goal = 1
			elif self.rect.bottom > game.height:
				game.goal = 1
				ai_pad.points +=1
				ai_result.text = ai_result.font.render("AI: %s" % ai_pad.points, 1, (150,255,0))
			if ai_pad.points == 11 or my_pad.points == 11:
				if ai_pad.points == 11:
					game.winner = "AI"
				else:
					game.winner = "Player"
				game.game_over = 1
				game.pause = 1

			#If the ball is out of the right border, corection and changing the direction X
			if self.positionX > game.width-self.radius:
				self.positionX = self.positionX - self.ball_speed
				self.directionX = -1
			#If the ball is out of the left border, corection and direction change
			if self.positionX < 0+self.radius:
				self.positionX = self.positionX + self.ball_speed
				self.directionX = 1
		elif self.rect.bottom < 0 or self.rect.top > game.height:
			game.goal = 0
			self.positionX = randint(100,540)
			self.positionY = randint(100,380)
			if randint(0,1):

				self.directionY = 1
			else:
				self.directionY = -1

			
		#updating new position
		self.positionX += self.ball_speed * self.directionX
		self.positionY += self.ball_speed * self.directionY
		self.rect.center = self.positionX, self.positionY


File: Text.py
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *

class Text(object):
	def __init__(self, text, color, position, center, size):
		self.font = pygame.font.Font(None,size)
		self.text = text
		self.color = color
		self.text = self.font.render(self.text, 1, self.color)
		self.poz = self.text.get_rect()
		self.poz.center = center
		if position <> None:
			self.poz.topleft = position
		self.picture = pygame.Surface((self.poz.width, self.poz.height))
 

File: Game.py
 
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *

class Game(object):
	def __init__(self):
		pygame.init()
		self.width, self.height = 640, 480
		self.screen_resolution = (self.width, self.height)
		if pygame.display.mode_ok(self.screen_resolution):
			self.screen = pygame.display.set_mode(self.screen_resolution)
		self.refresh_rate = 40
		self.clock = pygame.time.Clock()
		self.pause = 0
		self.goal = 0
		self.all_ok = 1
		self.background_color = [0, 0, 0]
		self.background = pygame.Surface((self.width, self.height))
		self.background_rect = self.background.get_rect()
		self.game_over = 0
		self.winner = ""

	def update(self, my_pad, ai_pad, ball):
		keys = pygame.key.get_pressed()
		if keys[K_LEFT] or keys[K_RIGHT]:
			if keys[K_LEFT]:
				my_pad.pad_direction = -1
			elif keys[K_RIGHT]:
				my_pad.pad_direction = 1
		else:
			my_pad.pad_direction = 0

		events = pygame.event.get()
	
		for event in events:
			if event.type == QUIT or (event.type == KEYDOWN and event.key in  [K_ESCAPE, K_q]):
				#quiting
				self.all_ok = 0
			elif event.type ==KEYDOWN and event.key == K_SPACE:
				if self.pause == 0:
					self.pause =1
				else:
					self.pause = 0

		if not self.pause:
			mylist = (my_pad.rect, ai_pad.rect)
			colision = ball.rect.collidelist(mylist)
			
			if colision == 0: # Ball hit the pad

				#from above
				if ball.rect.top < my_pad.rect.top and ball.rect.bottom > my_pad.rect.top:
					ball.directionY = ball.directionY * -1
					ball.positionY = ball.positionY - ball.ball_speed
				#from side
				elif ball.rect.top < my_pad.rect.bottom:
						ball.directionX = ball.directionX * -1
						ball.positionX = ball.positionX - ball.ball_speed
			elif colision == 1:

				if ball.rect.top <= ai_pad.rect.bottom: 

					ball.directionY = ball.directionY * -1
					ball.positionY = ball.positionY + ball.ball_speed

Share this post


Link to post
Share on other sites
superman3275    2061

If you can, try to include a Zip file with all the source files and another Zip with the game's Execution (.Exe) file.

 

I believe you are on the right track. I'm learning Python (While programming Hooded in C++), so I don't believe I'm qualified to give critique.

 

You can take a lot of different paths from here, Pong is the building block and you now know far more about development:

  • Make some more Knock-Offs (Breakout, Space Invaders, Tetris)
  • Embark on your own game :)!
  • Learn your language even more in depth!

I recommend looking into learning another language (JavaScript) / facet in programming (HTML) on the side to help you. It really helps when you can go to Code-Academy and take some quick excercises whenever you lose motivation or need to take a break. You're still being productive, however it's a nice breath of fresh air to go from programming a Map System in Python / C++ to making some projects / games in JavaScript :)!

Share this post


Link to post
Share on other sites
Zed McJack    349
If you can, try to include a Zip file with all the source files and another Zip with the game's Execution (.Exe) file.
Good idea! I should have thought about it myself.
I believe you are on the right track. I'm learning Python (While programming Hooded in C++), so I don't believe I'm qualified to give critique.
Thanks anyway, though I still need more people to look at the code.
You can take a lot of different paths from here, Pong is the building block and you now know far more about development:
  • Make some more Knock-Offs (Breakout, Space Invaders, Tetris)
  • Embark on your own game smile.png!
  • Learn your language even more in depth!
I recommend looking into learning another language (JavaScript) / facet in programming (HTML) on the side to help you. It really helps when you can go to Code-Academy and take some quick excercises whenever you lose motivation or need to take a break. You're still being productive, however it's a nice breath of fresh air to go from programming a Map System in Python / C++ to making some projects / games in JavaScript smile.png!
 
 


Well, I thought about starting my game, but decided to try out my hands first at breakout.
I did some coding in JavaScript and I code regularly in VFP (database programing), but this is my new vice.

Thanks

Share this post


Link to post
Share on other sites
kidman171    498

Hello, congrats on finishing your first game!

 

Here are a few things I noticed about your game:

 

    1) The paddle speed is way too slow. If the ball goes on the other half of the screen, I have no chance to save it.

    2) The ball start's in the center and goes in a random direction very quickly, sometimes scoring against the AI or myself 3 or 4 times in succession.

         Increasing the paddle speed may give a chance to save the ball in this scenario.

    3) There are a few things that might make your code cleaner, for example:

[code] 

if ai_pad.points == 11 or my_pad.points == 11:
if ai_pad.points == 11:
game.winner = "AI"
else:
game.winner = "Player"
game.game_over = 1
game.pause = 1if ai_pad.points == 11 or my_pad.points == 11:
if ai_pad.points == 11:
game.winner = "AI"
else:
game.winner = "Player"
game.game_over = 1
game.pause = 1
[/code]
      This is under the calculate_position function in ball.py. However, this piece of code is calculating the winner, not the position. Putting it in its own method called something like determine_winner()  could help you find errors easier because you will know right where your code is that determines the winner.
      Also, the speed for the pad is controlled by a variable called ball_speed. Don't you think you should call this variable pad_speed?
 

Share this post


Link to post
Share on other sites
Zed McJack    349
 
Hello, congrats on finishing your first game!
Thanks
 
Here are a few things I noticed about your game:
 
    1) The paddle speed is way too slow. If the ball goes on the other half of the screen, I have no chance to save it.
    2) The ball start's in the center and goes in a random direction very quickly, sometimes scoring against the AI or myself 3 or 4 times in succession.
         Increasing the paddle speed may give a chance to save the ball in this scenario.
    3) There are a few things that might make your code cleaner, for example:
Code removed 
      This is under the calculate_position function in ball.py. However, this piece of code is calculating the winner, not the position. Putting it in its own method called something like determine_winner()  could help you find errors easier because you will know right where your code is that determines the winner.
      Also, the speed for the pad is controlled by a variable called ball_speed. Don't you think you should call this variable pad_speed?
 
1. You are right about paddle speed. I left it as it is now cause I wanted to finish game, it took me too long to actually finish it. Otherwise I could do more improvements. But anyway thanks.
2. Same as above. Its left as is, because of my lack of commitment.
3. You are again right I should move the code, probably to Game class.
4. Heh, At first I thought it was paddle speed and that I made mistake during translation, but its actually wrong variable name even in my language. Thanks man.

Well, the game could be improved in many ways, but its good to know what other people think and see.

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this