1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import pygame as pg
CIRCLE_COLOR = pg.Color('yellow')
CIRCLE_WIDTH = 2
CIRCLE_RADIUS = 30
CIRCLE_STEP = 5
CIRCLE_MOTIONS = {
pg.K_UP:(0, -CIRCLE_STEP),
pg.K_DOWN:(0, CIRCLE_STEP),
pg.K_LEFT:(-CIRCLE_STEP, 0),
pg.K_RIGHT:(CIRCLE_STEP, 0),
}
FPS = 30
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Chargement de l'image de fond (à ne jamais faire dans une boucle)
background = pg.image.load("background.jpg").convert()
# Surface transparente destinée au cercle
size = ((CIRCLE_WIDTH + CIRCLE_RADIUS) * 2,) * 2
circle_surf = pg.Surface(size, pg.SRCALPHA, 32).convert_alpha()
# Récupération du rectangle englobant la surface
circle_rect = circle_surf.get_rect()
pg.draw.circle(
circle_surf, CIRCLE_COLOR, circle_rect.center,
CIRCLE_RADIUS, CIRCLE_WIDTH
)
# Affichage
# Apposition de l'image sur l'écran
screen.blit(background, (0, 0))
# Centrage du rect de la surface au centre de l'écran
circle_rect.center = (320, 240)
# Apposition de la surface du cercle sur l'écran
screen.blit(circle_surf, circle_rect)
# Mise à jour
pg.display.update()
# Rectangle de la portion de l'écran à mettre a jour
# On se sert du pas de déplacement * 2 pour agrandir circle_rect
rect_update = circle_rect.inflate(CIRCLE_STEP*2, CIRCLE_STEP*2)
pg.key.set_repeat(10, 10)
tournicoti = True
while tournicoti :
clock.tick(FPS)
for evt in pg.event.get() :
if (evt.type == pg.QUIT or
(evt.type == pg.KEYUP and evt.key == pg.K_ESCAPE)) :
tournicoti = False
break
elif evt.type == pg.KEYDOWN and evt.key in CIRCLE_MOTIONS :
# Déplacement du rectangle de la portion de l'écran à actualiser
rect_update.move_ip(CIRCLE_MOTIONS[evt.key])
# Remplissage de l'écran sur la portion à mettre à jour
screen.blit(background, rect_update, rect_update)
# Déplacement du rectangle de la surface du cercle
circle_rect.move_ip(CIRCLE_MOTIONS[evt.key])
# Apposition de la surface du cercle sur l'écran
screen.blit(circle_surf, circle_rect)
# Mise à jour de la portion de l'écran où des changements sont faits
pg.display.update(rect_update)
pg.quit() |
Partager