猿问

在python pygame中得分不变

我正在尝试制作乒乓球游戏,但是当球离开屏幕时,比分不会改变。另一件不太对的事情是,有时,球在球拍上滑动,然后离开屏幕,而不是从球拍上反弹。我能就这些问题获得一些帮助吗?(主要是第一个)这是我的代码:

import pygame as pg

import random

from os import path


img_dir = path.join(path.dirname(__file__), 'img')

snd_dir = path.join(path.dirname(__file__), 'snd')

WIDTH = 1280

HEIGHT = 690

FPS = 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 = 0



class 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



慕妹3146593
浏览 136回答 1
1回答

森林海

问题是你如何检查球是否离开了屏幕。您当前的检查条件永远不会触发,因为它永远不会等于,因为它是一个整数。pong_ball.rect.leftWIDTH - 12.5一个简单的调试方法只需打印要检查的值,因此print(pong_ball.rect.left, WIDTH - 12.5)将输出:1264 1267.51267 1267.51270 1267.51273 1267.51276 1267.51279 1267.5625 1267.5628 1267.5当球向屏幕右侧移动时,其位置将被重置。因此,您会看到球的位置超过您的极限,而不会触发您的条件。如果将比较分别更改为 和,则分数将更新。><但是,这将导致另一个问题,从上面的调试打印中也可以明显看出。有四帧的评分条件为真,因为重置位置检入没有 12.5 像素的回旋余地。这就是当初存在回旋余地的原因吗?你未来的自己可能会喜欢评论。PongBall.update()但是,如果您更改比较以删除 12.5 像素缓冲区,则分数不会更新。这是因为位置在其方法中更新和重置。pong_ballupdate()如果我们遵循您的弹跳方法,我们可以添加一个单独的方法,并在满足评分标准时调用该方法。resetPongBall所以你的类现在是:PongBallclass PongBall(pg.sprite.Sprite):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pg.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.image = pg.Surface((30, 30))&nbsp; &nbsp; &nbsp; &nbsp; self.image.fill(WHITE)&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.image.get_rect()&nbsp; &nbsp; &nbsp; &nbsp; self.speedx = random.choice(pong_ball_x)&nbsp; &nbsp; &nbsp; &nbsp; self.speedy = random.choice(pong_ball_y)&nbsp; &nbsp; &nbsp; &nbsp; self.reset()&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x += self.speedx&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y += self.speedy&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.top <= 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy = -self.speedy&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.bottom >= HEIGHT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy = -self.speedy&nbsp; &nbsp; def reset(self):&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x = WIDTH / 2 - 15&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y = HEIGHT / 2 - 15&nbsp; &nbsp; def bounce(self):&nbsp; &nbsp; &nbsp; &nbsp; self.speedx = -self.speedx您的主循环将更改为同时执行重置:while running:&nbsp; &nbsp; # process input (events)&nbsp; &nbsp; for event in pg.event.get():&nbsp; &nbsp; &nbsp; &nbsp; # check for closing window&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pg.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = False&nbsp; &nbsp; # check for score&nbsp; &nbsp; if pong_ball.rect.left > WIDTH - 12.5:&nbsp; &nbsp; &nbsp; &nbsp; score = score + 1&nbsp; &nbsp; &nbsp; &nbsp; pong_ball.reset()&nbsp; &nbsp; if pong_ball.rect.right < 12.5:&nbsp; &nbsp; &nbsp; &nbsp; score2 = score2 + 1&nbsp; &nbsp; &nbsp; &nbsp; pong_ball.reset()&nbsp; &nbsp; # update&nbsp; &nbsp; ....那么你的分数应该表现正确。
随时随地看视频慕课网APP

相关分类

Python
我要回答