【问题标题】:Pygame renders text over old textPygame 在旧文本上渲染文本
【发布时间】:2021-05-10 19:10:11
【问题描述】:

我有以下函数可以绘制到 pygame 窗口:

    base.draw(win, rocket.in_space)
win.blit(rocket_img, (120, 150))
if rocket.in_space:
    velocity_label = STAT_FONT.render(f"Velocity: {round(rocket.velocity, 3)} km/s", True, (255, 255, 255))
    acceleration_label = STAT_FONT.render(f"Acceleration: {round(rocket.acceleration, 3)} km/s^2", True, (255, 255, 255))
    altitude_label = STAT_FONT.render(f"Altitude: {round(rocket.altitude, 3)} km", True, (255, 255, 255))
    fuel_label = STAT_FONT.render(f"Fuel: {round(rocket.fuel, 2)} kg", True, (255, 255, 255))
    time_label = STAT_FONT.render(f"Time: {round(rocket.time_passed, 2)} s", True, (255, 255, 255))
else:
    velocity_label = STAT_FONT.render(f"Velocity: {round(rocket.velocity, 3)} km/s", True, (0, 0, 0))
    acceleration_label = STAT_FONT.render(f"Acceleration: {round(rocket.acceleration, 3)} km/s^2", True, (0, 0, 0))
    altitude_label = STAT_FONT.render(f"Altitude: {round(rocket.altitude, 3)} km", True, (0, 0, 0))
    fuel_label = STAT_FONT.render(f"Fuel: {round(rocket.fuel, 2)} kg", True, (0, 0, 0))
    time_label = STAT_FONT.render(f"Time: {round(rocket.time_passed, 2)} s", True, (0, 0, 0))
win.blit(velocity_label, (0, 0))
win.blit(acceleration_label, (0, 50))
win.blit(altitude_label, (0, 100))
win.blit(fuel_label, (0, 150))
win.blit(time_label, (0, 200))
pygame.display.update()

这段代码让背景移动:

class Base:
VEL = 5
HEIGHT = bg_img.get_height()
IMG = bg_img
space = space_img

def __init__(self, x):
    self.x = x
    self.y1 = 0
    self.y2 = self.HEIGHT

def move(self, vel):
    self.VEL = vel
    self.y1 -= self.VEL
    self.y2 -= self.VEL
    if self.y1 + self.HEIGHT < 0:
        self.y1 = self.y2 + self.HEIGHT

    if self.y2 + self.HEIGHT < 0:
        self.y2 = self.y1 + self.HEIGHT

def draw(self, win, in_space):
    if in_space:
        win.blit(self.space, (self.x, self.y2))
        win.blit(self.space, (self.x, self.y1))
    else:
        win.blit(self.IMG, (self.x, self.y2))
        win.blit(self.IMG, (self.x, self.y1))

但是,当背景开始加速时,文本开始呈现在自身之上。我不确定为什么会这样。非常感谢任何帮助。

编辑: 基地移动的速度随着时间的推移而增加

这是屏幕冻结时的样子。

【问题讨论】:

  • 这是一个很长很复杂的程序,但相关部分就是这些,而且速度会随着时间的推移而增加。
  • 没有更多相关代码,但我添加了它的截图
  • 我认为绘制它可以解决这个问题?有没有特定的线路可以做到这一点?
  • 我没有设置背景,所以我认为是,但我希望背景图像可见。

标签: python python-3.x pygame pygame-surface


【解决方案1】:

问题是计算背景的 y 坐标。如果self.VEL 大于HEIGHT,算法就会失效。使用取模 (%) 运算符计算 y 坐标:

class Base:
    VEL = 5
    HEIGHT = bg_img.get_height()
    IMG = bg_img
    space = space_img

    def __init__(self, x):
        self.x = x
        self.y = 0

    def move(self, vel):
        self.VEL = vel
        self.y = (self.y - self.VEL) % self.HEIGHT
        
    def draw(self, win, in_space):
    if in_space:
        win.blit(self.space, (self.x, self.y - self.HEIGHT))
        win.blit(self.space, (self.x, self.y))
    else:
        win.blit(self.IMG, (self.x, self.y - self.HEIGHT))
        win.blit(self.IMG, (self.x, self.y))

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 2017-06-21
    • 2017-06-20
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多