Last active
December 20, 2023 13:18
-
-
Save AnimeshShaw/4a7951405bdbd930514c to your computer and use it in GitHub Desktop.
PyGame Flying Animation in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on 16-Nov-2014 | |
@author: Psycho_Coder | |
''' | |
import random | |
import sys | |
import pygame | |
from pygame.locals import QUIT | |
def main(): | |
pygame.init() | |
size = width, height = 650, 450 | |
DISPSURF = pygame.display.set_mode( size, 0 , 32 ) | |
pygame.display.set_caption( "Simple Animation - 1" ) | |
FPS = 30 | |
fps_timer = pygame.time.Clock() | |
img = pygame.image.load( 'supertux.gif' ) | |
startx, starty = 40, height / 2 | |
while True: | |
DISPSURF.blit( pygame.image.load( 'city.png' ), ( 0, 0 ) ) | |
ch = get_direction() | |
if ch == "R": | |
startx += 5 | |
if startx == ( width - 20 ): | |
ch = get_direction() | |
elif ch == "L": | |
startx -= 5 | |
if startx == 20: | |
ch = get_direction() | |
elif ch == "T": | |
starty -= 5 | |
if starty == 20: | |
ch = get_direction() | |
elif ch == "B": | |
starty += 5 | |
if starty == ( height - 20 ): | |
ch = get_direction() | |
DISPSURF.blit( img, ( startx, starty ) ) | |
for evt in pygame.event.get(): | |
if evt.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
pygame.display.update() | |
fps_timer.tick( FPS ) | |
def get_direction(): | |
direction = ["R", "L", "T", "B" ] | |
return direction[ random.randint( 0, 3 ) ] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment