Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
les_programmes_a_connaitre:langages_term:api [2021/05/05 17:46] lf |
les_programmes_a_connaitre:langages_term:api [2023/01/07 02:06] (Version actuelle) gd [Pour finir] |
||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
=====Debuter avec pygame===== | =====Debuter avec pygame===== | ||
- | page non fini | + | Avant de commencer, tout ce que je vais vous montrer provient du site [[https:// |
+ | |||
+ | |||
+ | < | ||
+ | import pygame | ||
+ | |||
+ | pygame.init() | ||
+ | |||
+ | ecran = pygame.display.set_mode((300, | ||
+ | |||
+ | pygame.quit() | ||
+ | </ | ||
+ | Voici grosso modo la base de tout programme avec pygame,\\ | ||
+ | - D' | ||
+ | - Ensuite vous démarrer la bibliothèque | ||
+ | - Ici vous sélectionnez la taille de la fenêtre (taille horizontal, taille vertical) | ||
+ | - Cette commande ferme la fenêtre | ||
+ | |||
+ | Mais ce code est incomplet, votre fenêtre se ferme toute seule, c'est normal.\\ | ||
+ | ==Comment faire pour que votre fenêtre soit utile?== | ||
+ | Rien de plus simple, //la boucle while//. \\ | ||
+ | < | ||
+ | import pygame | ||
+ | |||
+ | |||
+ | pygame.init() | ||
+ | |||
+ | ecran = pygame.display.set_mode((300, | ||
+ | |||
+ | ouvert = " | ||
+ | while ouvert==" | ||
+ | ouvert = input() | ||
+ | |||
+ | pygame.quit() | ||
+ | </ | ||
+ | Cette boucle while empeche le programme de se fermer, pour fermer la fenêtre, il faudra taper dans la console autre chose que //0//. \\ | ||
+ | Cette fois ci vous avez pu voir votre fenêtre, avant de passer à l' | ||
+ | < | ||
+ | import pygame | ||
+ | |||
+ | ####### | ||
+ | import pygame | ||
+ | |||
+ | |||
+ | WIDTH = 1000 #largeur fenetre | ||
+ | HEIGHT = 600# hauteur fenetre | ||
+ | |||
+ | TITRE_FENETRE = " | ||
+ | |||
+ | running= True | ||
+ | couleur_background = (250, | ||
+ | |||
+ | pygame.init() | ||
+ | |||
+ | |||
+ | screen = pygame.display.set_mode((WIDTH, | ||
+ | pygame.display.set_caption(TITRE_FENETRE) | ||
+ | |||
+ | |||
+ | while running: | ||
+ | ouvert = input() | ||
+ | if ouvert ==" | ||
+ | running= False | ||
+ | |||
+ | |||
+ | pygame.quit() | ||
+ | |||
+ | |||
+ | </ | ||
+ | Voici un petit set pour bien débuter votre programme, on revoit la taille de la fenêtre mais avec quelques trucs en plus. | ||
+ | - TITRE_FENETRE comme son nom l' | ||
+ | - couleur_background servira pour plus tard | ||
+ | - running aussi | ||
+ | |||
+ | **ATTENTION** \\ | ||
+ | Les commandes de pygame doivent être entrées après la ligne // | ||
+ | Vous avez remarquer que toute les commande // | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | < | ||
+ | icon_32x32 = pygame.image.load(" | ||
+ | </ | ||
+ | < | ||
+ | Bijour | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | </ | ||
+ | |||
+ | ====Pour finir==== | ||
+ | Quelques petits exemples de programmes utilisant pygame ainsi que d' | ||
+ | < | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Created on Thu Jan 5 23:51:40 2023 | ||
+ | |||
+ | @author: gdani | ||
+ | """ | ||
+ | |||
+ | import random | ||
+ | import pygame, sys | ||
+ | from pygame.locals import * | ||
+ | |||
+ | pygame.init() | ||
+ | fps = pygame.time.Clock() | ||
+ | |||
+ | #colors | ||
+ | WHITE = (255, | ||
+ | RED = (255,0,0) | ||
+ | GREEN = (0,255,0) | ||
+ | BLACK = (0,0,0) | ||
+ | |||
+ | #globals | ||
+ | WIDTH = 600 | ||
+ | HEIGHT = 400 | ||
+ | BALL_RADIUS = 20 | ||
+ | PAD_WIDTH = 8 | ||
+ | PAD_HEIGHT = 80 | ||
+ | HALF_PAD_WIDTH = PAD_WIDTH / 2 | ||
+ | HALF_PAD_HEIGHT = PAD_HEIGHT / 2 | ||
+ | ball_pos = [0,0] | ||
+ | ball_vel = [0,0] | ||
+ | paddle1_vel = 0 | ||
+ | paddle2_vel = 0 | ||
+ | l_score = 0 | ||
+ | r_score = 0 | ||
+ | |||
+ | #canvas declaration | ||
+ | window = pygame.display.set_mode((WIDTH, | ||
+ | pygame.display.set_caption(' | ||
+ | |||
+ | # helper function that spawns a ball, returns a position vector and a velocity vector | ||
+ | # if right is True, spawn to the right, else spawn to the left | ||
+ | def ball_init(right): | ||
+ | global ball_pos, ball_vel # these are vectors stored as lists | ||
+ | ball_pos = [WIDTH/ | ||
+ | horz = random.randrange(2, | ||
+ | vert = random.randrange(1, | ||
+ | |||
+ | if right == False: | ||
+ | horz = - horz | ||
+ | |||
+ | ball_vel = [horz, | ||
+ | |||
+ | # define event handlers | ||
+ | def init(): | ||
+ | global paddle1_pos, | ||
+ | global score1, score2 | ||
+ | paddle1_pos = [HALF_PAD_WIDTH - 1, | ||
+ | paddle2_pos = [WIDTH +1 - HALF_PAD_WIDTH, | ||
+ | l_score = 0 | ||
+ | r_score = 0 | ||
+ | if random.randrange(0, | ||
+ | ball_init(True) | ||
+ | else: | ||
+ | ball_init(False) | ||
+ | |||
+ | |||
+ | #draw function of canvas | ||
+ | def draw(canvas): | ||
+ | global paddle1_pos, | ||
+ | |||
+ | canvas.fill(BLACK) | ||
+ | pygame.draw.line(canvas, | ||
+ | pygame.draw.line(canvas, | ||
+ | pygame.draw.line(canvas, | ||
+ | pygame.draw.circle(canvas, | ||
+ | |||
+ | # update paddle' | ||
+ | if paddle1_pos[1] > HALF_PAD_HEIGHT and paddle1_pos[1] < HEIGHT - HALF_PAD_HEIGHT: | ||
+ | paddle1_pos[1] += paddle1_vel | ||
+ | elif paddle1_pos[1] == HALF_PAD_HEIGHT and paddle1_vel > 0: | ||
+ | paddle1_pos[1] += paddle1_vel | ||
+ | elif paddle1_pos[1] == HEIGHT - HALF_PAD_HEIGHT and paddle1_vel < 0: | ||
+ | paddle1_pos[1] += paddle1_vel | ||
+ | |||
+ | if paddle2_pos[1] > HALF_PAD_HEIGHT and paddle2_pos[1] < HEIGHT - HALF_PAD_HEIGHT: | ||
+ | paddle2_pos[1] += paddle2_vel | ||
+ | elif paddle2_pos[1] == HALF_PAD_HEIGHT and paddle2_vel > 0: | ||
+ | paddle2_pos[1] += paddle2_vel | ||
+ | elif paddle2_pos[1] == HEIGHT - HALF_PAD_HEIGHT and paddle2_vel < 0: | ||
+ | paddle2_pos[1] += paddle2_vel | ||
+ | |||
+ | #update ball | ||
+ | ball_pos[0] += int(ball_vel[0]) | ||
+ | ball_pos[1] += int(ball_vel[1]) | ||
+ | |||
+ | #draw paddles and ball | ||
+ | pygame.draw.circle(canvas, | ||
+ | pygame.draw.polygon(canvas, | ||
+ | pygame.draw.polygon(canvas, | ||
+ | |||
+ | #ball collision check on top and bottom walls | ||
+ | if int(ball_pos[1]) <= BALL_RADIUS: | ||
+ | ball_vel[1] = - ball_vel[1] | ||
+ | if int(ball_pos[1]) >= HEIGHT + 1 - BALL_RADIUS: | ||
+ | ball_vel[1] = -ball_vel[1] | ||
+ | |||
+ | #ball collison check on gutters or paddles | ||
+ | if int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH and int(ball_pos[1]) in range(paddle1_pos[1] - HALF_PAD_HEIGHT, | ||
+ | ball_vel[0] = -ball_vel[0] | ||
+ | ball_vel[0] *= 1.1 | ||
+ | ball_vel[1] *= 1.1 | ||
+ | elif int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH: | ||
+ | r_score += 1 | ||
+ | ball_init(True) | ||
+ | |||
+ | if int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH and int(ball_pos[1]) in range(paddle2_pos[1] - HALF_PAD_HEIGHT, | ||
+ | ball_vel[0] = -ball_vel[0] | ||
+ | ball_vel[0] *= 1.1 | ||
+ | ball_vel[1] *= 1.1 | ||
+ | elif int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH: | ||
+ | l_score += 1 | ||
+ | ball_init(False) | ||
+ | |||
+ | #update scores | ||
+ | myfont1 = pygame.font.SysFont(" | ||
+ | label1 = myfont1.render(" | ||
+ | canvas.blit(label1, | ||
+ | |||
+ | myfont2 = pygame.font.SysFont(" | ||
+ | label2 = myfont2.render(" | ||
+ | canvas.blit(label2, | ||
+ | |||
+ | |||
+ | #keydown handler | ||
+ | def keydown(event): | ||
+ | global paddle1_vel, | ||
+ | |||
+ | if event.key == K_UP: | ||
+ | paddle2_vel = -8 | ||
+ | elif event.key == K_DOWN: | ||
+ | paddle2_vel = 8 | ||
+ | elif event.key == K_w: | ||
+ | paddle1_vel = -8 | ||
+ | elif event.key == K_s: | ||
+ | paddle1_vel = 8 | ||
+ | |||
+ | #keyup handler | ||
+ | def keyup(event): | ||
+ | global paddle1_vel, | ||
+ | |||
+ | if event.key in (K_w, K_s): | ||
+ | paddle1_vel = 0 | ||
+ | elif event.key in (K_UP, K_DOWN): | ||
+ | paddle2_vel = 0 | ||
+ | |||
+ | init() | ||
+ | |||
+ | |||
+ | #game loop | ||
+ | while True: | ||
+ | |||
+ | draw(window) | ||
+ | |||
+ | for event in pygame.event.get(): | ||
+ | |||
+ | if event.type == KEYDOWN: | ||
+ | keydown(event) | ||
+ | elif event.type == KEYUP: | ||
+ | keyup(event) | ||
+ | elif event.type == QUIT: | ||
+ | pygame.quit() | ||
+ | sys.exit() | ||
+ | |||
+ | pygame.display.update() | ||
+ | fps.tick(60) | ||
+ | </ | ||
+ | Et voici le rendu : {{: | ||
+ | |||
+ | Un dernier petit exemple avec tkinter sur la récurrence, | ||
+ | < | ||
+ | import turtle as t | ||
+ | |||
+ | def koch(longueur, | ||
+ | if n == 0: | ||
+ | t.forward(longueur) | ||
+ | else: | ||
+ | koch(longueur/ | ||
+ | t.left(60) | ||
+ | koch(longueur/ | ||
+ | t.right(120) | ||
+ | koch(longueur/ | ||
+ | t.left(60) | ||
+ | koch(longueur/ | ||
+ | |||
+ | def flocon(taille, | ||
+ | koch(taille, | ||
+ | t.right(120) | ||
+ | koch(taille, | ||
+ | t.right(120) | ||
+ | koch(taille, | ||
+ | |||
+ | |||
+ | |||
+ | flocon(400, 3) | ||
+ | </ | ||
+ | Et voici le ptit flocon dessiné grâce à cette bibliothèque. {{: | ||
+ | |||
+ | |||
+ | |||
+ | | ||