• Aucun résultat trouvé

Documentation Pygame ISN – Novembre 2017

N/A
N/A
Protected

Academic year: 2022

Partager "Documentation Pygame ISN – Novembre 2017"

Copied!
4
0
0

Texte intégral

(1)

Documentation Pygame

ISN – Novembre 2017

1 Code minimal

# I m p o r t a t i o n de pygame i m p o r t pygame

pygame . i n i t ( )

# I n i t i a l i s a t i o n de l a f e n e t r e l a r g e u r = 600

hauteur = 400

windowSurface = pygame . d i s p l a y . set_mode ( ( l a r g e u r , hauteur ) , 0 , 3 2 )

# I n i t i a l i s a t i o n des parametres

# Boucle de j e u

c l o c k = pygame . time . C l oc k ( ) r u n n i n g = True

w h i l e r u n n i n g :

# L i m i t a t i o n du nombre de t o u r s de b o u c l e par seconde . c l o c k . t i c k ( 1 0 )

# Boucle des evenements

f o r event i n pygame . event . get ( ) : i f event .t y p e == pygame . QUIT :

r u n n i n g = F a l s e

# Elements a t r a c e r pygame . d i s p l a y . update ( )

pygame . q u i t ( )

2 Dessiner sur la fenêtre : Draw

Dans toute la suite, on supposera que vous avez appelé la fenêtre windowSurface.

Les couleurs se définissent avec leur code RGB :

BLACK = ( 0 , 0 , 0 )

WHITE = ( 2 5 5 , 255 , 255) RED = ( 2 5 5 , 0 , 0 )

GREEN = ( 0 , 255 , 0 ) BLUE = ( 0 , 0 , 255)

Tracer de objets géométriques

• Un segment : (60, 60) sont les coordonnées du points de départ, (120,60) le point d’ar- rivé et 4 est l’épaisseur du trait.

pygame . draw . l i n e ( windowSurface , c o l o r , ( 6 0 , 60) , ( 1 2 0 , 60) , 4 )

• Un cercle : (300, 50) sont les coordonnées du centre, 50 le rayon et 0 l’épaisseur du trait (0 signifie que le cercle est entièrement colorié).

pygame . draw . c i r c l e ( windowSurface , c o l o r , ( 3 0 0 , 50) , 20 , 0 )

ISN – Novembre 2017 1 / 4

(2)

Documentation Pygame Novembre 2017

• Une ellipse : 300 et 250 sont les coordonnées du centre, 40 le rayon horizontal, 80 le rayon vertical et 1 l’épaisseur du trait.

pygame . draw . e l l i p s e ( windowSurface , c o l o r , ( 3 0 0 , 250 , 4 0 , 8 0 ) , 1 )

• Un rectangle : 20 et 30 sont les coordonnées du coin en haut à gauche du rectangle, 40 est la largeur et 50 est la hauteur.

pygame . draw . r e c t ( windowSurface , c o l o r , ( 2 0 , 30 , 40 , 50) )

• Un polygone : ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)) sont les coordonnées des sommets du polygone.

pygame . draw . polygon ( windowSurface , c o l o r ,

( ( 1 4 6 , 0 ) , ( 2 9 1 , 106) , ( 2 3 6 , 277) , ( 5 6 , 277) , ( 0 , 106) ) )

Il ne faut pas oublier la ligne suivante après avoir tracé tout ce que vous vouliez, sinon rien ne s’a ffi chera.

pygame . d i s p l a y . update ( )

D’autres fonctions de dessins existent. Voici un exemple de tout ce qui peut être fait en dessin avec pygame.

# I m p o r t a l i b r a r y o f f u n c t i o n s c a l l e d ’ pygame ’ i m p o r t pygame

from math i m p o r t p i

# I n i t i a l i z e th e game engine pygame . i n i t ( )

# D e f i n e th e c o l o r s we w i l l use i n RGB format BLACK = ( 0 , 0 , 0 )

WHITE = ( 2 5 5 , 255 , 255) BLUE = ( 0 , 0 , 255) GREEN = ( 0 , 255 , 0 ) RED = ( 2 5 5 , 0 , 0 )

# Set th e h e i g h t and w i d t h o f th e screen s i z e = [ 4 0 0 , 300]

screen = pygame . d i s p l a y . set_mode ( s i z e )

pygame . d i s p l a y . s e t _ c a p t i o n (" Examplecodef o rth edrawmodule ")

#Loop u n t i l th e user c l i c k s th e c l o s e b u t t o n . done = F a l s e

c l o c k = pygame . time . C l oc k ( ) w h i l e not done :

# T h i s l i m i t s th e w h i l e l o o p t o a max o f 10 t i m e s per second .

# Leave t h i s out and we w i l l use a l l CPU we can . c l o c k . t i c k ( 1 0 )

f o r event i n pygame . event . get ( ) : # User d i d something i f event .t y p e == pygame . QUIT : # I f user c l i c k e d c l o s e

done=True # F l a g t h a t we are done so we e x i t t h i s l o o p

# A l l drawing code happens a f t e r th e f o r l o o p and but

# i n s i d e th e main w h i l e done==F a l s e l o o p .

# C l e a r th e screen and s e t th e screen background

ISN – Novembre 2017 2 / 4

(3)

Documentation Pygame Novembre 2017

screen . f i l l ( WHITE )

# Draw on th e screen a GREEN l i n e from ( 0 , 0 ) t o ( 5 0 . 7 5 )

# 5 p i x e l s wide .

pygame . draw . l i n e ( screen , GREEN , [ 0 , 0 ] , [ 5 0 , 3 0 ] , 5 )

# Draw on th e screen a GREEN l i n e from ( 0 , 0 ) t o ( 5 0 . 7 5 )

# 5 p i x e l s wide .

pygame . draw . l i n e s ( screen , BLACK , F a l s e , [ [ 0 , 8 0 ] , [ 5 0 , 9 0 ] , [ 2 0 0 , 8 0 ] , [ 2 2 0 , 3 0 ] ] , 5 )

# Draw on th e screen a GREEN l i n e from ( 0 , 0 ) t o ( 5 0 . 7 5 )

# 5 p i x e l s wide .

pygame . draw . a a l i n e ( screen , GREEN , [ 0 , 5 0 ] , [ 5 0 , 8 0 ] , True )

# Draw a r e c t a n g l e o u t l i n e

pygame . draw . r e c t ( screen , BLACK , [ 7 5 , 10 , 50 , 2 0 ] , 2 )

# Draw a s o l i d r e c t a n g l e

pygame . draw . r e c t ( screen , BLACK , [ 1 5 0 , 10 , 50 , 2 0 ] )

# Draw an e l l i p s e o u t l i n e , u s i n g a r e c t a n g l e as th e o u t s i d e b o u n d a r i e s pygame . draw . e l l i p s e ( screen , RED , [ 2 2 5 , 10 , 50 , 2 0 ] , 2 )

# Draw an s o l i d e l l i p s e , u s i n g a r e c t a n g l e as th e o u t s i d e b o u n d a r i e s pygame . draw . e l l i p s e ( screen , RED , [ 3 0 0 , 10 , 50 , 2 0 ] )

# T h i s draws a t r i a n g l e u s i n g th e polygon command

pygame . draw . polygon ( screen , BLACK , [ [ 1 0 0 , 1 0 0 ] , [ 0 , 2 0 0 ] , [ 2 0 0 , 2 0 0 ] ] , 5 )

# Draw an arc as p a r t o f an e l l i p s e .

# Use r a d i a n s t o determine what angle t o draw .

pygame . draw . arc ( screen , BLACK , [ 2 1 0 , 75 , 150 , 1 2 5 ] , 0 , p i / 2 , 2 ) pygame . draw . arc ( screen , GREEN , [ 2 1 0 , 75 , 150 , 1 2 5 ] , p i / 2 , p i , 2 ) pygame . draw . arc ( screen , BLUE , [ 2 1 0 , 75 , 150 , 1 2 5 ] , p i , 3*p i / 2 , 2 ) pygame . draw . arc ( screen , RED , [ 2 1 0 , 75 , 150 , 1 2 5 ] , 3*p i / 2 , 2*p i , 2 )

# Draw a c i r c l e

pygame . draw . c i r c l e ( screen , BLUE , [ 6 0 , 2 5 0 ] , 40)

# Go ahead and update th e screen w i t h what we ’ ve drawn .

# T h i s MUST happen a f t e r a l l th e o t h e r drawing commands . pygame . d i s p l a y . update ( )

# Be I D L E f r i e n d l y pygame . q u i t ( )

Ajouter une image : Pygame permet d’ajouter des images ayant les formats suivant : JPG, PNG, GIF (non-animated), BMP. On supposera dans la suite qu’elles sont rangées dans le même dossier que notre programme.

# Charger l ’ image

img = pygame . image . l o a d (’ image . j p g ’)

# L ’ a f f i c h e r s u r l a s u r f a c e windowSurface . b l i t ( img , ( 0 , 0 ) )

Les coordonnées (0,0) sont les coordonnées de l’angle en haut à droit de l’image sur la surface.

ISN – Novembre 2017 3 / 4

(4)

Documentation Pygame Novembre 2017

3 Interaction avec les périphériques : Events

L’interaction avec l’utilisateur se fait dans la boucle des évènements.

f o r event i n pygame . event . get ( ) : i f event .t y p e == pygame . QUIT :

r u n n i n g = F a l s e

e l i f event .t y p e == pygame . KEYUP :

# choses a f a i r e quand une touche du c l a v i e r e s t r e l a c h e e i f event . key == pygame . K_UP :

# choses a f a i r e quand c ’ e s t l a touche f l e c h e du haut e l i f event . key == pygame . K_DOWN :

# choses a f a i r e quand c ’ e s t l a touche f l e c h e du bas e l i f event .t y p e == pygame . KEYDOWN :

# choses a f a i r e quand une touche du c l a v i e r e s t p r e s s e e e l i f event . key == pygame . K_LEFT :

# choses a f a i r e quand c ’ e s t l a touche f l e c h e de gauche e l i f event . key == pygame . K_RIGHT :

# choses a f a i r e quand c ’ e s t l a touche f l e c h e de d r o i t e e l i f event .t y p e == pygame . MOUSEBUTTONUP :

# choses a f a i r e quand l e bouton de l a s o u r i s e s t r e l a c h e e l i f event .t y p e == pygame . MOUSEBUTTONDOWN :

# choses a f a i r e quand l e bouton de l a s o u r i s e s t p r e s s e e

De manière générale, le nom des touches de clavier sont faite sur le même modèle : K_### où on remplace les # par le nom de la touche.

• Touche flèche du haut : K_UP

• Touche E : K_E

• Touche entrée : K_ESCAPE

Quelques méthodes pratiques pour manipuler la souris

• pygame.mouse.get_pos() : connaître la position de la souris sur la fenêtre.

• pygame.mouse.set_pos((x,y)) : déplacer la souris à un endroit.

ISN – Novembre 2017 4 / 4

Références

Documents relatifs

pomme abricot Eclair au chocolat Salade de fruits frais Tartelette aux pommes 6 novembre 2017 - 10 novembre 2017.. Cake

Let’s learn how to make interesting things appear on this window instead of just blackness by learning about pixels, Surface objects, Color objects, Rect objects, and the

Dans le cadre de la Semaine des médias et de la presse dans l’École, en partenariat avec le CLEMI et son réseau de coordonnateurs en académies, des visites du bus et des

Jusqu’ici, si vous avez exécuté votre code, votre image n’est pas apparue, et c’est normal, car il faut rafraichir, actualisé la surface pour faire apparaitre notre « collage

« D’abord, le fait considérable de l’universalité du langage : « Tous les hommes parlent » ; c’est là un critère d’humanité à côté de l’outil,

4/ En cliquant sur Ouvrir, une fenêtre de dialogue permet de choisir un fichier BMP.. S – Spécialité ISN 4/ Créer une procédure chargée d’ouvrir le fichier BMP et de

Dans la seconde question, on notera que l’hypothèse de la dimension finie nous garantit l’existence d’un supplémentaire F tel que mentionné.. La question 2.a est déterminante

Organisé par l’association femmes & mathématiques, en partenariat avec la Mission pour la Place des Femmes du CNRS, le Forum est une manifestation qui s’adresse aux