Ejemplo n.º 1
0
 def __init__(self):
     init()
     self.n = 0
     self.balls = []
     self.player = PLAYER()
     self.target = TARGET()
     self.target.new()
     display.set_caption('Ball Game')
     self.clock = time.Clock()
     self.Font = font.SysFont("arial", 40)
Ejemplo n.º 2
0
    def __init__(self, w, h, mag, root):
        # Please add variables as appropriate
        # Variables of class
        self.WIDTH = w
        self.HEIGHT = h
        self.MAG = mag
        self.root = root
        self.scene_cnt = 0
        self.goal_order = []

        # Variables of function
        self.var_start_menu = (1, None)
        self.var_select_menu = (3, 2, None)

        # Keyboard config
        self.frame = tk.Frame(self.root, width=w, height=h)
        self.frame.bind("<KeyPress>", self.key_pressed)
        self.frame.bind("<KeyRelease>", self.key_released)
        self.frame.focus_set()
        self.frame.pack()
        self.pressed = {}  #pressed key

        # Create canvas
        canvas = tk.Canvas(bg="black", width=self.WIDTH, height=self.HEIGHT)
        canvas.place(x=0, y=0)

        # Array of field
        self.field = FIELD(w, h, mag)

        # Create instance
        self.player = [PLAYER(i) for i in range(4)]
        self.turn = 0

        self.start_menu()
Ejemplo n.º 3
0
 def __init__(self):
     init()
     self.n = 0
     self.balls = []
     self.player = PLAYER()
     self.target = TARGET()
     self.target.new()
     display.set_caption('Ball Game')
     self.clock = time.Clock()
     self.Font = font.SysFont("arial", 40);
Ejemplo n.º 4
0
 def reset(self):
     '''
     Reset objects for a new game
     '''
     bricks.SPEED = 2
     self.bricks = dict()
     self.player = PLAYER() 
     self.n = 0
     self.score = 0
     self.rank = False
     self.msg_center = ""
Ejemplo n.º 5
0
def start():
    global now_turn
    get_data = request.args.to_dict()
    name = get_data.get('name')
    ip = get_data.get('ip')
    print(name)
    player = PLAYER(name, ip)
    dict = start_give(black, white)
    for j in dict:
        player.Hand.append(j)
    players.append(player)
    now_turn = name
    #print(now_turn)
    return 'good luck'
Ejemplo n.º 6
0
 def __init__(self):
     init()
     self.clock = time.Clock()
     self.n = 0  # Asteroid index
     self.pic = image.load('images/back.png').convert()  # background image
     self.quit, self.pause, self.stop = False, False, False
     self.score = 0
     self.inc = 1
     try:    # to read high score from file
         f=open('high.dat','r')
         self.high=load(f)
         f.close()
     except:
         self.high=0
     self.message = "Score : %3d    High : %3d"%(self.score,self.high)
     self.Font = font.SysFont("arial", 40)
     self.msg_center = "" 
     self.bricks = dict()
     self.player = PLAYER()
     self.tick = 100
     self.bg_x = 0
     self.rank = False
     self.bg_sfx = mixer.Sound('sounds/bg.wav')  # Background music
Ejemplo n.º 7
0
 def reset(self):
     self.n = 0
     self.balls = []
     self.player = PLAYER()
Ejemplo n.º 8
0
class Game:
    def __init__(self):
        init()
        self.n = 0
        self.balls = []
        self.player = PLAYER()
        self.target = TARGET()
        self.target.new()
        display.set_caption('Ball Game')
        self.clock = time.Clock()
        self.Font = font.SysFont("arial", 40)

    def put_message(self):
        message = "Balls : %d" % (self.n)
        text = self.Font.render(message, True, (0, 0, 255))
        screen.blit(text, (100, int(.9 * screen.get_height())))
        display.update(
            [[0, int(screen.get_height() * .9)],
             [int(screen.get_width() / 2),
              int(screen.get_height() * .1)]])

    def check(self):
        if (abs(self.target.x - self.player.x) <
                self.target.width / 2 + self.player.width / 2) and (
                    abs(self.target.y - self.player.y) <
                    self.target.width / 2 + self.player.height / 2):
            self.balls.append(BALL())
            self.n += 1
            self.target.play_sound()
            self.target.new()
            self.put_message()
            self.draw()
        for ball in self.balls:
            if (self.player.x - self.player.width / 2 < ball.x <
                    self.player.x + self.player.width / 2) and (
                        self.player.y - self.player.height / 2 < ball.y <
                        self.player.y + self.player.height / 2):
                self.player.play_sound()
                self.stop = True

    def update(self):
        self.player.move()
        for ball in self.balls:
            ball.bounce()
            ball.move()

    def draw(self):
        screen.fill(color_black)
        draw.rect(
            screen, color_white,
            [[0, 0], [int(screen.get_width()),
                      int(screen.get_height() * .9)]])
        self.target.draw()
        self.player.draw()
        for ball in self.balls:
            ball.draw()
        display.update(
            [[0, 0], [int(screen.get_width()),
                      int(screen.get_height() * .9)]])

    def reset(self):
        self.n = 0
        self.balls = []
        self.player = PLAYER()

    def handle_events(self):
        keys = key.get_pressed()
        if keys[K_q] or keys[K_ESCAPE]:
            self.quit = True
        if keys[K_SPACE] and self.stop:
            self.reset()
            self.stop = False
        for evt in event.get():
            if evt.type == QUIT:
                self.quit = True
            if evt.type == MOUSEBUTTONDOWN:
                for ball in self.balls:
                    ball.reverse()

    def run(self):
        self.quit = False
        self.stop = False
        self.put_message()
        while not self.quit:

            self.handle_events()

            if not self.stop:
                self.update()
                self.draw()
                self.check()
            self.clock.tick(100)
Ejemplo n.º 9
0
 def reset(self):
     self.n = 0
     self.balls = []
     self.player = PLAYER()
Ejemplo n.º 10
0
class Game:
    def __init__(self):
        init()
        self.n = 0
        self.balls = []
        self.player = PLAYER()
        self.target = TARGET()
        self.target.new()
        display.set_caption('Ball Game')
        self.clock = time.Clock()
        self.Font = font.SysFont("arial", 40);
    
    def put_message(self):
        message = "Balls : %d"%(self.n)
        text = self.Font.render(message, True, (0, 0, 255))    
        screen.blit(text, (100,int(.9*screen.get_height())))
        display.update([[0,int(screen.get_height()*.9)],[int(screen.get_width()/2),int(screen.get_height()*.1)]])
        
    def check(self):
        if (abs(self.target.x-self.player.x) < self.target.width/2+self.player.width/2) and (abs(self.target.y-self.player.y) < self.target.width/2+self.player.height/2):
            self.balls.append(BALL())
            self.n+=1
            self.target.play_sound()
            self.target.new()
            self.put_message()
            self.draw()
        for ball in self.balls:
            if (self.player.x-self.player.width/2 < ball.x < self.player.x+self.player.width/2) and (self.player.y-self.player.height/2 < ball.y < self.player.y+self.player.height/2):
                self.player.play_sound()
                self.stop = True      
                
    def update(self):
        self.player.move()
        for ball in self.balls:
            ball.bounce()
            ball.move() 
            
    def draw(self):
        screen.fill(color_black)
        draw.rect(screen, color_white, [[0,0],[int(screen.get_width()),int(screen.get_height()*.9)]])
        self.target.draw()
        self.player.draw()
        for ball in self.balls:
            ball.draw()
        display.update([[0,0],[int(screen.get_width()),int(screen.get_height()*.9)]])
        
    
    def reset(self):
        self.n = 0
        self.balls = []
        self.player = PLAYER()
    
    def handle_events(self):
        keys = key.get_pressed()
        if keys[K_q] or keys[K_ESCAPE]:
            self.quit = True
        if keys[K_SPACE] and self.stop:
            self.reset()
            self.stop = False
        for evt in event.get():
            if evt.type == QUIT:
                self.quit = True
            if evt.type == MOUSEBUTTONDOWN:
                for ball in self.balls:
                    ball.reverse()
        
    def run(self):
        self.quit = False
        self.stop = False
        self.put_message()
        while not self.quit:
            
            self.handle_events()
            
            if not self.stop:
                self.update()
                self.draw()
                self.check()
            self.clock.tick(100)
Ejemplo n.º 11
0
class Game:
    def __init__(self):
        init()
        self.clock = time.Clock()
        self.n = 0  # Asteroid index
        self.pic = image.load('images/back.png').convert()  # background image
        self.quit, self.pause, self.stop = False, False, False
        self.score = 0
        self.inc = 1
        try:    # to read high score from file
            f=open('high.dat','r')
            self.high=load(f)
            f.close()
        except:
            self.high=0
        self.message = "Score : %3d    High : %3d"%(self.score,self.high)
        self.Font = font.SysFont("arial", 40)
        self.msg_center = "" 
        self.bricks = dict()
        self.player = PLAYER()
        self.tick = 100
        self.bg_x = 0
        self.rank = False
        self.bg_sfx = mixer.Sound('sounds/bg.wav')  # Background music

    def handle_events(self):    # handling keyboard
        for evt in event.get():
            if evt.type == QUIT:    # quit the game
                self.quit = True
            if evt.type == KEYDOWN:
                if evt.key == K_u and self.stop:
                    try:
                        if not self.rank:
                            r=str(post_score(self.player.name,self.score))
                            self.msg_center = 'Your Global Rank :'+r
                        self.rank = True
                    except:
                        self.rank = False
                        self.msg_center = 'Sorry! Failed to fetch rank'
                if evt.key == K_ESCAPE: # quit the game
                    self.quit = True
                if evt.key == K_p and not self.stop:  # press p for pause
                    self.pause = True
                    self.msg_center = "Press 'Space' to resume" 
                if evt.key == K_SPACE:  # press space to ...
                    if self.pause:  # ...unpause on pressing p
                        self.pause = False
                        self.msg_center = "" 
                    if self.stop:   #  ...start a new game if gameover
                        self.reset()
                        self.stop = False
                
    def put_message(self):
        '''
        Displays self.message
        '''
        self.message = "Score : %3d    High : %3d"%(self.score,self.high)
        text = self.Font.render(self.message, True, (0, 0, 255))    
        screen.blit(text, (WIDTH - 50 - text.get_width(),int(.9*HEIGHT)))
        
    def center_msg(self):
        '''
        Displays self.message
        '''
        text = self.Font.render(self.msg_center, True, (255, 0, 0))    
        screen.blit(text, (int(WIDTH-text.get_width())/2,int(HEIGHT-text.get_height())/2))
        
    def draw(self):
        '''
        Draws every thing on the screen
        '''
        screen.fill((0,0,0))    # Fill screen with black
        # Fill screen with background pattern
        for y in range(0, HEIGHT, self.pic.get_height()):
            for x in range(int(self.bg_x), WIDTH+self.pic.get_width(), self.pic.get_width()):
                screen.blit(self.pic, (x, y))
        for brick in self.bricks.values():  # Draws all the bricks
            brick.draw()
        self.player.draw()  # Draw the player
        self.put_message()
        self.center_msg()
        display.flip()  # Update the display

    def update(self):
        '''
        Changes position of all objects after every frame
        '''
        self.score += self.inc
        if self.score > self.high:
            self.high = self.score                                        
        # move the background
        if not ( self.pause or self.stop ):
            self.bg_x = self.bg_x-1 if self.bg_x > -self.pic.get_width() else 0
        if randint(1,20)==1:    # Bring a new asteroid with 1 in 20 probability
            self.n += 1
            self.bricks[self.n] = BRICK()
        for brick in self.bricks.values():
            brick.move()
        self.player.move()

    def check_bounds(self):
        self.tick, self.inc = 100, 1
        for index, brick in self.bricks.items():
            if brick.position.x + brick.radius < 0:
                del self.bricks[index]
            if ( brick.position-self.player.position ).length < ( brick.size.length/3 + self.player.size.length/3 ):
                self.player.pic = self.player.pic_dead
                self.player.sound.stop()
                self.player.explode.play()
                self.stop = True
                self.msg_center = "'Space'-New Game, 'U'-Upload Score"
            if ( brick.position-self.player.position ).length < ( brick.size.length/2 + self.player.size.length/2 ):
                self.tick,self.inc = 25, 4
                
        if not( self.player.size.y/2 < self.player.position.y < HEIGHT-self.player.size.y/2 ):
            self.player.pic = self.player.pic_dead
            self.player.sound.stop()
            self.player.explode.play()
            self.stop = True
            self.msg_center = "'Space'-New Game, 'U'-Upload Score"
    
    def reset(self):
        '''
        Reset objects for a new game
        '''
        bricks.SPEED = 2
        self.bricks = dict()
        self.player = PLAYER() 
        self.n = 0
        self.score = 0
        self.rank = False
        self.msg_center = ""
    
    def write_scores(self):
        '''
        Writes scores into the file
        '''
        f=open('high.dat','w')
        dump(self.high,f)
        f.close()   
        
    def run(self):
        '''
        Main loop
        '''
        self.bg_sfx.play(-1)
        self.bg_sfx.set_volume(01)
        while not self.quit:
            self.handle_events()
            if not (self.pause or self.stop):
                self.update()
                self.check_bounds()
            self.draw()
            self.clock.tick(self.tick)
        else:
            self.bg_sfx.stop()
            self.write_scores()