Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pygame controls #291

Merged
merged 8 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions _sources/lectures/TWP60/TWP60_6.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
Eventos
=======

.. raw:: html

<script src='../_static/game.js'></script>

<script type="module">
window.gamejs.init();
</script>

Los eventos en pygame se manejan a través del módulo pygame.event.
El método pygame.event.get() obtiene todos los eventos de la cola de eventos.
La cola de eventos es una lista de objetos de eventos enviados al programa por el administrador de ventanas.
o el propio programa pygame.
El método pygame.event.get() devuelve una lista de objetos pygame.event.Event.
Cada objeto de evento tiene un tipo de evento almacenado en el atributo de tipo.
El atributo de tipo se puede comparar con las constantes definidas en el módulo pygame.locals.
El módulo pygame.locals define constantes para los tipos de eventos.
Los tipos de eventos se utilizan para determinar qué tipo de evento ha ocurrido.
Los tipos de eventos se enumeran a continuación:

+ KEYUP
+ KEYDOWN
+ MOUSEMOTION
+ MOUSEBUTTONUP
+ MOUSEBUTTONDOWN

KEYDOWN es un tipo de evento que se dispara cuando se presiona una tecla. como cuando presionas una tecla en el teclado.

KEYUP es un tipo de evento que se activa cuando se suelta una tecla. como cuando quitas el dedo de una tecla.


.. activecode:: ac_l60_6_en
NicolasSandoval marked this conversation as resolved.
Show resolved Hide resolved
:language: python3
:python3_interpreter: brython

from browser import load, timer, window
load('../../_static/game.js')
load('../../_static/pygame.brython.js')
gjs = window.gamejs
gjs.ready()

^^^^
import pygame
from pygame.locals import *

# Pygame to show keyevents
# green box moves with arrow keys

# set up the colors
BLACK = pygame.color.Color('black')
WHITE = pygame.color.Color('white')
GREEN = pygame.color.Color('green')

screen = pygame.display.set_mode((400,300))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, pygame.Rect(100, 100, 50, 50))

blockX = 100
blockY = 100

def main():


# keyboard event loop
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
global blockX
global blockY
if event.key == pygame.K_LEFT:
blockX -= 10
if event.key == pygame.K_RIGHT:
blockX += 10
if event.key == pygame.K_UP:
blockY -= 10
if event.key == pygame.K_DOWN:
blockY += 10


screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, pygame.Rect(blockX, blockY, 50, 50))

# draw the window onto the screen
pygame.display.update()




pygame.init()

timer1 = timer.set_interval(main,80)


MOUSEMOTION es un tipo de evento que se dispara cuando se mueve el mouse.

MOUSEBUTTONDOWN es un tipo de evento que se activa cuando se presiona un botón del mouse.

MOUSEBUTTONUP es un tipo de evento que se activa cuando se suelta un botón del mouse.


.. activecode:: ac_l60_6_2
:language: python3
:python3_interpreter: brython

from browser import load, timer, window
load('../../_static/game.js')
load('../../_static/pygame.brython.js')
gjs = window.gamejs
gjs.ready()

^^^^
import pygame
from pygame.locals import *

# Pygame para mostrar eventos del mouse
# Si el mouse se está moviendo, mostraremos el texto "El mouse se está moviendo"
# Si se hace clic con el mouse, mostraremos el texto "Se hace clic con el mouse"
# Si se suelta el mouse, mostraremos el texto "Se suelta el mouse"
# También mostraremos la posición del mouse y en qué botón se hace clic

BLACK = pygame.color.Color('black')
WHITE = pygame.color.Color('white')
GREEN = pygame.color.Color('green')

screen = pygame.display.set_mode((800,800))
screen.fill(GREEN)

font=pygame.font.SysFont('timesnewroman',30)
left_key = font.render('se hace clic en el botón izquierdo del ratón,True,BLACK)
right_key = font.render('se hace clic en el botón derecho del ratón',True,BLACK)
mouse = font.render('El mouse se está moviendo',True,BLACK)
button_down = font.render('Se suelta el mouse',True,BLACK)
mouse_pos = font.render('Posición del mouse',True,BLACK)

def main():
NicolasSandoval marked this conversation as resolved.
Show resolved Hide resolved

# mouse event loop
for event in pygame.event.get():
screen.fill(GREEN)
if event.type == pygame.MOUSEMOTION:
screen.blit(mouse,(100,100))
mouse_position = pygame.mouse.get_pos()
screen.blit(mouse_pos,(100,200))
screen.blit(font.render(str(mouse_position),True,BLACK),(400,200))
if event.type == pygame.MOUSEBUTTONUP:
mouse_keys = pygame.mouse.get_pressed()
if mouse_keys[0]:
screen.blit(left_key,(100,100))
if mouse_keys[2]:
screen.blit(right_key,(100,100))
else :
if event.type == pygame.MOUSEBUTTONDOWN:
screen.blit(button_down,(100,100))

pygame.display.update()

pygame.init()

timer1 = timer.set_interval(main,80)


159 changes: 159 additions & 0 deletions _sources/lectures/TWP60/TWP60_6_en.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
Events
================

.. raw:: html

<script src='../_static/game.js'></script>

<script type="module">
window.gamejs.init();
</script>

Event in pygame are handled through the pygame.event module.
The pygame.event.get() method gets all the events from the event queue.
The event queue is a list of event objects that are sent to the program by the window manager
or by the pygame program itself.
The pygame.event.get() method returns a list of pygame.event.Event objects.
Each Event object has an event type stored in the type attribute.
The type attribute can be compared to the constants defined in the pygame.locals module.
The pygame.locals module defines constants for the event types.
The event types are used to determine what type of event has occurred.
The event types are listed below:

+ KEYDOWN
+ KEYUP
+ MOUSEMOTION
+ MOUSEBUTTONDOWN
+ MOUSEBUTTONUP

KEYDOWN is an event type that is fired when a key is pressed down. like when you press a key on the keyboard.

KEYUP is an event type that is fired when a key is released. like when you lift your finger off a key.

.. activecode:: ac_l60_6_1_en
:language: python3
:python3_interpreter: brython

from browser import load, timer, window
load('../../_static/game.js')
load('../../_static/pygame.brython.js')
gjs = window.gamejs
gjs.ready()

^^^^
import pygame
from pygame.locals import *

# Pygame to show keyevents
# green box moves with arrow keys

# set up the colors
BLACK = pygame.color.Color('black')
WHITE = pygame.color.Color('white')
GREEN = pygame.color.Color('green')

screen = pygame.display.set_mode((400,300))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, pygame.Rect(100, 100, 50, 50))

blockX = 100
blockY = 100

def main():


# keyboard event loop
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
global blockX
global blockY
if event.key == pygame.K_LEFT:
blockX -= 10
if event.key == pygame.K_RIGHT:
blockX += 10
if event.key == pygame.K_UP:
blockY -= 10
if event.key == pygame.K_DOWN:
blockY += 10


screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, pygame.Rect(blockX, blockY, 50, 50))

# draw the window onto the screen
pygame.display.update()




pygame.init()

timer1 = timer.set_interval(main,80)


MOUSEMOTION is an event type that is fired when the mouse is moved.

MOUSEBUTTONDOWN is an event type that is fired when a mouse button is pressed down.

MOUSEBUTTONUP is an event type that is fired when a mouse button is released.

.. activecode:: ac_l60_6_2_en
:language: python3
:python3_interpreter: brython

from browser import load, timer, window
load('../../_static/game.js')
load('../../_static/pygame.brython.js')
gjs = window.gamejs
gjs.ready()

^^^^
import pygame
from pygame.locals import *

# Pygame to show mouse events
# If mouse is moving, we will display the text "Mouse is moving"
# If mouse is clicked, we will display the text "Mouse is clicked"
# If mouse is released, we will display the text "Mouse is released"
# We will also display the mouse position and which button is clicked

BLACK = pygame.color.Color('black')
WHITE = pygame.color.Color('white')
GREEN = pygame.color.Color('green')

screen = pygame.display.set_mode((800,800))
screen.fill(GREEN)

font=pygame.font.SysFont('timesnewroman',30)
left_key = font.render('Left mouse button clicked',True,BLACK)
right_key = font.render('Right mouse button clicked',True,BLACK)
mouse = font.render('Mouse is moving',True,BLACK)
button_down = font.render('Mouse button is pressed down',True,BLACK)
mouse_pos = font.render('Mouse position is : ',True,BLACK)

def main():

# mouse event loop
for event in pygame.event.get():
screen.fill(GREEN)
if event.type == pygame.MOUSEMOTION:
screen.blit(mouse,(100,100))
mouse_position = pygame.mouse.get_pos()
screen.blit(mouse_pos,(100,200))
screen.blit(font.render(str(mouse_position),True,BLACK),(400,200))
if event.type == pygame.MOUSEBUTTONUP:
mouse_keys = pygame.mouse.get_pressed()
if mouse_keys[0]:
screen.blit(left_key,(100,100))
if mouse_keys[2]:
screen.blit(right_key,(100,100))
else :
if event.type == pygame.MOUSEBUTTONDOWN:
screen.blit(button_down,(100,100))

pygame.display.update()

pygame.init()

timer1 = timer.set_interval(main,80)

1 change: 1 addition & 0 deletions _sources/lectures/TWP60/toctree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Pygame
TWP60_3.rst
TWP60_4.rst
TWP60_5.rst
TWP60_6.rst
1 change: 1 addition & 0 deletions _sources/lectures/TWP60/toctree_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Pygame
TWP60_3_en.rst
TWP60_4_en.rst
TWP60_5_en.rst
TWP60_6_en.rst
Loading