示例#1
0
    def initialize(self):
        self.window.border(left=0, top=5, right=5, bottom=5)
        self.window.set_title('Pong!')

        self.paddle = Paddle()
        self.sprites.add(self.paddle)
        self.ball = Ball()
        self.sprites.add(self.ball)

        self.events.add(KEYDOWN_Event(key=(K_UP, K_q), callback=self.paddle.up))
        self.events.add(KEYUP_Event(key=(K_UP, K_q), callback=self.paddle.noup))
        self.events.add(KEYDOWN_Event(key=(K_DOWN, K_a), callback=self.paddle.down))
        self.events.add(KEYUP_Event(key=(K_DOWN, K_a), callback=self.paddle.nodown))

        self.score = Score(position=(conf.WINWIDTH-195, 40))
        self.sprites.add(self.score)

        self.maxscore = Score(text='Max:', position=(conf.WINWIDTH-170, 70))
        self.sprites.add(self.maxscore)
示例#2
0
class Pong(Game):
    def initialize(self):
        self.window.border(left=0, top=5, right=5, bottom=5)
        self.window.set_title('Pong!')

        self.paddle = Paddle()
        self.sprites.add(self.paddle)
        self.ball = Ball()
        self.sprites.add(self.ball)

        self.events.add(KEYDOWN_Event(key=(K_UP, K_q), callback=self.paddle.up))
        self.events.add(KEYUP_Event(key=(K_UP, K_q), callback=self.paddle.noup))
        self.events.add(KEYDOWN_Event(key=(K_DOWN, K_a), callback=self.paddle.down))
        self.events.add(KEYUP_Event(key=(K_DOWN, K_a), callback=self.paddle.nodown))

        self.score = Score(position=(conf.WINWIDTH-195, 40))
        self.sprites.add(self.score)

        self.maxscore = Score(text='Max:', position=(conf.WINWIDTH-170, 70))
        self.sprites.add(self.maxscore)
    
    def checkCollisions(self):
        if self.ball.collide(self.paddle):
            self.ball.hit()
            self.paddle.hit()
            self.score.addPoints(1)
            self.score.updateScore()

        if not self.ball.onscreen(left=10, jail=1):
            self.ball.onscreen(left=-10, jail=1)
            if self.ball.collide(self.paddle):
                self.checkCollisions()
                return
            self.ball.center()
            self.paddle.set_speed(100)
            self.ball.path.set_velocity(vx=150, vy=0) 
            self.maxscore.set_points(max(self.score.points, self.maxscore.points))
            self.maxscore.updateScore()
            self.score.set_points(0)
            self.score.updateScore()