Incluimos el texto del marcador para nuestro juego.
Con este video terminamos nuestro primer juego Pygame, que nos servira como base para realizar mas.
El codigo completo del juego esta en el siguiente enlace.
nota: en el video no aparece “ventana.blit(TextoMarcador,(0,0)” para añadir el texto a la pantalla, añadirlo debajo para que se pueda mostrar el marcador.
Espero sea util. Gracias.
Preparamos la clase que manejara los meteoritos a los que tendremos que disparar o evitar colisionar con ellos.
En el siguiente enlace esta publicado el codigo completo de este juego.
En este video creamos la clase que manejara nuestra nave, el jugador principal.
# -*- coding: utf-8 -*- “”” Created on Thu Sep 6 20:46:42 2018 @author: Jose “”” import pygame #heredamos de Sprite class Nave(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self)
self.imagenNave=pygame.image.load(‘imagenes/nave.png’) #tomamos rectangulo imagen self.rect=self.imagenNave.get_rect() #la situamos en medio y abajo de la pantalla self.rect.centerx=240 self.rect.centery=690 self.velocidad=10 self.vida=True self.listaDisparo=[] def mover(self): if self.vida==True: if self.rect.left<=0: self.rect.left=0 elif self.rect.right>490: self.rect.right=490 def disparar(self): print(‘Disparo’) def dibujar(self,superficie): superficie.blit(self.imagenNave,self.rect)
Preparando carpetas y archivos para crear nuestro propio juego con pygame.
# -*- coding: utf-8 -*- “”” Created on Thu Sep 6 20:46:42 2018
@author: Jose “”” import pygame,sys from pygame.locals import* #variables ANCHO=480 ALTO=700 #funcion principal def Meteoritos(): pygame.init() ventana=pygame.display.set_mode((ANCHO,ALTO)) #imagen de fondo fondo=pygame.image.load(‘imagenes/fondo.png’) ventana.blit(fondo,(0,0)) #titulo pygame.display.set_caption(‘Meteoritos’) #ciclo del juego while True: for evento in pygame.event.get(): #para cerrar con X if evento.type==QUIT: pygame.quit() sys.exit() pygame.display.update() #llamada a funcion principal Meteoritos()
En los juegos una parte fundamental y muy importante son las colisiones entre objetos.
En este video vemos como detectar que un rectangulo toca al otro.
nota: el codigo siguiente esta rectificado con respecto al video segun se indica al final del mismo, para verificar que el rectangulo no se salga de los limites de la pantalla.
# -*- coding: utf-8 -*-“””Created on Tue Aug 14 15:44:41 2018 @author: Jose“”” #importamos modulosimport pygame, sysfrom pygame.locals import *from random import randint #init antes de usar pygamepygame.init()#declaramos ventana con alto anchoventana=pygame.display.set_mode((500,400))#titulopygame.display.set_caption(“Colisiones”)#variablescolorFondo=(25,150,70)colorRectangulo1=(255,255,255)colorRectangulo2=(255,55,0)velocidad=10 posX,posY=randint(1,400),randint(1,300)#rectangulosrectangulo1=pygame.Rect(5,10,70,40)rectangulo2=pygame.Rect(posX,posY,70,40)#bucle ejecucion ventanawhile True: ventana.fill(colorFondo) #dibujamos rectangulos pygame.draw.rect(ventana,colorRectangulo1,rectangulo1) pygame.draw.rect(ventana,colorRectangulo2,rectangulo2) #codigo seguir puntero raton posX,posY=pygame.mouse.get_pos() #lo centramos a rectangulo posX=posX-35 posY=posY-20 #cambiamos posicion rectangulo 1 rectangulo1.left=posX rectangulo1.top=posY #colision if rectangulo1.colliderect(rectangulo2): print(“colisionando”) posX,posY=randint(1,400),randint(1,300) #cambiamos las coordenadas si se sale de limites if posX<0: posX=0 elif posX>430: posX=430 elif posY<0: posY=0 elif posY>360: posY=360 rectangulo2.left=posX-35 rectangulo2.top=posY-20 #control de eventos for evento in pygame.event.get(): if evento.type==QUIT: pygame.quit() sys.exit() #actualizamos segun pulse tecla flechas elif evento.type==pygame.KEYDOWN: if evento.key==K_LEFT: posX-=velocidad if posX<0: posX=0 elif evento.key==K_RIGHT: posX+=velocidad if posX>(500-70): posX=500-70 elif evento.key==K_UP: posY-=velocidad if posY<0: posY=0 elif evento.key==K_DOWN: posY+=velocidad if posY>360: posY=360 #actualiza ventana pygame.display.update()