python - 视差滚动弄乱了射弹的位置
问题描述
我每 3 秒将 lazers 附加到一个列表中,以便绘制它。
if event.type == pygame.USEREVENT:
timer += 1
print(timer)
if timer%3 == 0:
for enemy in enemies:
lazers.append(Projectile(enemy.x-enemy.width//3, enemy.y-enemy.height//2, scroll))
在 pygame 事件循环之外,我绘制了所有的 lazers:
for lazer in lazers:
lazer.draw_rect(screen)
lazer.x += lazer.velocity
projectile 类很简单,它只在提供的 x,y 坐标处绘制矩形:
class Projectile():
def __init__(self, x, y, scroll, color=(255,0,0)):
self.scroll = scroll
self.x = x-self.scroll[0]
self.y = y-self.scroll[1]
self.color = color
self.velocity = -4
self.width = 20
self.height = 5
def draw_rect(self, screen):
pygame.draw.rect(screen, self.color, pygame.Rect(self.x, self.y, self.width, self.height))
它按照我的编程方式工作。在 x 和 y 处绘制矩形,但在屏幕向左、向右或向上滚动时无法正确绘制。我能想到的唯一解决方案是找到某种方法将屏幕上的激光绘制到敌人的 x 和 y 位置,包括滚动偏移。
解决方案
推荐阅读
- amazon-web-services - 公共子网中的 ALB 是否可以将流量路由到私有子网中的实例?
- apache-nifi - 如何在 Nifi 中为 CSV 设置开始和结束行或间隔行?
- c# - UserControl 绑定仅在构造函数中工作
- bash - 无法使用 Insmod 持久插入内核对象(debian)
- python - 通过 Python 连接 Google Cloud MySQL;如何访问表?
- django - 在 DJANGO 中使用 HttpResponseRedirect 时数据未传递
- tabulator - 有没有办法用 Tabulator 制作一个垂直表格?
- javascript - 将反应函数作为道具传递
- java - 在数据库和文件系统事务中更改路径
- ios - 对于每个视图将内容推开