当前位置: 首页>>代码示例>>Python>>正文


Python Ball.check_collision方法代码示例

本文整理汇总了Python中ball.Ball.check_collision方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.check_collision方法的具体用法?Python Ball.check_collision怎么用?Python Ball.check_collision使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ball.Ball的用法示例。


在下文中一共展示了Ball.check_collision方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import check_collision [as 别名]
def main():
    # Initialisation
    pygame.init()
    fps_clock = pygame.time.Clock()

    width, height = size = 700, 700
    black = 0, 0, 0
    screen = pygame.display.set_mode(size)

    ball = Ball()

    boxes = []
    rows = 5
    row = 0
    columns = 17
    column = 0
    while row < rows:
        row = row + 1
        while column < columns:
            column = column + 1
            boxes.append(Box(column,row))
        column = 0

    # Game Loop Start
    while 1:

        #Events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        #Logic
        ball.move()
        broken_box = ball.check_collision(size, boxes)
        if broken_box is not None:
            i = 0
            new_boxes = []
            for each in boxes:
                if broken_box != i:
                    new_boxes.append(each)
                i = i + 1
            boxes = new_boxes

        #Render
        for each in boxes:
            screen.blit(each.image, each.rect)
        screen.blit(ball.image, ball.rect)

        pygame.display.flip()
        screen.fill(black)
        fps_clock.tick(60)
开发者ID:RyongBo,项目名称:Ball-Bounce-Animation,代码行数:53,代码来源:BoxPong.py

示例2: Game

# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import check_collision [as 别名]
class Game(pyglet.window.Window):
    running = False
    racket_left = None
    racket_right = None
    racket_me = None
    score = None
    master_client = False
    multiplayer_mode = False

    def __init__(self, multiplayer_mode = False):
        self.load_sprites()
        self.multiplayer_mode = multiplayer_mode
        if self.multiplayer_mode:
            self.me, self.conn = connect()

    def draw(self):
        if self.multiplayer_mode:
            self.draw_multiplayer()
        else:
            self.draw_singleplayer()

    def run(self):
        if not self.running:
            pyglet.clock.schedule_interval(self.ball.moving, .005)
            self.running = True

    def pause(self):
        if self.running:
            pyglet.clock.unschedule(self.ball.moving)
            self.running = False

    def load_sprites(self):
        self.score = pyglet.text.Label('', font_size=15, x=settings.WINDOW_WIDTH/2, y=settings.WINDOW_HEIGHT - 15, anchor_x='center', anchor_y='center')
        self.racket_left = Racket(pyglet.resource.image(settings.RACKET_IMG)).center_anchor_y(settings.WINDOW_HEIGHT)
        self.racket_right = Racket(pyglet.resource.image(settings.RACKET_IMG)).center_anchor_y(settings.WINDOW_HEIGHT)
        self.ball = Ball(pyglet.resource.image(settings.BALL_IMG)).center_anchor_y(settings.WINDOW_HEIGHT).center_anchor_x(settings.WINDOW_WIDTH)
        self.racket_right.x = settings.WINDOW_WIDTH - self.racket_right.width
        self.racket_me = self.racket_left

    def define_players(self, server_response):
        if self.me == sorted(server_response.keys())[0]: #the first client connection
            self.master_client = True
            self.racket_me = self.racket_left
            self.racket_vs = self.racket_right
            self.score.text = 'Im master'
        else:
            self.racket_me = self.racket_right
            self.racket_vs = self.racket_left
            self.score.text = 'Im slave'

    def on_collision(self):
        player = self.ball.check_collision([self.racket_left, self.racket_right])
        if player:
            self.ball.hit_racket()
            player.increase_score()
            self.ball.prevent_stick(player)
        if self.ball.check_collision_laterals(settings.WINDOW_HEIGHT):
            self.ball.hit_lateral()

    def update_server_data(self):
        data = {
            "ball": {
                "x": self.ball.x,
                "y": self.ball.y,
            },
            "racket": {
                "x": self.racket_me.x,
                "y": self.racket_me.y,
            }
        }
        self.conn.send(simplejson.dumps(data))
        return simplejson.loads(self.conn.recv(2000))

    def update_multiplayer_positions(self, data):
        for playerid in data.keys():
            try:
                if playerid != self.me:
                    self.racket_vs.y = data[playerid]['racket']['y']
                    if not self.master_client:
                        self.ball.x = data[playerid]['ball']['x']
                        self.ball.y = data[playerid]['ball']['y']
            except:
                pass

    def draw_multiplayer(self):
        data = self.update_server_data()
        self.define_players(data)

        if self.master_client:
            if len(data.keys()) == 2:
                self.run()
            else:
                self.pause()

        if self.master_client:
            self.on_collision()

        self.update_multiplayer_positions(data)

    def draw_singleplayer(self):
#.........这里部分代码省略.........
开发者ID:joni2back,项目名称:pong-multiplayer,代码行数:103,代码来源:game.py


注:本文中的ball.Ball.check_collision方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。