玩乒乓球,当你打不中时,我如何更新分数,也会反弹?

2024-09-23 22:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为学校做一个乒乓球游戏,从pygame和学校提供的类导入。我把比赛的大部分时间都搞定了,只不过比分更新了,球从球拍上弹了回来。到目前为止我的代码是这样的,我把它分成了几个部分,我的大部分问题都发生在ball类中:

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

这是评分的函数

^{pr2}$

我创造了球和矩形

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]

更新我的分数定义。在

  def edge_collision1(self):
        if self.center[0] == 50 :

            global score  +=1
            return score
        else:
            pass


main()

分数会显示,但不会正确更新。我知道我的if条件在代码的底部是错误的,但是什么才是正确的呢?我也不知道怎么让它从桨上弹下来,不过我是从边缘弄来的!我删除了一些代码来简化它,保留了所有与我的问题相关的东西。任何帮助都太好了!在


Tags: selfdefwindowsurfacepygamelengthcolorscore