Pygpoint 3 – move slides with the mouse

Previous code

Now you can:

  • go back and forward with the mouse to

When you click on the left and on the right of the middle of the image.

# load a spreadsheet
import pygame
import sys
from glob import glob

def init():
    "Load images and set the screen to fit them"
    global images
    global screen
    global clock

    images = [pygame.image.load(png) for png in glob("images01\\*.png")]
    width = images[0].get_rect().width
    height = images[0].get_rect().height
    screen = pygame.display.set_mode((width, height))
    clock = pygame.time.Clock()



counter = 0
init()
def go_forward():
    global counter, images

    if counter < len(images) - 1:
        counter += 1


def go_back():
    global counter
    if counter > 0:
        counter -= 1
    else:
        counter = 0


loop = 1
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            print(pos)
            if pos[0] > images[0].get_rect().width // 2:
                go_forward()
            else:
                go_back()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                go_forward()
            elif event.key == pygame.K_LEFT:
                go_back()

    screen.fill((0,0,0))
    screen.blit(images[counter], (0, 0))
    pygame.display.flip()
    clock.tick(10)

pygame.quit()
sys.exit()

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.