pygame pong,如果你没有击中,我怎么更新得分,也会反弹?

时间:2017-11-02 03:40:41

标签: python pygame pong

我正在为pygame建造的乒乓球比赛和学校提供进口课程。我得到了大部分比赛,除了得分更新和它从桨上弹跳。到目前为止,这是我的代码,我将其分解为某些部分,我的大多数问题都发生在球类中:

common.feature

以下是绘制分数的功能

class Game:

    def __init__ (self, window):
        self.window=window
        self.bg_color=pygame.Color('black')

        self.pause_time = 0.03
        self.close_clicked = False
        self.continue_game = True        


        surface = window.get_surface()
        print(type(surface))
        pong_color = 'white'
        pong_radius = 5
        pong_center = [int(screensize[0]*0.5), int(screensize[1]*0.5)]
        pong_velocity = [6, 4] 
        self.pong= Ball(pong_color, pong_radius, pong_center, pong_velocity, surface)

        self.bg_color = 'black'
        self.window.set_bg_color(self.bg_color)
        self.fg_color = 'white'
        self.window.set_font_color(self.fg_color)        
        self.score = 0
        paddle_length = 100  
    def paddle_1 ():
        pygame.Rect(100, 100, 100, 100)

    def draw(self): 
        self.window.clear()
        self.pong.draw()
        self.draw_score()
        self.window.update()
    def play(self):
        while not self.close_clicked:
            self.game_run()
            self.draw()
            if self.continue_game:
                self.update()
                self.game_con()
                time.sleep(self.pause_time)

    def draw(self):

        self.window.clear()
        self.pong.draw()
        self.draw_score_left()
        self.draw_score_right()
        self.window.update()           
    def update(self):          
        self.pong.move()

    def game_con(self):
        None

我创建球和矩形的Ba类

def draw_score_left(self):
        score_string = 'Score: ' + str(self.pong.edge_collision1())
        self.window.set_font_size(30)
        self.window.draw_string(score_string, 0, 0)
    def draw_score_right(self):
        score_string = 'Score: ' + str(self.pong.edge_collision1())
        self.window.set_font_size(30)
        self.window.draw_string(score_string, screensize[0]-100, 0)    

我对要更新的分数的定义。

class Ball:
    score =0    

    def __init__(self, color, radius, center, velocity, surface):

        self.color = pygame.Color(color)
        self.radius = radius
        self.center = center
        self.velocity = velocity
        self.surface = surface

    def draw(self):
        paddle_length = 100
        pygame.draw.circle(self.surface, self.color, self.center, self.radius)
        pygame.draw.rect(self.surface, self.color, (int(screensize[0]-50),int(screensize[1]*0.5-paddle_length*0.5),10,paddle_length))
        pygame.draw.rect(self.surface, self.color, (50,int(screensize[1]*0.5-paddle_length*0.5),10,paddle_length))        
    def move(self):
        self.score = 0
        size = self.surface.get_size()
        for coord in range(0,2):
            self.center[coord] = (self.center[coord] + self.velocity[coord]) % size[coord]
            if self.center[coord] < self.radius or self.center[coord] + self.radius > size[coord]:
                self.velocity[coord] = - self.velocity[coord]

分数显示,但无法正确更新。我知道我的条件底部的条件是错误的,但是什么是正确的?我也不确定如何让它从桨叶反弹,我从边缘得到了它!我删除了一些代码来简化它,保留所有与我的问题相关的东西。任何帮助都会很棒!

0 个答案:

没有答案