Institute of Computer Science
  1. Main page
  2. Python koolis
ET
Log in

Python koolis

  • Sissejuhatus
  • Teemad
  • PyGame

1. Aken
2. Värvid ja koordinaadid
3. Joonistamine
4. Pildid
5. Tekst
6. Animeerimine
7. Juhtimine klaviatuuriga
8. Juhtimine hiirega
9. Heli
10. Õpetusi

  • ÜLESANDED

8. PyGame: juhtimine hiirega

Hiire liikumist kontrollib MOUSEMOTION, hiire koordinaadid saame (x, y) paarina kätte sündmuse atribuudist pos. Järgmises näites liigub pildifail hiirega kaasa.

import pygame, sys
pygame.init()
ekraan = pygame.display.set_mode([800, 600])
ekraan.fill([255, 255, 255])
pilt = pygame.image.load("pildifail.png")
x = y = 0 # algsed koordinaadid
ekraan.blit(pilt, (x, y)) # Joonistame pildi koordinaatidega (x, y)
running = True
while running:
    ekraan.fill([255, 255, 255])
    ekraan.blit(pilt, (x,y))
    pygame.display.flip()
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            running = False
        # Kui sündmuseks on hiire liikumine,...
        elif i.type == pygame.MOUSEMOTION:
            # ... siis muudame pildi koordinaate sisaldavaid muutujaid x ja y 
            x, y = i.pos 
pygame.quit()

Vasakpoolsele hiirenupule vajutamist registreerib MOUSEBUTTONDOWN, nupu lahtilaskmist MOUSEBUTTONUP. Järgmises näites paigutatakse pilt hiireklõpsu peale hiirenoole kohale.

import pygame, sys
pygame.init()
ekraan = pygame.display.set_mode([800, 600])
ekraan.fill([255, 255, 255])
pilt = pygame.image.load("pildifail.png")
x = y = 0 # algsed koordinaadid
ekraan.blit(pilt, (x, y)) # Joonistame pildi koordinaatidega (x, y)
running = True
while running:
    ekraan.fill([255, 255, 255])
    ekraan.blit(pilt, (x,y))
    pygame.display.flip()
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            running = False
        # Kui sündmuseks on hiireklõps,...
        elif i.type == pygame.MOUSEBUTTONDOWN:
            # ... siis muudame pildi koordinaate sisaldavaid muutujaid x ja y 
            x, y = i.pos 
pygame.quit()

Lisamaterjalid

  • Sündmuste loetelu: http://www.pygame.org/docs/ref/event.html
  • Hiirega seotud sündmused: http://www.pygame.org/docs/ref/mouse.html

Creative Commons litsentsiga Autorile viitamine + Mitteäriline eesmärk 3.0 Eesti (CC BY-NC 3.0 EE) - https://creativecommons.org/licenses/by-nc/3.0/ee/

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment