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

Python - Pygame Simple Animation

mxzzzz

Codebase Historian
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
75
Likes
135
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
In this tutorial we will create a Simple Animation Using 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 beginner to learn Pygame when making basic 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_.png


Wait for the Pygame to be downloaded and installed at the same time. When its completed type "python -m pygame.examples.oldaliens" and hit enter to check if Pygame is good to go.

2017-11-08_14_50_11-pygame_window_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,

    os
  2. from

    pygame.locals

    import

    *

Then write these several line of codes to make the application work properly
  1. #Center the Application
  2. os

    .environ

    [

    'SDL_VIDEO_CENTERED'

    ]

    =

    '1'

  3. #Initialize the modules
  4. pygame.init

    (

    )

Assigning Variables

We will assign the a certain variables that we will need to declare later in the main function. To do that just kindly write this code inside your text editor
  1. y1 =

    0
  2. y2 =

    0
  3. y3 =

    0
  4. x1 =

    0
  5. speed1 =

    0.5
  6. speed2 =

    0.55
  7. speed3 =

    0.6
  8. speed4 =

    0.6
  9. bgColor =

    0

    ,

    0

    ,

    0
  10. barHeight =

    75
  11. barWidth =

    25
  12. screen_width =

    800
  13. screen_height =

    600
  14. display =

    pygame.display

    .set_mode

    (

    (

    screen_width,

    screen_height)

    )
  15. pygame.display

    .set_caption

    (

    "Pygame Simple Animation"

    )
  16. progress =

    True
  17. barcolor1 =

    (

    255

    ,

    0

    ,

    0

    )
  18. barcolor2 =

    (

    0

    ,

    255

    ,

    0

    )
  19. barcolor3 =

    (

    0

    ,

    0

    ,

    255

    )
  20. barcolor4 =

    (

    255

    ,

    255

    ,

    255

    )

Creating The Main Function

This is the Main Code for the Pygame application. The code contains a several function that will render the objects of the application. To do that just write this code inside the IDLE text editor.
  1. while

    progress:
  2. event =

    pygame.event

    .poll

    (

    )
  3. if

    event.type

    ==

    pygame.QUIT

    :
  4. progress =

    False
  5. pygame.quit

    (

    )
  6. quit(

    )

  7. display.fill

    (

    (

    bgColor)

    )

  8. for

    i in

    range

    (

    0

    ,

    barHeight)

    :
  9. pygame.draw

    .line

    (

    display,

    barcolor1,

    (

    0

    ,

    y1+i)

    ,

    (

    screen_width-1

    ,

    y1+i)

    )

  10. y1 +=

    speed1

  11. if

    y1 + barHeight >

    screen_height-1

    or

    y1 <

    0

    :
  12. speed1 *=

    -1

  13. for

    i in

    range

    (

    0

    ,

    barHeight)

    :
  14. pygame.draw

    .line

    (

    display,

    barcolor2,

    (

    0

    ,

    y2+i)

    ,

    (

    screen_width-1

    ,

    y2+i)

    )

  15. y2 +=

    speed2

  16. if

    y2 + barHeight >

    screen_height-1

    or

    y2 <

    0

    :
  17. speed2 *=

    -1


  18. for

    i in

    range

    (

    0

    ,

    barHeight)

    :
  19. pygame.draw

    .line

    (

    display,

    barcolor3,

    (

    0

    ,

    y3+i)

    ,

    (

    screen_width-1

    ,

    y3+i)

    )

  20. y3 +=

    speed3

  21. if

    y3 + barHeight >

    screen_height-1

    or

    y3 <

    0

    :
  22. speed3 *=

    -1


  23. for

    i in

    range

    (

    0

    ,

    barWidth)

    :
  24. pygame.draw

    .line

    (

    display,

    barcolor4,

    (

    x1+i,

    0

    )

    ,

    (

    x1+i,

    screen_height-1

    )

    )


  25. x1 +=

    speed4


  26. if

    x1 + barWidth >

    screen_width-1

    or

    x1 <

    0

    :
  27. speed4 *=

    -1

  28. pygame.display

    .flip

    (

    )

Then try to run it and see if it works.
ezgif.com-crop.gif


There you have it we just created a Simple Animation Using Pygame. I hope that this tutorial help you understand about Pygame. 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 the hidden content.
 

452,496

336,529

336,537

Top