# 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 Ball import Ball
from Text import Text
from Game import Game

#Creating instances
game = Game()
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:

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:
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(ball.picture, ball.rect)
game.clock.tick(game.refresh_rate)

pygame.display.flip()


# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *

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.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:
#If ball is out of the left border
elif self.rect.left < 0:
self.rect.left = 0
self.movement = 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
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):
self.directionX = -1
self.directionY = -1
self.positionX=randint(100,540)
self.positionY=randint(100,380)
self.color = color
self.ball_speed = 4
self.picture.set_colorkey((0,0,0))
#Initial positioning is random
self.rect.center = self.positionX, self.positionY

picture = pygame.Surface((size, size))
return picture, picture.get_rect()

if not game.goal:
if self.rect.top <= 0:
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_result.text = ai_result.font.render("AI: %s" % ai_pad.points, 1, (150,255,0))
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
self.positionX = self.positionX - self.ball_speed
self.directionX = -1
#If the ball is out of the left border, corection and direction change
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 = ""

keys = pygame.key.get_pressed()
if keys[K_LEFT] or keys[K_RIGHT]:
if keys[K_LEFT]:
elif keys[K_RIGHT]:
else:

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:
colision = ball.rect.collidelist(mylist)

if colision == 0: # Ball hit the pad

#from above
ball.directionY = ball.directionY * -1
ball.positionY = ball.positionY - ball.ball_speed
#from side
ball.directionX = ball.directionX * -1
ball.positionX = ball.positionX - ball.ball_speed
elif colision == 1:

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


##### 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 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 !
• 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 !

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 on other sites
kidman171    498

Hello, congrats on finishing your first 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]

game.winner = "AI"
else:
game.winner = "Player"
game.game_over = 1
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 on other sites
Zed McJack    349

Hello, congrats on finishing your first game!
Thanks

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.