コード例 #1
0
 def __init__(self, resource, song, screen, input, beatsPerBoard = 2):
     self.input = input
     self.song = song
     self.screen = screen
     self.downLeft = pygame.image.load(resource.fileName("images", "DownLeft.png"))
     self.downRight = pygame.image.load(resource.fileName("images", "DownRight.png"))
     self.upLeft = pygame.image.load(resource.fileName("images", "UpLeft.png"))
     self.upRight = pygame.image.load(resource.fileName("images", "UpRight.png"))
     
     self.downLeftActive = pygame.image.load(resource.fileName("images", "DownLeftActive.png"))
     self.downRightActive = pygame.image.load(resource.fileName("images", "DownRightActive.png"))
     self.upLeftActive = pygame.image.load(resource.fileName("images", "UpLeftActive.png"))
     self.upRightActive = pygame.image.load(resource.fileName("images", "UpRightActive.png"))
     
     self.center = pygame.image.load(resource.fileName("images", "Center.png"))    
     self.glow = pygame.image.load(resource.fileName("images", "Glow.png")) 
     
     # ALL IMAGES HAVE SAME HEIGHT AND WIDTH
     Constants.SPRITE_SIZE = self.glow.get_width()
     self.beatsPerBoard = beatsPerBoard
     self.backgroundColor = (0,0,0)
     self.score = 0
     self.font = PyGameHieroFont(resource.fileName("font", "font.fnt"))
     
     self.opacityGlow = []
     for i in range(0,4):
         self.opacityGlow.append(0)
     
     self.calculateCenterTargetsDistance()
     
     Constants.SQRT_2 = math.sqrt(2)
     
     self.targetsHypotenuse = math.sqrt(2*(self.centerTargetsDistance-Constants.SPRITE_SIZE)*(self.centerTargetsDistance-Constants.SPRITE_SIZE))
     self.arrowTimeRelation = self.beatsPerBoard/self.targetsHypotenuse
     
     self.sceneClock = pygame.time.Clock()
     
 
     self.song.play()
コード例 #2
0
ファイル: demo.py プロジェクト: sseemayer/PyHiero
from pyhiero.pygamefont import PyGameHieroFont
import pygame, sys
from pygame.locals import *
import math

# init pygame
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption('Hello World!')

# striped background to show off alpha blending
background = pygame.image.load("demo_bg.png")

# load some fonts
fonts = {
    "cabinsketch": PyGameHieroFont("examples/CabinSketch/CabinSketch.fnt"),
    "ranchers": PyGameHieroFont("examples/Ranchers/Ranchers.fnt"),
    "engagement": PyGameHieroFont("examples/Engagement/Engagement.fnt")
}

fnt_header = PyGameHieroFont("examples/Electrolize/Electrolize40.fnt")
fnt_body = PyGameHieroFont("examples/Electrolize/Electrolize16.fnt")

# create some demo text
blah = "Quick zephyrs blow, vexing daft Jim."

colors = {
    "cabinsketch": (128, 0, 0),
    "ranchers": (0, 128, 0, 128)  # alpha! 
    ,
    "engagement": (0, 0, 128)
コード例 #3
0
 def __init__(self, input, game_brain, resource):
     super(Gameplay, self).__init__()
     self.input = input
     self.game_brain = game_brain
     self.fontScore = PyGameHieroFont(resource.fileName("font", "font.fnt"))
コード例 #4
0
class Gameplay(GameState):
    def __init__(self, input, game_brain, resource):
        super(Gameplay, self).__init__()
        self.input = input
        self.game_brain = game_brain
        self.fontScore = PyGameHieroFont(resource.fileName("font", "font.fnt"))

    def startup(self, persistent):
        self.game_brain.start_challenge()

    def update(self, dt):
        self.input.run()
        timeout = self.game_brain.tick_time(dt)
        if timeout:
            self.game_brain.end_challenge(self.input.getCurrentPose())
            self.next_state = "SHOW_CHALLENGE_SCORE"
            self.done = True

    def draw(self, surface):
        # Mostrar camara de fondo
        surface.fill(pg.Color("black"))
        frame = self.input.getCurrentFrameAsImage()
        surface.blit(frame, (0, 0))

        #Mostrar tablero
        margin = self.game_brain.margin
        max_height = Constants.SCREEN_HEIGHT - margin
        max_width = Constants.SCREEN_WIDTH - margin
        board_size = self.game_brain.board_size

        for i in range(1, board_size):
            Point.draw_dashed_line(
                surface, (29, 53, 87),
                ((margin + (max_width - margin) * i / board_size), margin),
                ((margin + (max_width - margin) * i / board_size), max_height),
                3, 20)
            Point.draw_dashed_line(
                surface, (29, 53, 87),
                (margin, margin + ((max_height - margin) * i / board_size)),
                (max_width, margin + ((max_height - margin) * i / board_size)),
                3, 20)

        size_grid_x = (Constants.SCREEN_WIDTH - 2 * margin) / board_size
        size_grid_y = (Constants.SCREEN_HEIGHT - 2 * margin) / board_size
        for pos_x, pos_y in self.game_brain.get_target_coords():
            gradients.draw_circle(
                surface, (pos_x, pos_y),
                (pos_x + size_grid_x / 5, pos_y + size_grid_y / 5),
                (29, 53, 87, 190), (241, 250, 238, 190))

        font_name = Constants.FONT_NAME

        # Mostrar timer
        #self.game_brain.getTimeLeft() = 30

        clock_img = pg.image.load('clock.png').convert_alpha()
        clock_img = pg.transform.scale(clock_img, (100, 100))
        surface.blit(clock_img,
                     (Constants.SCREEN_WIDTH - 230,
                      self.game_brain.margin / 2 - clock_img.get_height() / 2))

        fontPlayerName = pg.font.SysFont(font_name, 50)
        showName = fontPlayerName.render(
            str(round(self.game_brain.get_time_left() / 1000, 1)), True,
            (0, 0, 0))
        surface.blit(showName,
                     (Constants.SCREEN_WIDTH - 120,
                      self.game_brain.margin / 2 - showName.get_height() / 2))

        # Mostrar nombre de jugador

        player_img = pg.image.load('player.png').convert_alpha()
        player_img = pg.transform.scale(player_img, (70, 70))
        surface.blit(
            player_img,
            (90, self.game_brain.margin / 2 - player_img.get_height() / 2))

        fontPlayerName = pg.font.SysFont(font_name, 50)
        showName = fontPlayerName.render(self.game_brain.playerName, True,
                                         (255, 255, 255))
        surface.blit(
            showName,
            (180, self.game_brain.margin / 2 - showName.get_height() / 2))

        #Mostrar puntaje
        scoreImage = self.fontScore.render(str(self.game_brain.score))
        surface.blit(
            scoreImage,
            (Constants.SCREEN_WIDTH / 2 - scoreImage.get_width() / 2, 20))
コード例 #5
0
ファイル: demo.py プロジェクト: sseemayer/PyHiero
# init pygame
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("Hello World!")

# striped background to show off alpha blending
background = pygame.image.load("demo_bg.png")

# load some fonts
fonts = {
    "cabinsketch": PyGameHieroFont("examples/CabinSketch/CabinSketch.fnt"),
    "ranchers": PyGameHieroFont("examples/Ranchers/Ranchers.fnt"),
    "engagement": PyGameHieroFont("examples/Engagement/Engagement.fnt"),
}

fnt_header = PyGameHieroFont("examples/Electrolize/Electrolize40.fnt")
fnt_body = PyGameHieroFont("examples/Electrolize/Electrolize16.fnt")


# create some demo text
blah = "Quick zephyrs blow, vexing daft Jim."

colors = {"cabinsketch": (128, 0, 0), "ranchers": (0, 128, 0, 128), "engagement": (0, 0, 128)}  # alpha!

samples = {fontname: fnt.render(blah, color=colors[fontname]) for fontname, fnt in fonts.items()}

# split doctext by lines
docsplit = __doc__.split("\n")

# first line of doctext is header
headertext = fnt_header.render(docsplit[0], color=(0, 0, 0))
コード例 #6
0
class Scene():
    def __init__(self, resource, song, screen, input, beatsPerBoard = 2):
        self.input = input
        self.song = song
        self.screen = screen
        self.downLeft = pygame.image.load(resource.fileName("images", "DownLeft.png"))
        self.downRight = pygame.image.load(resource.fileName("images", "DownRight.png"))
        self.upLeft = pygame.image.load(resource.fileName("images", "UpLeft.png"))
        self.upRight = pygame.image.load(resource.fileName("images", "UpRight.png"))
        
        self.downLeftActive = pygame.image.load(resource.fileName("images", "DownLeftActive.png"))
        self.downRightActive = pygame.image.load(resource.fileName("images", "DownRightActive.png"))
        self.upLeftActive = pygame.image.load(resource.fileName("images", "UpLeftActive.png"))
        self.upRightActive = pygame.image.load(resource.fileName("images", "UpRightActive.png"))
        
        self.center = pygame.image.load(resource.fileName("images", "Center.png"))    
        self.glow = pygame.image.load(resource.fileName("images", "Glow.png")) 
        
        # ALL IMAGES HAVE SAME HEIGHT AND WIDTH
        Constants.SPRITE_SIZE = self.glow.get_width()
        self.beatsPerBoard = beatsPerBoard
        self.backgroundColor = (0,0,0)
        self.score = 0
        self.font = PyGameHieroFont(resource.fileName("font", "font.fnt"))
        
        self.opacityGlow = []
        for i in range(0,4):
            self.opacityGlow.append(0)
        
        self.calculateCenterTargetsDistance()
        
        Constants.SQRT_2 = math.sqrt(2)
        
        self.targetsHypotenuse = math.sqrt(2*(self.centerTargetsDistance-Constants.SPRITE_SIZE)*(self.centerTargetsDistance-Constants.SPRITE_SIZE))
        self.arrowTimeRelation = self.beatsPerBoard/self.targetsHypotenuse
        
        self.sceneClock = pygame.time.Clock()
        
    
        self.song.play()

    def blitAlpha(self, source, location, opacity):
        x = location[0]
        y = location[1]
        temp = pygame.Surface((source.get_width(), source.get_height())).convert()
        temp.blit(self.screen, (-x, -y))
        temp.blit(source, (0, 0))
        temp.set_alpha(opacity)        
        self.screen.blit(temp, location)         
    
    def handleNotes(self):
        pos = self.song.getPosition()
        track = self.song.track
        period = self.song.period
        delay = self.song.delay
            
        for time, event in track.getUnplayedNotes(pos - delay - self.screenDelay - period * 2 , pos - delay - self.screenDelay + period * self.beatsPerBoard):
           
            delta = time - pos + delay + self.screenDelay
            delta = delta / period
            self.renderNote(event.number, delta)

    def renderNote(self, number, delta):
        d = delta/self.arrowTimeRelation
        d2 = d/Constants.SQRT_2
        if number == 0:
            self.screen.blit(self.upLeft, (d2,d2))
        elif number == 1:
            self.screen.blit(self.upRight, (Constants.SCREEN_WIDTH-d2-Constants.SPRITE_SIZE,d2))
        elif number == 2:
            self.screen.blit(self.downLeft, (d2,Constants.SCREEN_HEIGHT-d2-Constants.SPRITE_SIZE))
        elif number == 3:
            self.screen.blit(self.downRight, (Constants.SCREEN_WIDTH-d2-Constants.SPRITE_SIZE,Constants.SCREEN_HEIGHT-d2-Constants.SPRITE_SIZE))
    
    def calculateCenterTargetsDistance(self):
        if Constants.SCREEN_HEIGHT/2 > Constants.SCREEN_WIDTH/2:
            self.centerTargetsDistance = Constants.SCREEN_WIDTH/2
        else:
            self.centerTargetsDistance = Constants.SCREEN_HEIGHT/2
    
    def renderCenterTargets(self):
        self.screen.blit(self.center, (self.centerTargetsDistance - Constants.SPRITE_SIZE, self.centerTargetsDistance - Constants.SPRITE_SIZE))
        self.screen.blit(self.center, (Constants.SCREEN_WIDTH - self.centerTargetsDistance, self.centerTargetsDistance - Constants.SPRITE_SIZE))
        self.screen.blit(self.center, (self.centerTargetsDistance - Constants.SPRITE_SIZE,Constants.SCREEN_HEIGHT - self.centerTargetsDistance))
        self.screen.blit(self.center, (Constants.SCREEN_WIDTH- self.centerTargetsDistance,Constants.SCREEN_HEIGHT- self.centerTargetsDistance))
        
    def renderCornerTargets(self):

        if self.input.isActive(0):
            self.screen.blit(self.upLeftActive, (0,0))
        else:
            self.screen.blit(self.upLeft, (0,0))
            
        if self.input.isActive(1):
            self.screen.blit(self.upRightActive, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,0))
        else:
            self.screen.blit(self.upRight, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,0))
            
        if self.input.isActive(2):
            self.screen.blit(self.downLeftActive, (0,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE))
        else:
            self.screen.blit(self.downLeft, (0,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE))
            
        if self.input.isActive(3):
            self.screen.blit(self.downRightActive, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE))
        else:
            self.screen.blit(self.downRight, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE))
     
    
    def renderWebCam(self):
        frame = self.input.getCurrentFrameAsImage()
        self.screen.blit(frame, (0,0))
    
    def render(self):
        self.renderWebCam()
        self.renderCornerTargets()
        self.handleNotes()
        self.renderCenterTargets()   
        self.renderGlow()
        self.fadeGlow()
        self.renderScore()
        
    def renderGlow(self):
        self.blitAlpha(self.glow, (0,0), self.opacityGlow[0])
        self.blitAlpha(self.glow, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,0), self.opacityGlow[1])
        self.blitAlpha(self.glow, (0,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE), self.opacityGlow[2])
        self.blitAlpha(self.glow, (Constants.SCREEN_WIDTH-Constants.SPRITE_SIZE,Constants.SCREEN_HEIGHT-Constants.SPRITE_SIZE), self.opacityGlow[3])
        
        
    def fadeGlow(self):
        for i in range(0,4):
            if self.opacityGlow[i] > 0:
                self.opacityGlow[i] -= Constants.GLOW_FADE
            
        
        
    
    def handleRequiredNotes(self, notes):
        arrows = [False, False, False, False]   
        for time, note in notes:
            if self.input.toggled(note.number):   
                if arrows[note.number] == False:
                    self.score += 50
                    note.played = True
                    arrows[note.number] = True
                    self.opacityGlow[note.number] = 255
                
                
                     
    def renderScore(self):
        scoreImage = self.font.render(str(self.score))
        self.screen.blit(scoreImage, 
                         (Constants.SCREEN_WIDTH/2 - scoreImage.get_width()/2, 0))
    
    
    def updateScore(self):
        pos = self.song.getPosition()
        track = self.song.track
        period = self.song.period
        delay = self.song.delay
        margin = period * 0.5      

        requiredNotes = track.getNotes(pos - delay - self.screenDelay - margin, pos - delay - self.screenDelay + margin)           
        update = [True, True, True, True]
        for time, event in requiredNotes:
            update[event.number] = False
        self.input.adapt(update)
        requiredNotes = track.getUnplayedNotes(pos - delay - self.screenDelay - margin, pos - delay - self.screenDelay + margin)                   
        self.handleRequiredNotes(requiredNotes)
        
    
    
    
    def run(self):
        self.screenDelay = self.sceneClock.tick()
        self.screen.fill(self.backgroundColor)
        self.updateScore()
        self.render()
        pygame.display.flip()