# -*- 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)
domingo, 9 de septiembre de 2018
12 Pygame. Nave
En este video creamos la clase que manejara nuestra nave, el jugador principal.
viernes, 7 de septiembre de 2018
11 Pygame. Preparando nuestro juego
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()
jueves, 6 de septiembre de 2018
10 Pygame. Textos y tiempo
Vemos como mostrar textos por pantalla y mostrar el tiempo transcurrido.
# -*- 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("Time")#variablescolorFondo=(25,150,70)colorRectangulo1=(255,255,255)colorRectangulo2=(255,55,0)colorTexto=(125,10,200)velocidad=10
posX,posY=randint(1,400),randint(1,300)#rectangulosrectangulo1=pygame.Rect(5,10,70,40)rectangulo2=pygame.Rect(posX,posY,70,40)#textosfuente=pygame.font.SysFont('Arial',40)
#bucle ejecucion ventanawhile True: ventana.fill(colorFondo) #tiempo tiempo=pygame.time.get_ticks()/1000 #mostramos texto texto=fuente.render("Tiempo: "+str(tiempo),0,colorTexto) ventana.blit(texto,(140,140)) #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()
Suscribirse a:
Entradas (Atom)