コード例 #1
0
ファイル: game.py プロジェクト: panthorstudios/Gold-Rush
class Game(object):
    TITLE = "Gold Rush!"
    WIDTH=680
    HEIGHT=600
    WAITING = 1
    PLAYING = 2
    FRAMES_PER_SECOND = 30

    board=[]

    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()

        self.sounds=Sounds()


        self.input_handler = InputHandler()
        self.game_screen = GameScreen(self.TITLE,self.WIDTH,self.HEIGHT)
        self.miner=Miner(8,0)
        self.explosion=Explosion(-1,-1)
        self.game_screen.add_sprite(self.miner)
        self.game_screen.add_sprite(self.explosion)
        self.health = 100
         
        self.clock=pygame.time.Clock()
        self.state=self.WAITING
        self.game_screen.display_opening()

 
    def setup(self):
        self.gold=0
        self.charges=10
        self.cash=0
        self.health=100 
        self.board=[]
       
# top row of empty spaces
        self.board.append([' ']*20)
        self.board.append(['*']*20)
        for y in range(2,14):
            row=[]
            for x in range(20):
                c='*'
                if y>1 and random()<0.4:
                    c=' '
                    if random()<0.5:
                        c='0'
                    else:
                        c='1'
                row.append(c)
            self.board.append(row)
        self.miner.set_location(8,0)
        self.game_screen.setup()
        self.game_screen.set_board(self.board)
        self.game_screen.draw_board()



    def mainloop(self):
        deltat=self.clock.tick(self.FRAMES_PER_SECOND)
        running=True
        while running:
            self.input_handler.check()
            if self.input_handler.exit_action:
                running=False
            elif self.state == self.WAITING:
                if self.input_handler.key_press:
                    self.setup()
                    self.state=self.PLAYING
            else:
                self.game_screen.clear_sprites()
                if self.miner.can_move():
                    kpress=self.input_handler.arrow_press

                    if kpress:
                        dx=0
                        dy=0

                        if kpress == K_RIGHT:
                            dx=1
                        elif kpress == K_LEFT:
                            dx=-1
                        elif kpress == K_UP:
                            dy=-1
                        elif kpress == K_DOWN:
                            dy=1

                        if self.input_handler.space_press and (dx!=0 or dy!=0):
                            self.do_explosion(dx,dy)

                        tx=self.miner.x + dx
                        ty=self.miner.y + dy

                        if (dx!=0 or dy!=0) and (tx>=0 and tx<=19 and ty>=0 and ty<=13):
                            o=self.board[ty][tx]
                            if o in ' 01':
                                self.miner.set_location(tx,ty)
                                if o in '01':
                                    self.take_nugget(tx,ty)

                        elif (dy==-1 and tx==17 and ty==-1 and self.gold!=0):
                            self.cash_out()
                            if self.charges==0:
                                self.state=self.WAITING
                                self.input_handler.reset()
                                self.game_screen.display_gameover()



                    if self.miner.y>0:
                        self.health-=1
                        if self.health<0:
                            self.health=0
                        self.game_screen.display_health(self.health)
                    else:
                        self.health+=1
                        if self.health>100:
                            self.health=100
                        self.game_screen.display_health(self.health)
                

            self.game_screen.draw(deltat)
 
        pygame.quit()


    def do_explosion(self,dx,dy):
        bx=self.miner.x + dx
        by=self.miner.y + dy
        if bx>=0 and bx<20 and (by>0 or (by==0 and dy==1)) and by<14 and self.charges>0:

            self.explosion.explode(bx,by)

            self.charges-=1
            self.board[by][bx]=' '
            self.miner.add_delay(20)

            self.game_screen.clear_square(bx,by)
            self.game_screen.display_charges()
            self.sounds.play_boom()
            self.game_screen.display_charges(self.charges)

            for j in range(20):
                x=randint(0,19)
                y=randint(2,11)
                o=self.board[y][x]
                a=self.board[y-1][x]
                if o==' ' and a=='*':
                    self.board[y][x]='*'
                    self.game_screen.reset_square(x,y)

    def cash_out(self):
        self.cash+=self.gold*self.charges
        self.gold=0
        self.sounds.play_kaching()
        self.game_screen.display_gold(self.gold)
        self.game_screen.display_cash(self.cash)
        self.sounds.play_yeehaw()


    def take_nugget(self,tx,ty):
        self.board[ty][tx]=' '
        self.gold += 1
        self.sounds.play_bell()
        self.game_screen.clear_square(tx,ty)
        self.game_screen.display_gold(self.gold)
コード例 #2
0
ファイル: oldgame.py プロジェクト: panthorstudios/Gold-Rush
class Game(object):
    TITLE = "Gold Rush!"
    BOARD_LEFT = 20
    BOARD_TOP = 130
    SQUARE_SIZE = 32
    BLACK = (0,0,0)
    GREEN=(128,255,128)
    YELLOW=(255,255,128)
    RED=(255,128,128)
    FRAMES_PER_SECOND = 30

    ASSAY_X = 540
    ASSAY_Y = 84
    CHARGES_X = 180 
    CASH_X = 20
    CASH_OFFSET = 30 
    GOLD_X = 16
    CHARGES_OFFSET = 32
    HEALTH_X =CHARGES_X + 40
    TITLE_X = 340

    def display_gold(self):
        scoretext='%03d' % self.gold
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),20),(num,0,24,35))

    def display_charges(self):
        scoretext='%02d' % self.charges
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CHARGES_X+self.CHARGES_OFFSET+(i*24),20),(num,0,24,35))

    def display_cash(self):
        scoretext='%05d' % self.cash
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),66),(num,0,24,35))

    def display_health(self):
         h=int(84*(self.health/100.0))
         b=84-h
         c=self.GREEN
         if self.health<20:
             c=self.RED
         elif self.health<40:
             c=self.YELLOW
         self.screen.fill(c,(self.HEALTH_X,70,h,32))
         self.screen.fill(self.BLACK,(self.HEALTH_X+h,70,b,32))
#        num=int(scoretext[i])*24
#        pos=i*24
#        self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),66),(num,0,24,35))


    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()
        self.screen=pygame.display.set_mode((680,600))
        pygame.display.set_caption(self.TITLE)
        self.pressedkey=None
        self.bellsound=pygame.mixer.Sound('assets/sounds/bell.ogg')
        self.chargesound=pygame.mixer.Sound('assets/sounds/bomb.ogg')
        self.yeehawsound=pygame.mixer.Sound('assets/sounds/yeehaw.ogg')
        self.kachingsound=pygame.mixer.Sound('assets/sounds/kaching.ogg')
        self.board=[]
        self.bgbase=pygame.image.load('assets/images/background.png')
        self.bg=pygame.image.load('assets/images/background.png') 
        self.digits=pygame.image.load('assets/images/digits.png') 
        self.gamearea=pygame.Surface(self.bg.get_size())
        self.is_playing=False
 
# currently 2 nugget images
        self.nuggets=[]
        self.nuggets.append(pygame.image.load('assets/images/gold01-%dpx.png' % self.SQUARE_SIZE))
        self.nuggets.append(pygame.image.load('assets/images/gold02-%dpx.png' % self.SQUARE_SIZE))

        self.explosion=Explosion(0,0,self.SQUARE_SIZE)
        self.explosion_group=pygame.sprite.RenderPlain(self.explosion)

        self.miner=Miner(0,0)

        self.clock=pygame.time.Clock()

# add title
        text=pygame.image.load('assets/images/text_title.png')
        self.screen.blit(text,(self.TITLE_X,self.BOARD_LEFT))

# add assay office
        self.office=pygame.image.load('assets/images/assayoffice.png')
        self.screen.blit(self.office,(self.ASSAY_X+self.BOARD_LEFT,self.ASSAY_Y))

        self.cash=0
        self.gold=0
        self.charges=10
        self.health=100

# add "Gold"
        text=pygame.image.load('assets/images/nugget.png')
        self.screen.blit(text,(self.GOLD_X,self.BOARD_LEFT))
        self.display_gold()

# add "Cash"
        text=pygame.image.load('assets/images/text_cash.png')
        self.screen.blit(text,(self.CASH_X,66))
        self.display_cash()

# add "Charges"
        text=pygame.image.load('assets/images/dynamite.png')
        self.screen.blit(text,(self.CHARGES_X,16))
        self.display_charges()

# add "Miner head"
        text=pygame.image.load('assets/images/miner_head.png')
        self.screen.blit(text,(self.CHARGES_X,66))
        self.display_health()


        self.setup()



    def setup(self):

# initialize score items
        self.cash=0
        self.gold=0
        self.charges=10

# load background image every time
        self.bg=pygame.image.load('assets/images/background.png')

#redraw assay office        
        self.bg.blit(self.office,(self.ASSAY_X,self.ASSAY_Y-self.BOARD_TOP))

        self.board=[]
# top row of empty spaces
        pathsup=2
        self.board.append([' ']*20)
        self.board.append(['*']*20)
        for y in range(2,14):
            row=[]
            for x in range(20):
                c='*'
                if random()<0.4:
# make a hole
                    self.bg.fill(self.BLACK,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    c=' '
                    if y>1:
                        c='G'
                        nugg=self.nuggets[0 if random()<0.5 else 1] 
                        self.bg.blit(nugg,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE))
                row.append(c)
            self.board.append(row)



# add soil
        self.gamearea.blit(self.bg,(0,0))
        pygame.display.flip()
      

 
    def print_board(self):
        for row in self.board:
            print ' '.join(row)




    def mainloop(self):
        deltat=self.clock.tick(self.FRAMES_PER_SECOND)
        tx=self.miner.x
        ty=self.miner.y
        self.miner_group.clear(self.gamearea,self.bg)
        self.explosion_group.clear(self.gamearea,self.bg)
 
        pressedspace=False
        for event in pygame.event.get():
            #print event
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit(0) 
                elif event.key in (K_RIGHT,K_LEFT,K_UP,K_DOWN):
                    self.pressedkey= event.key
                elif event.key == K_SPACE:
                    pressedspace = True
            elif event.type == KEYUP:
                if event.key in (K_RIGHT,K_LEFT,K_UP,K_DOWN):
                    if self.pressedkey == event.key:
                        self.pressedkey = None
                #elif event.key == K_SPACE:
                    #pressedspace = False

# only draw explosion if necessary
        if self.explosion.update(deltat):
            self.explosion_group.update(deltat)
            self.explosion_group.draw(self.gamearea)

        else:
            if pressedspace and self.pressedkey:
# Do explosion
                pressedspace=False
                bx=self.miner.x
                by=self.miner.y
                if self.pressedkey == K_LEFT:
                    bx-=1
                if self.pressedkey == K_RIGHT:
                    bx+=1
                if self.pressedkey == K_UP:
                    by-=1
                if self.pressedkey == K_DOWN:
                    by+=1

                if bx>=0 and bx<20 and (by>0 or (by==0 and self.pressedkey == K_DOWN)) and by<20 and self.charges>0:
                    self.explosion.explode(bx,by)
                    self.charges-=1
#                    print "(%d,%d)->(%d,%d) Boom! %d charges left." % (self.miner.x,self.miner.y,bx,by,self.charges)
                    self.board[by][bx]=' '
                    self.bg.fill(self.BLACK,(bx*self.SQUARE_SIZE,by*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    self.gamearea.blit(self.bg,(0,0))
                    self.display_charges()
                    #self.screen.blit(self.digits,(460+(i*24),20),(num,0,24,35))
                    self.chargesound.play()
                    for j in range(20):
                        x=randint(0,19)
                        y=randint(2,11)
                        o=self.board[y][x]
                        a=self.board[y-1][x]
                        if o==' ' and a=='*':
                            self.board[y][x]='*'
                            xpos=x*self.SQUARE_SIZE
                            ypos=y*self.SQUARE_SIZE
                            self.bg.blit(self.bgbase,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE),(xpos,ypos,self.SQUARE_SIZE,self.SQUARE_SIZE))
 
            if self.pressedkey == K_RIGHT and self.miner.can_move():
                if tx<19:
                    tx += 1
 
            if self.pressedkey == K_LEFT and self.miner.can_move(): 
                if tx>0:
                    tx -= 1
            if self.pressedkey == K_UP and self.miner.can_move():
                if ty>0:
                    ty -= 1
                else:
                    if tx==17:
                        if self.gold!=0:
                            self.cash+=self.gold*self.charges
                            self.gold=0
                            self.kachingsound.play()
                            self.display_gold()
                            self.display_cash()
                            self.yeehawsound.play()
                            
            if self.pressedkey == K_DOWN and self.miner.can_move():
                if ty<13:
                    ty += 1

            
            o=self.board[ty][tx]

            if (tx!=self.miner.x or ty!=self.miner.y) and o in ' G':
                self.miner.set_location(tx,ty)
                if o=='G':
                    self.board[ty][tx]=' '
                    self.gold += 1
                    self.bellsound.play()
                    self.bg.fill(self.BLACK,(self.miner.x*self.SQUARE_SIZE,self.miner.y*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    self.gamearea.blit(self.bg,(0,0))
                self.display_gold()

            self.miner.update_move()
            self.miner_group.update(deltat)
            self.miner_group.draw(self.gamearea)
        if self.miner.y>0:
            self.health-=0.25
            if self.health<0:
                self.health=0
                pass
            self.display_health()
        else:
            self.health+=1
            if self.health>100:
                self.health=100
            self.display_health()
  
        self.screen.blit(self.gamearea,(self.BOARD_LEFT,self.BOARD_TOP))

        pygame.display.flip()