Pygame tutorial 1 – part 3.2 – Move Sprite

This time we are going to load a surface with an image to move it on the screen with the arrow keys. Something very basic, the next time we will go deeper into sprites moving and collisions too. The github repo with the examples are here.

import pygame


pygame.init()
screen = pygame.display.set_mode((400, 600))


class Cat:
    x = 100
    y = 100


cat = pygame.image.load("cat/Idle (1).png").convert_alpha()
square = pygame.Surface((cat.get_size()))
square.fill((0, 180, 250))
sw, sh = screen.get_size()
clock = pygame.time.Clock()
while True:
    screen.fill((128, 255, 128))
    # screen.blit(pygame.transform.scale(cat, (sw, sh)),(0, 0))
    #screen.blit(square, (Cat.x, Cat.y))
    screen.blit(cat, (Cat.x, Cat.y))
    if pygame.event.get(pygame.QUIT):
        break
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                Cat.x += 10
            if event.key == pygame.K_LEFT:
                Cat.x -= 10
            if event.key == pygame.K_UP:
                Cat.y -= 10
            if event.key == pygame.K_DOWN:
                Cat.y += 10



    pygame.display.update()
    clock.tick(60)


pygame.quit()

Video


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.