Python - Pygame Simple Sprite Movement
Submitted by razormist on Monday, November 13, 2017 - 17:45.
In this tutorial we will create a Simple Sprite Movement. 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 beginner to learn Pygame when making basic games. So let's now do the coding...
There you have it we just created a Simple Sprite Movement using Pygame. I hope that this tutorial help you for what you are looking for . For more updates and tutorials just kindly visit this site. Enjoy 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, open the command prompt then type "python -m pip install pygame", and hit enter.
Importing Modules
After setting up the installation, run the IDLE and click file and then 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, os
- from pygame.locals import *
- pygame.init()
Writing The Variables
We will assign the a 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.- #============VARIABLES==============
- os.environ['SDL_VIDEO_CENTERED'] = '1'
- clock = pygame.time.Clock()
- screen_width=800
- screen_height=600
- bgColor = (255, 255, 255)
- size=(screen_width, screen_height)
- display = pygame.display.set_mode(size)
- progress = True
- pygame.display.set_caption('Python - Pygame Simple Sprite Movement')
- char = pygame.image.load(spider).convert_alpha()
- x=325
- y=250
- speed = 5
- left = False
- right = False
- up = False
- down = False
Main Function
This is the Main Code for the Pygame application. The code contains a several input function that will allow the object to move and render it to the application. To do that just write this code inside the IDLE text editor.- #============MAIN LOOP==============
- while progress:
- display.fill(bgColor)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- progress = False
- pygame.quit()
- quit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_q:
- progress = False
- pygame.quit()
- quit()
- #Player Input KeyDown
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- left = True
- elif event.key == pygame.K_RIGHT:
- right = True
- elif event.key == pygame.K_UP:
- up = True
- elif event.key == pygame.K_DOWN:
- down = True
- #Player Input KeyUP
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT:
- left = False
- elif event.key == pygame.K_RIGHT:
- right = False
- elif event.key == pygame.K_UP:
- up = False
- elif event.key == pygame.K_DOWN:
- down = False
- #Sprite Started To Move
- if left:
- x-=speed
- elif right:
- x+=speed
- elif up:
- y-=speed
- elif down:
- y+=speed
- #Limit The Player Movement within the boundary
- if x > screen_width - char.get_width():
- x = screen_width - char.get_width()
- if x < 0:
- x = 0
- if y > screen_height - char.get_height():
- y = screen_height - char.get_height()
- if y < 0:
- y = 0
- display.blit(char, (x, y))
- pygame.display.flip()
- #Framerate of the Game
- clock.tick(60)
Add new comment
- 638 views