• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Zed McJack

My first game MyPong, finished at last

4 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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 :)!

1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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?
 
1

Share this post


Link to post
Share on other sites
 
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.
0

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  
Followers 0