为了账号安全,请及时绑定邮箱和手机立即绑定

在python pygame中得分不变

在python pygame中得分不变

慕妹3146593 2022-09-06 16:54:33
我正在尝试制作乒乓球游戏,但是当球离开屏幕时,比分不会改变。另一件不太对的事情是,有时,球在球拍上滑动,然后离开屏幕,而不是从球拍上反弹。我能就这些问题获得一些帮助吗?(主要是第一个)这是我的代码:import pygame as pgimport randomfrom os import pathimg_dir = path.join(path.dirname(__file__), 'img')snd_dir = path.join(path.dirname(__file__), 'snd')WIDTH = 1280HEIGHT = 690FPS = 60# define colors WHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)YELLOW = (0, 255, 255)OPPONENT_SPEED = 2.4# initialize PyGame and create window pg.init()pg.mixer.init()screen = pg.display.set_mode((WIDTH, HEIGHT))pg.display.set_caption('PONG!')clock = pg.time.Clock()pong_ball_x = [-3, 3]pong_ball_y = [-3, 3]font_name = pg.font.match_font('arial')def draw_text(surf, text, size, x, y):    font = pg.font.Font(font_name, size)    text_surface = font.render(text, True, WHITE)    text_rect = text_surface.get_rect()    text_rect.midtop = (x, y)    surf.blit(text_surface, text_rect)class Player(pg.sprite.Sprite):    def __init__(self):        pg.sprite.Sprite.__init__(self)        self.image = pg.Surface((5, 100))        self.image.fill(WHITE)        self.rect = self.image.get_rect()        self.rect.x = 10        self.rect.y = HEIGHT / 2        self.speedy = 0    def update(self):        self.speedy = 0        keys = pg.key.get_pressed()        if keys[pg.K_s]:            self.speedy = 5        if keys[pg.K_w]:            self.speedy = -5        self.rect.y += self.speedy        if self.rect.bottom >= HEIGHT:            self.rect.bottom = HEIGHT        if self.rect.top <= 0:            self.rect.top = 0class Player2(pg.sprite.Sprite):    def __init__(self):        pg.sprite.Sprite.__init__(self)        self.image = pg.Surface((5, 100))        self.image.fill(WHITE)        self.rect = self.image.get_rect()        self.rect.x = WIDTH - 15        self.rect.y = HEIGHT / 2        self.speedy = 0
查看完整描述

1 回答

?
森林海

TA贡献2011条经验 获得超2个赞

问题是你如何检查球是否离开了屏幕。您当前的检查条件永远不会触发,因为它永远不会等于,因为它是一个整数。pong_ball.rect.leftWIDTH - 12.5


一个简单的调试方法只需打印要检查的值,因此


print(pong_ball.rect.left, WIDTH - 12.5)

将输出:


1264 1267.5

1267 1267.5

1270 1267.5

1273 1267.5

1276 1267.5

1279 1267.5

625 1267.5

628 1267.5

当球向屏幕右侧移动时,其位置将被重置。


因此,您会看到球的位置超过您的极限,而不会触发您的条件。


如果将比较分别更改为 和,则分数将更新。><


但是,这将导致另一个问题,从上面的调试打印中也可以明显看出。有四帧的评分条件为真,因为重置位置检入没有 12.5 像素的回旋余地。这就是当初存在回旋余地的原因吗?你未来的自己可能会喜欢评论。PongBall.update()


但是,如果您更改比较以删除 12.5 像素缓冲区,则分数不会更新。这是因为位置在其方法中更新和重置。pong_ballupdate()


如果我们遵循您的弹跳方法,我们可以添加一个单独的方法,并在满足评分标准时调用该方法。resetPongBall


所以你的类现在是:PongBall


class PongBall(pg.sprite.Sprite):

    def __init__(self):

        pg.sprite.Sprite.__init__(self)

        self.image = pg.Surface((30, 30))

        self.image.fill(WHITE)

        self.rect = self.image.get_rect()

        self.speedx = random.choice(pong_ball_x)

        self.speedy = random.choice(pong_ball_y)

        self.reset()


    def update(self):

        self.rect.x += self.speedx

        self.rect.y += self.speedy

        if self.rect.top <= 0:

            self.speedy = -self.speedy

        if self.rect.bottom >= HEIGHT:

            self.speedy = -self.speedy


    def reset(self):

        self.rect.x = WIDTH / 2 - 15

        self.rect.y = HEIGHT / 2 - 15


    def bounce(self):

        self.speedx = -self.speedx

您的主循环将更改为同时执行重置:


while running:

    # process input (events)

    for event in pg.event.get():

        # check for closing window

        if event.type == pg.QUIT:

            running = False

    # check for score

    if pong_ball.rect.left > WIDTH - 12.5:

        score = score + 1

        pong_ball.reset()

    if pong_ball.rect.right < 12.5:

        score2 = score2 + 1

        pong_ball.reset()

    # update

    ....

那么你的分数应该表现正确。


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号