Moving Image in a window

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()


DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')

WHITE = (255, 255, 255)
img = pygame.image.load('gajjar.jpg')
x = 10
y = 10
direction = 'right'

while True: # the main game loop
    DISPLAYSURF.fill(WHITE)

    if direction == 'right':
        x += 5
        if x == 270:
            direction = 'down'
    elif direction == 'down':
        y += 5
        if y == 210:
           direction = 'left'
    elif direction == 'left':
        x -= 5
        if x == 10:
            direction = 'up'
    elif direction == 'up':
        y -= 5
        if y == 10:
            direction = 'right'
    
    DISPLAYSURF.blit(img, (x, y))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()

    fpsClock.tick(FPS)
Advertisements

Make a window to build a game

Just type this code in text editor:

import pygame, sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption(‘Hello World!’)
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

This is how we open a new window!