En este primer tutorial, mostraré la configuración inicial del juego, cargaremos musica y un sprite, con esto será suficiente para poder entender como funciona pygame.
Para descargar pygame, usaremos la herramienta pip, de la que ya hemos hablado anteriormente:
Windows, en CMD: python -m pip install pygame
Linux, en la terminal: pip install pygame
Sin más y como siempre, pasamos al codigo, sobre el mismo código pondré los comentarios, creo que os resultará más práctico:
'''
Created on 2 mar. 2018
@author: Juanp
'''
import pygame
from pygame.locals import *
#Constantes
W = 680
H = 460
AZUL_CLARO = (190,220,255)
def main():
#Iniciamos pygame:
pygame.init()
pygame.font.init()
pygame.mixer.init(frequency=22050, size=16, channels=2, buffer=4096)
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((W,H))
#La funcion fill, nos sirve para definir el color de fondo
SCREEN.fill(AZUL_CLARO)
#Cargamos los archivos de juego y definimos variables
sprite = pygame.image.load('globo.png').convert_alpha()
mus_fondo = pygame.mixer_music.load("loopvint.mp3") #Importante diferenciar entre musica y sonido del juego
pygame.mixer.music.play(loops=0, start=0.0)
running = True
#Bucle del juego
while running:
#Mostramos el sprite en pantalla
SCREEN.blit(sprite,(W/2,H/2))
#Eventos de juego, por el momento solo cerramos la aplicacion usando la ventana
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Actualiza pantalla
pygame.display.update()
main()
La pantalla deberia mostrar algo así:



No hay comentarios:
Publicar un comentario