亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pygame 不允許我將 float 用于 rect.move,但我需要它

Pygame 不允許我將 float 用于 rect.move,但我需要它

互換的青春 2023-06-06 10:26:33
我最近在 Python 3 和 Pygame 中重新創建了一個版本的Lunar Lander(你知道,舊的復古游戲):rect.move由于重力,我的著陸器沿 y 軸移動 (???) 每一幀。\問題:在我達到 1 m/s 之前,添加到 rect.move 的 y 值是一個小于 1 的浮點數:我必須使用int()它來向上舍入,因為 pygame 不喜歡浮點數。在以前的 Tkinter 版本中,著陸器的 y 坐標是這樣的:0.010.02...0.7651.031.45...在pygame中它是000...11122...這真的很煩人,因為運動不流暢。有人知道如何解決這個問題嗎?比如,輸入一個浮點數到rect.move?提前致謝!
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

由于pygame.Rect應該代表屏幕上的一個區域,一個pygame.Rect對象只能存儲整數數據:

Rect 對象的坐標都是整數。[...]

如果要以浮點精度存儲對象位置,則必須將對象的位置分別存儲在單獨的變量和屬性中,并同步對象pygame.Rectround坐標并將其分配給.topleft矩形的位置(例如):

x,?y?=?#?floating?point?coordinates
rect.topleft?=?round(x),?round(y)

因此(x, y)是準確的位置并且(rect.x, rect.y)包含最接近該位置的整數。

請參見以下示例。紅色物體是通過直接改變物體的位置來移動的pygame.Rect,而綠色物體的位置存儲在單獨的屬性中,移動是用浮點精度計算的。你可以清楚地看到紅色物體在方向和速度方面的不準確:

import pygame


class RedObject(pygame.sprite.Sprite):

? ? def __init__(self, p, t):

? ? ? ? super().__init__()

? ? ? ? self.image = pygame.Surface((20, 20), pygame.SRCALPHA)

? ? ? ? pygame.draw.circle(self.image, "red", (10, 10), 10)

? ? ? ? self.rect = self.image.get_rect(center = p)

? ? ? ? self.move = (pygame.math.Vector2(t) - p).normalize()

? ? def update(self, window_rect):

? ? ? ? self.rect.centerx += self.move.x * 2

? ? ? ? self.rect.centery += self.move.y * 2

? ? ? ? if not window_rect.colliderect(self.rect):

? ? ? ? ? ? self.kill()


class GreenObject(pygame.sprite.Sprite):

? ? def __init__(self, p, t):

? ? ? ? super().__init__()

? ? ? ? self.image = pygame.Surface((20, 20), pygame.SRCALPHA)

? ? ? ? pygame.draw.circle(self.image, "green", (10, 10), 10)

? ? ? ? self.rect = self.image.get_rect(center = p)

? ? ? ? self.pos = pygame.math.Vector2(self.rect.center)

? ? ? ? self.move = (pygame.math.Vector2(t) - p).normalize()

? ? def update(self, window_rect):

? ? ? ? self.pos += self.move * 2

? ? ? ? self.rect.center = round(self.pos.x), round(self.pos.y)

? ? ? ? if not window_rect.colliderect(self.rect):

? ? ? ? ? ? self.kill()


pygame.init()

window = pygame.display.set_mode((400, 400))

clock = pygame.time.Clock()


start_pos = (200, 200)

all_sprites = pygame.sprite.Group()


run = True

while run:

? ? clock.tick(100)

? ? for event in pygame.event.get():

? ? ? ? if event.type == pygame.QUIT:

? ? ? ? ? ? run = False

? ? ? ? if event.type == pygame.MOUSEBUTTONDOWN:

? ? ? ? ? ? all_sprites.add(RedObject(start_pos, event.pos))

? ? ? ? ? ? all_sprites.add(GreenObject(start_pos, event.pos))


? ? all_sprites.update(window.get_rect())


? ? window.fill(0)

? ? all_sprites.draw(window)

? ? pygame.draw.circle(window, "white", start_pos, 10)

? ? pygame.draw.line(window, "white", start_pos, pygame.mouse.get_pos())

? ? pygame.display.flip()


pygame.quit()

exit()


查看完整回答
反對 回復 2023-06-06
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號