Tuesday, 26 December 2017

Python - Pygame Simple SpriteSheet Animation

In this tutorial we will create a Simple SpriteSheet Animation. Pygame is a cross-platform set of Python modules designed for creating games. It includes computer graphics and sound libraries designed to be used with the Python programming language. This is a good opportunity for beginners to learn Pygame first when it comes in developing some games. 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, open the command prompt then type "python -m pip install pygame", and hit enter.
tut1

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

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.
  1. #Game Initialization
  2. pygame.init()
  3.  
  4. #Center the Game Application
  5. os.environ['SDL_VIDEO_CENTERED']='1'
  6.  
  7. # Game Resolution
  8. screen_width=800
  9. screen_height=600
  10. screen=pygame.display.set_mode((screen_width, screen_height))
  11.  
  12. # Color
  13. black=(0, 0, 0)
  14. white=(255, 255, 255)
  15.  
  16. # Fonts
  17. font="fonts/Gamer.ttf"
  18.  
  19. # Framerate
  20. clock=pygame.time.Clock()
  21. fps=12
  22.  
  23.  
  24. # Image Files
  25. image=pygame.image.load("images/sprite.png")
  26. imageSize=image.get_size()
  27. horiz_cell=6
  28. vert_cell=1
  29. cell_width=imageSize[0]/horiz_cell
  30. cell_height=imageSize[1]/vert_cell
  31.  
  32. cell_list=[]
  33.  
  34. cell_position=0
  35.  
  36. # Slicing The Sprite Sheet
  37. for y in range(0, imageSize[1], int(cell_height)):
  38. for x in range(0, imageSize[0], int(cell_width)):
  39. surface = pygame.Surface((cell_width, cell_height))
  40. surface.blit(image, (0, 0),
  41. (x, y, cell_width, cell_height))
  42. cell_list.append(surface)

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. # Methods
  2. def text_format(text, textFont, textSize, textColor):
  3. font=pygame.font.Font(textFont, textSize)
  4. newText=font.render(text, 0, textColor)
  5. return newText

Main Function

This is the Main Code for the Pygame application. The code contains a several function that will allow to render the application. To do that just write this code inside the IDLE text editor.
  1. # Main Loop
  2. while True:
  3. for event in pygame.event.get():
  4. if event.type==pygame.QUIT:
  5. pygame.quit()
  6. sys.exit()
  7.  
  8. if cell_position < len(cell_list)-1:
  9. cell_position+=1
  10. else:
  11. cell_position=0
  12. title=text_format("Python - Pygame Simple SpriteSheet Animation", font, 40, white)
  13. titleRect=title.get_rect()
  14. screen.blit(title, (screen_width/2 - (titleRect[2]/2), 50))
  15. screen.blit(cell_list[cell_position], (300, 200))
  16. pygame.display.update()
  17. pygame.display.set_caption("Python - Pygame Simple SpriteSheet Animation")
  18. clock.tick(fps)
There you have it we successfully created a Simple SpriteSheet Animation using Pygame. I hope that this simple tutorial help you to what you are looking for and enhance your programming capabilities. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

0 comments:

Social Profiles

Twitter Facebook Google Plus LinkedIn RSS Feed Email Pinterest

Mission

Humanity Protection Trust is committed to enhancing the quality of life of vulnerable Nigerians by improving access to health and education

Vision

o have society where all people have access to education, health care, good standard of living and opportunities to realize their God given potential

Popular Posts

Mission

-to promote testing and vaccination of hepatitis B virus to IDP’s. -to provide water and sanitation in the IDP Camps. -to give quality education to deserving IDP's through scholarships.

OUR VISION.

Continue to give hope to the internally displaced persons in Africa. At H.I.D.P-FOUNDATION we strive to have a profound impact on the lives of the IDP’s. Our communal spirit creates an environment we support and inspire, helping the I.D.P’s achieve their fullest potential

About

HIDP-FOUNDATION is a non-profit organization registered to give hope to the internally displaced persons [IDP’s] in Africa. Our commitment includes, identifying talents and giving lifesaving skills etc to people living in the internally displaced camps in Africa.

Copyright © Zina Comedy Network | Powered by Blogger