Juego de Flappy Bird haciendo uso de una Raspberry Pi.
En este Proyecto vamos a desarrollar un vídeo Juego basado en Flappy Bird en la Raspberry Pi. Se utilizaron los puertos BCM #5 conectado a un pulsador unido a los 3.3 voltios suministrados en el Pin 1 de la Placa Raspberry PI, como se observa en el vídeo.
Las Líneas de programación se realizaron en Python usando las librerías de pygame en el desarrollo del Juego. El programa desarrollado y las imagenes usadas son las siguiente:
#programa escrito por Jose Munoz Caracas-Venezuela 2019
#jose.munoz2@gmail.com
#Importar las Librerias que Necesitamos
import pygame
from pygame.locals import *
import os
import sys
import random
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#Constantes del programa
SCREEN_WIDTH=800
SCREEN_HEIGHT=480
IMG_DIR = "imagenes"
GAP=160
TUBE_GAP=160
FLUCTUATION=160
LOWEST_OPENING=150
D1=400
D2=800
D3=1200
D4=1600
Min=95
isPlaying=True
score=0
highScore=0
PIN=5
GPIO.setup(PIN,GPIO.IN,GPIO.PUD_DOWN)
#Definimos Funci[on para Cargar las Imagenes
def load_image(nombre,dir_imagen,alpha=False):
ruta=os.path.join(dir_imagen,nombre)
try:
image=pygame.image.load(ruta)
except:
print("Error, cargando imagen",ruta)
sys.exit(1)
if alpha is True:
image=image.convert_alpha()
else:
image =image.convert()
return image
#Definimos la Clase para Los Tubos
class TubeDown(pygame.sprite.Sprite):
"Comportamineto de la pelota en el juego"
def __init__(self,x,ran):
pygame.sprite.Sprite.__init__(self)
self.image=load_image("tubeDown.png",IMG_DIR,alpha=True)
self.rect=self.image.get_rect()
self.x=x
self.rect.centerx=self.x
self.out=False
self.rect.centery=ran
self.speed=[-3,0]
def update(self):
if self.rect.right<0:
self.out=True
self.rect.move_ip((self.speed[0],self.speed[1]))
def setVel(self,vel):
self.speed[0]=-vel
def setPos(self,xPos,yPos):
self.x=xPos
self.rect.centerx=self.x
self.rect.centery=yPos
def getOut(self):
return self.out
def setOut(self,out):
self.out=False
class TubeUp(pygame.sprite.Sprite):
"Comportamineto de la pelota en el juego"
def __init__(self,x,ran):
pygame.sprite.Sprite.__init__(self)
self.image=load_image("tubeUp.png",IMG_DIR,alpha=True)
self.rect=self.image.get_rect()
self.x=x
self.rect.centerx=self.x
self.out=False
self.rect.centery=ran
self.speed=[-3,0]
def update(self):
if self.rect.right<0:
self.out=True
self.rect.move_ip((self.speed[0],self.speed[1]))
def setVel(self,vel):
self.speed[0]=-vel
def setPos(self,xPos,yPos):
self.x=xPos
self.rect.centerx=self.x
self.rect.centery=yPos
def getOut(self):
return self.out
def setOut(self,out):
self.out=out
#Crea la Clase de la Paleta
class Player(pygame.sprite.Sprite):
"Define el comportameinto de la Paleta"
def __init__(self,x):
pygame.sprite.Sprite.__init__(self)
self.image=load_image("bird.png",IMG_DIR,alpha=True)
self.rect=self.image.get_rect()
self.rect.centerx=x
self.rect.centery=SCREEN_HEIGHT/2
self.velY=1
self.colission=False
def humano(self):
#Controla que la paleta no salga de la pantalla
self.rect.centery+=self.velY
if self.rect.bottom>SCREEN_HEIGHT:
self.rect.bottom=SCREEN_HEIGHT
elif self.rect.top<=0:
self.rect.top=0
def colision(self,objeto):
if self.rect.colliderect(objeto.rect):
return True
else:
return False
def millis():
return int(round(time.time()*1000))
#Funcion principal del Juego
def main():
pygame.init()
#Le colocamos el nombre a la ventana
screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird")
fX1=0
fX2=800
#Definimos la Fuente del texto
fuente=pygame.font.Font(None,40)
#Cargamos Imagen
fondo=load_image("fondo1.jpg",IMG_DIR,alpha=False)
fondo1=load_image("fondo1.jpg",IMG_DIR,alpha=False)
fondo2=load_image("gameOver.png",IMG_DIR,alpha=True)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo1=TubeUp(D1+SCREEN_WIDTH,ran)
tubo2=TubeDown(D1+SCREEN_WIDTH,dif)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo3=TubeUp(D2+SCREEN_WIDTH,ran)
tubo4=TubeDown(D2+SCREEN_WIDTH,dif)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo5=TubeUp(D3+SCREEN_WIDTH,ran)
tubo6=TubeDown(D3+SCREEN_WIDTH,dif)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo7=TubeUp(D4+SCREEN_WIDTH,ran)
tubo8=TubeDown(D4+SCREEN_WIDTH,dif)
jugador1=Player(40)
clock=pygame.time.Clock()
#pygame.mouse.set_visible(False)
velP=5
isPlaying=True
timeOld=millis()
score=0
highScore=0
while True:
clock.tick(60)
timeCurrent=millis()
#Obtenemos la Posici[or del Mouse
pos_mouse=pygame.mouse.get_pos()
mov_mouse=pygame.mouse.get_rel()
if GPIO.input(PIN)==True:
newpos= jugador1.rect.centery
newpos -=30
jugador1.rect.centery -=5
fX1 -=1
fX2-=1
if fX1< -800:
fX1=800
if fX2< -800:
fX2=800
screen.blit(fondo,(fX1,0))
screen.blit(fondo1,(fX2,0))
if jugador1.colision(tubo1) or jugador1.colision(tubo2) or jugador1.colision(tubo3) or jugador1.colision(tubo4) or jugador1.colision(tubo5) or jugador1.colision(tubo6) or jugador1.colision(tubo7) or jugador1.colision(tubo8):
isPlaying=False
if isPlaying==True:
score+=5
jugador1.humano()
if tubo8.out==False:
tubo1.update()
tubo2.update()
tubo3.update()
tubo4.update()
tubo5.update()
tubo6.update()
tubo7.update()
tubo8.update()
if tubo8.out==True:
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo1.setPos((D1+SCREEN_WIDTH),ran)
tubo2.setPos((D1+SCREEN_WIDTH),dif)
tubo1.setOut(False)
tubo2.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo3.setPos((D2+SCREEN_WIDTH),ran)
tubo4.setPos((D2+SCREEN_WIDTH),dif)
tubo3.setOut(False)
tubo4.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo5.setPos((D3+SCREEN_WIDTH),ran)
tubo6.setPos((D3+SCREEN_WIDTH),dif)
tubo5.setOut(False)
tubo6.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo7.setPos((D4+SCREEN_WIDTH),ran)
tubo8.setPos((D4+SCREEN_WIDTH),dif)
tubo7.setOut(False)
tubo8.setOut(False)
random.seed()
vel=random.randint(3,10)
tubo1.setVel(vel)
tubo2.setVel(vel)
tubo3.setVel(vel)
tubo4.setVel(vel)
tubo5.setVel(vel)
tubo6.setVel(vel)
tubo7.setVel(vel)
tubo8.setVel(vel)
else:
clock.tick(60)
score=0
todos=pygame.sprite.RenderPlain(tubo1,tubo2,tubo3,tubo4,tubo5,tubo6,tubo7,tubo8,jugador1)
todos.draw(screen)
screen.blit(fondo2,(SCREEN_WIDTH/2-128,SCREEN_HEIGHT/2-128))
pygame.display.flip()
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo1.setPos((D1+SCREEN_WIDTH),ran)
tubo2.setPos((D1+SCREEN_WIDTH),dif)
tubo1.setOut(False)
tubo2.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo3.setPos((D2+SCREEN_WIDTH),ran)
tubo4.setPos((D2+SCREEN_WIDTH),dif)
tubo3.setOut(False)
tubo4.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo5.setPos((D3+SCREEN_WIDTH),ran)
tubo6.setPos((D3+SCREEN_WIDTH),dif)
tubo5.setOut(False)
tubo6.setOut(False)
random.seed()
ran=random.randint(20,120)-Min
dif=SCREEN_HEIGHT/2+ran+100+Min
tubo7.setPos((D4+SCREEN_WIDTH),ran)
tubo8.setPos((D4+SCREEN_WIDTH),dif)
tubo7.setOut(False)
tubo8.setOut(False)
random.seed()
vel=random.randint(3,5)
tubo1.setVel(vel)
tubo2.setVel(vel)
tubo3.setVel(vel)
tubo4.setVel(vel)
tubo5.setVel(vel)
tubo6.setVel(vel)
tubo7.setVel(vel)
tubo8.setVel(vel)
time.sleep(2)
isPlaying=True
#Posibles entredas desde el teclado
for event in pygame.event.get():
if event.type== pygame.QUIT:
GPIO.cleanup()
sys.exit()
elif mov_mouse[1]!=0:
pass
#jugador1.rect.centery=pos_mouse[1]
elif event.type== MOUSEBUTTONDOWN and event.button==1:
if isPlaying==True:
newpos= jugador1.rect.centery
newpos -=30
jugador1.rect.centery = newpos
#Actualizamos la pantalla
#Mostramos la Imagen
if score>highScore:
highScore=score
texto= "Score= %5d HighScore=%5d"%(score,highScore)
mensaje=fuente.render(texto,1,(255,0,255))
todos=pygame.sprite.RenderPlain(tubo1,tubo2,tubo3,tubo4,tubo5,tubo6,tubo7,tubo8,jugador1)
todos.draw(screen)
screen.blit(mensaje,(SCREEN_WIDTH/2-200,15))
pygame.display.flip()
if __name__== "__main__":
main()
Comments