Skip to main content

Creating a Simple Arcade Game in Python/Pygame

Screenshot
Body

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.

So let's now do the coding...

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.

tut1

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.

  1. import pygame, sys, os
  2. 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.

  1. # Game Initialization
  2. pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=4096)
  3. pygame.init()
  4.  
  5. # Center the Game Application
  6. os.environ['SDL_VIDEO_CENTERED']='1'
  7.  
  8.  
  9. # Game Resolution
  10. screen_width=800
  11. screen_height=600
  12. screen=pygame.display.set_mode((screen_width, screen_height))
  13.  
  14. # Initialize Sound Files
  15. sound=pygame.mixer.Sound('sounds/sound.wav')
  16. sound.set_volume(.5)
  17.  
  18. # Color
  19. black=(0, 0, 0)
  20. white=(255, 255, 255)
  21. blue=(0, 0, 255)
  22.  
  23. # Fonts
  24. font="font/Arcade Future.otf"
  25.  
  26. # Framerate
  27. clock=pygame.time.Clock()
  28. fps=200
  29.  
  30. # Initial Variables
  31. lineWidth=10
  32. paddleSize=50
  33. 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.

  1. def text_format(text, textFont, textSize, textColor):
  2. font=pygame.font.Font(textFont, textSize)
  3. newText=font.render(text, 0, textColor)
  4. return newText
  5.  
  6. def BackgroundGameplay():
  7. screen.fill(black)
  8. pygame.draw.line(screen, white, ((screen_width / 2), 0), ((screen_width / 2), screen_height), 2)
  9. pygame.draw.rect(screen, blue, ((0, 0), (screen_width, screen_height)), lineWidth)
  10.  
  11. def Paddle(paddle):
  12. if paddle.bottom > screen_height - lineWidth:
  13. paddle.bottom = screen_height - lineWidth
  14. elif paddle.top < lineWidth:
  15. paddle.top = lineWidth
  16.  
  17. pygame.draw.rect(screen, white, paddle)
  18.  
  19. def Ball(ball):
  20. pygame.draw.rect(screen, white, ball)
  21.  
  22. def BallMovement(ball, ballDirX, ballDirY):
  23. ball.x += ballDirX
  24. ball.y += ballDirY
  25. return ball
  26.  
  27. def WallCollision(ball, ballDirX, ballDirY):
  28. if ball.top == (lineWidth) or ball.bottom == (screen_height - lineWidth):
  29. ballDirY = ballDirY * -1
  30. if ball.left == (lineWidth) or ball.right == (screen_width - lineWidth):
  31. ballDirX = ballDirX * -1
  32.  
  33. return ballDirX, ballDirY
  34.  
  35. def BallCollision(ball, paddle1, paddle2, ballDirX):
  36. if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
  37. return -1
  38. elif ballDirX == 1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
  39. return -1
  40. else:
  41. return 1
  42.  
  43. def UpdateScore(paddle1, ball, score, ballDirX):
  44. if ball.left == lineWidth:
  45. return 0
  46. elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
  47. score += 1
  48. return score
  49. elif ball.right == screen_width - lineWidth:
  50. score=0
  51. return score
  52. else:
  53. return score
  54.  
  55. def ScoreHandler(score):
  56. text=text_format("Score: %s" % (score), font, 24, white)
  57. textRect=text.get_rect()
  58. textRect.topleft=(50, 25)
  59. screen.blit(text, textRect)
  60.  
  61.  
  62. def EnemyMovement(ball, ballDirX, paddle2):
  63. if ballDirX == -1:
  64. if paddle2.centery < (screen_height / 2):
  65. paddle2.y += 1
  66. elif paddle2.centery > (screen_height / 2):
  67. paddle2.y -= 1
  68. # if ball moving towards bat, track its movement.
  69. elif ballDirX == 1:
  70. if paddle2.centery < ball.centery:
  71. paddle2.y += 1
  72. else:
  73. paddle2.y -= 1
  74. 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.

  1. def main():
  2.  
  3. ballPosX=screen_width/2 - lineWidth/2
  4. ballPosY=screen_height/2 - lineWidth/2
  5.  
  6. playerPos=(screen_height-paddleSize)/2
  7. enemyPos=(screen_height-paddleSize)/2
  8. score=0
  9.  
  10. ballDirX = -1
  11. ballDirY = -1
  12.  
  13. paddle1=pygame.Rect(paddleOffset, playerPos, lineWidth, paddleSize)
  14. paddle2 = pygame.Rect(screen_width - paddleOffset - lineWidth, enemyPos, lineWidth, paddleSize)
  15. ball=pygame.Rect(ballPosX, ballPosY, lineWidth, lineWidth)
  16.  
  17. BackgroundGameplay()
  18. Paddle(paddle1)
  19. Paddle(paddle2)
  20. Ball(ball)
  21. pygame.mouse.set_visible(0)
  22.  
  23. sound.play(loops=-1)
  24.  
  25. while True:
  26. for event in pygame.event.get():
  27. if event.type==QUIT:
  28. pygame.quit()
  29. sys.exit()
  30. elif event.type==MOUSEMOTION:
  31. mousex, mousey=event.pos
  32. paddle1.y=mousey
  33.  
  34. BackgroundGameplay()
  35. Paddle(paddle1)
  36. Paddle(paddle2)
  37. Ball(ball)
  38.  
  39. ball=BallMovement(ball, ballDirX, ballDirY)
  40. ballDirX, ballDirY=WallCollision(ball, ballDirX, ballDirY)
  41. score=UpdateScore(paddle1, ball, score, ballDirX)
  42. ballDirX = ballDirX * BallCollision(ball, paddle1, paddle2, ballDirX)
  43. paddle2=EnemyMovement(ball, ballDirX, paddle2)
  44. ScoreHandler(score)
  45. pygame.display.set_caption('Python - Pygame Simple Arcade Game')
  46. pygame.display.update()
  47. 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.

  1. if __name__=='__main__':
  2. 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