Creating a Simple Arcade Game in Python/Pygame
In this tutorial, we will create a Simple Arcade Game in Python/Pygame. Pygame is a Free and Open Source python programming language framework for making game applications. It is highly portable and runs on nearly every platform and operating system. It is highly recommended for beginners to learn Pygame when making basic games.
Our goal in this tutorial is to build a Simple Arcade Game that is a 1 player game. The game will have a 1 bouncing box 2 vertical blocks, 1 block is for the player and 1 is for the programmed enemy. We will make a simple scoring rule. In order for the player to score, the player should let the box to bounce in his/her block to pass the box back to the enemy. When the bouncing box will touch the player's end of his/her side, the score will be automatically restarted.
Getting Started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
After Python IDLE's is installed, install the pygame module. To do this, open the command prompt then type "python -m pip install pygame", and hit enter.

Note: I have 2 sample files that I will use in this tutorial. 1 is an otf file for the font and the other 1 is a sample background music of the game. I will include these files in the source code file.
Importing Modules
After setting up the installation, run the IDLE and click the file and then the new file. After that, a new window will appear containing a black file this will be the text editor for the python.
Then copy code that I provided below and paste it inside the IDLE text editor.
- import pygame, sys, os
- from pygame.locals import *
Writing The Variables
We will assign the certain variables that we will need to declare later in the main loop. To do that just kindly write this code inside your text editor.
- # Game Initialization
- pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=4096)
- pygame.init()
- # Center the Game Application
- os.environ['SDL_VIDEO_CENTERED']='1'
- # Game Resolution
- screen_width=800
- screen_height=600
- screen=pygame.display.set_mode((screen_width, screen_height))
- # Initialize Sound Files
- sound=pygame.mixer.Sound('sounds/sound.wav')
- sound.set_volume(.5)
- # Color
- black=(0, 0, 0)
- white=(255, 255, 255)
- blue=(0, 0, 255)
- # Fonts
- font="font/Arcade Future.otf"
- # Framerate
- clock=pygame.time.Clock()
- fps=200
- # Initial Variables
- lineWidth=10
- paddleSize=50
- paddleOffset=20
Declaring Methods
We will now declare some methods to make it easier to call it in the main functions. This contains several function that can be use all through.
- def text_format(text, textFont, textSize, textColor):
- font=pygame.font.Font(textFont, textSize)
- newText=font.render(text, 0, textColor)
- return newText
- def BackgroundGameplay():
- screen.fill(black)
- pygame.draw.line(screen, white, ((screen_width / 2), 0), ((screen_width / 2), screen_height), 2)
- pygame.draw.rect(screen, blue, ((0, 0), (screen_width, screen_height)), lineWidth)
- def Paddle(paddle):
- if paddle.bottom > screen_height - lineWidth:
- paddle.bottom = screen_height - lineWidth
- elif paddle.top < lineWidth:
- paddle.top = lineWidth
- pygame.draw.rect(screen, white, paddle)
- def Ball(ball):
- pygame.draw.rect(screen, white, ball)
- def BallMovement(ball, ballDirX, ballDirY):
- ball.x += ballDirX
- ball.y += ballDirY
- return ball
- def WallCollision(ball, ballDirX, ballDirY):
- if ball.top == (lineWidth) or ball.bottom == (screen_height - lineWidth):
- ballDirY = ballDirY * -1
- if ball.left == (lineWidth) or ball.right == (screen_width - lineWidth):
- ballDirX = ballDirX * -1
- return ballDirX, ballDirY
- def BallCollision(ball, paddle1, paddle2, ballDirX):
- if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
- return -1
- elif ballDirX == 1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
- return -1
- else:
- return 1
- def UpdateScore(paddle1, ball, score, ballDirX):
- if ball.left == lineWidth:
- return 0
- elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
- score += 1
- return score
- elif ball.right == screen_width - lineWidth:
- score=0
- return score
- else:
- return score
- def ScoreHandler(score):
- text=text_format("Score: %s" % (score), font, 24, white)
- textRect=text.get_rect()
- textRect.topleft=(50, 25)
- screen.blit(text, textRect)
- def EnemyMovement(ball, ballDirX, paddle2):
- if ballDirX == -1:
- if paddle2.centery < (screen_height / 2):
- paddle2.y += 1
- elif paddle2.centery > (screen_height / 2):
- paddle2.y -= 1
- # if ball moving towards bat, track its movement.
- elif ballDirX == 1:
- if paddle2.centery < ball.centery:
- paddle2.y += 1
- else:
- paddle2.y -= 1
- return paddle2
Main Function
This is the Main Code for the Pygame application. The code contains a several input function that will allow to act accordingly and render to the application. To do that just write this code inside the IDLE text editor.
- def main():
- ballPosX=screen_width/2 - lineWidth/2
- ballPosY=screen_height/2 - lineWidth/2
- playerPos=(screen_height-paddleSize)/2
- enemyPos=(screen_height-paddleSize)/2
- score=0
- ballDirX = -1
- ballDirY = -1
- paddle1=pygame.Rect(paddleOffset, playerPos, lineWidth, paddleSize)
- paddle2 = pygame.Rect(screen_width - paddleOffset - lineWidth, enemyPos, lineWidth, paddleSize)
- ball=pygame.Rect(ballPosX, ballPosY, lineWidth, lineWidth)
- BackgroundGameplay()
- Paddle(paddle1)
- Paddle(paddle2)
- Ball(ball)
- pygame.mouse.set_visible(0)
- sound.play(loops=-1)
- while True:
- for event in pygame.event.get():
- if event.type==QUIT:
- pygame.quit()
- sys.exit()
- elif event.type==MOUSEMOTION:
- mousex, mousey=event.pos
- paddle1.y=mousey
- BackgroundGameplay()
- Paddle(paddle1)
- Paddle(paddle2)
- Ball(ball)
- ball=BallMovement(ball, ballDirX, ballDirY)
- ballDirX, ballDirY=WallCollision(ball, ballDirX, ballDirY)
- score=UpdateScore(paddle1, ball, score, ballDirX)
- ballDirX = ballDirX * BallCollision(ball, paddle1, paddle2, ballDirX)
- paddle2=EnemyMovement(ball, ballDirX, paddle2)
- ScoreHandler(score)
- pygame.display.set_caption('Python - Pygame Simple Arcade Game')
- pygame.display.update()
- clock.tick(fps)
Initializing The Game
This is the Initialization code, this will run the entire code when the application starts. It will render the Main Loop to display the specific methods. To do that just write this code inside the IDLE text editor.
- if __name__=='__main__':
- main()
There you have it! we have successfully created a Simple Arcade Game using Pygame. I hope that this simple tutorial helps you with what you are looking for and enhance your programming capabilities. For more updates and tutorials just kindly visit this site. You can also download my working source code below, just click the download button below.
Enjoy Coding!!Add new comment
- 8959 views