• We just launched and are currently in beta. Join us as we build and grow the community.

Python - Pygame Simple SpriteSheet Animation

3rafneonline

Anime Script Translator
3 Rep
0
0
0
Rep
0
3 Vouches
0
0
0
Vouches
0
Posts
65
Likes
130
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.
2017-11-09_14_11_41-c_windows_system32_cmd.exe_0_2_0_0.png

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. #Center the Game Application
  4. os

    .environ

    [

    'SDL_VIDEO_CENTERED'

    ]

    =

    '1'

  5. # Game Resolution
  6. screen_width=

    800
  7. screen_height=

    600
  8. screen=

    pygame.display

    .set_mode

    (

    (

    screen_width,

    screen_height)

    )

  9. # Color
  10. black=

    (

    0

    ,

    0

    ,

    0

    )
  11. white=

    (

    255

    ,

    255

    ,

    255

    )

  12. # Fonts
  13. font=

    "fonts/Gamer.ttf"

  14. # Framerate
  15. clock=

    pygame.time

    .Clock

    (

    )
  16. fps=

    12


  17. # Image Files
  18. image=

    pygame.image

    .load

    (

    "images/sprite.png"

    )
  19. imageSize=

    image.get_size

    (

    )
  20. horiz_cell=

    6
  21. vert_cell=

    1
  22. cell_width=

    imageSize[

    0

    ]

    /horiz_cell
  23. cell_height=

    imageSize[

    1

    ]

    /vert_cell

  24. cell_list=

    [

    ]

  25. cell_position=

    0

  26. # Slicing The Sprite Sheet
  27. for

    y in

    range

    (

    0

    ,

    imageSize[

    1

    ]

    ,

    int

    (

    cell_height)

    )

    :
  28. for

    x in

    range

    (

    0

    ,

    imageSize[

    0

    ]

    ,

    int

    (

    cell_width)

    )

    :
  29. surface =

    pygame.Surface

    (

    (

    cell_width,

    cell_height)

    )
  30. surface.blit

    (

    image,

    (

    0

    ,

    0

    )

    ,
  31. (

    x,

    y,

    cell_width,

    cell_height)

    )
  32. 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. if

    cell_position <

    len

    (

    cell_list)

    -1

    :
  8. cell_position+=

    1
  9. else

    :
  10. cell_position=

    0
  11. title=

    text_format(

    "Python - Pygame Simple SpriteSheet Animation"

    ,

    font,

    40

    ,

    white)
  12. titleRect=

    title.get_rect

    (

    )
  13. screen.blit

    (

    title,

    (

    screen_width/2

    - (

    titleRect[

    2

    ]

    /2

    )

    ,

    50

    )

    )
  14. screen.blit

    (

    cell_list[

    cell_position]

    ,

    (

    300

    ,

    200

    )

    )
  15. pygame.display

    .update

    (

    )
  16. pygame.display

    .set_caption

    (

    "Python - Pygame Simple SpriteSheet Animation"

    )
  17. 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!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top