- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试按照“Raiden 2”的风格创建自上而下的拍摄,但在添加滚动 map 时遇到问题。当我添加矩形 map 并合并相机类时,相机不会将船舶放置在 map 的底部,允许船舶飞到 map 的下半部分,或将船舶保持在屏幕中间。
我希望将船放置在 map 的底部,并且相机缓慢向上滚动,使船保持在同一位置。
这是游戏的简化版本,您可以在屏幕上控制一艘船:
import sys, pygame, os, math
# Force static position of screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Constants
LEFT = 'left'
RIGHT = 'right'
BLACK = (0, 0, 0)
GRAY = (70, 70, 70)
WHITE = (255, 255, 255)
WIN_W = 500
WIN_H = 800
SHIP_WIDTH = WIN_W/15
SHIP_HEIGHT = WIN_H/15
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Ship(Entity):
def __init__(self, container):
Entity.__init__(self)
self.speed = 5
self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect.centerx = container.centerx
self.rect.y = container.centery
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.rect.centery -= self.speed
if key[pygame.K_s]:
self.rect.centery += self.speed
if key[pygame.K_d]:
self.rect.centerx += self.speed
if key[pygame.K_a]:
self.rect.centerx -= self.speed
# Ship Movement Boundaries
if self.rect.y < 0:
self.rect.y = 0
if self.rect.y > WIN_H - SHIP_HEIGHT:
self.rect.y = WIN_H - SHIP_HEIGHT
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > WIN_W - SHIP_WIDTH:
self.rect.x = WIN_W - SHIP_WIDTH
def main():
# Initialize Everything
pygame.init()
fps = 60
clock = pygame.time.Clock()
play = True
pygame.display.set_caption('Raiden')
screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.SRCALPHA)
# Create Groups
shipGroup = pygame.sprite.Group()
# Create Game Objects
ship = Ship(pygame.rect.Rect(0, 0, WIN_W, WIN_H))
# Add Game Objects to Groups
shipGroup.add(ship)
# Gameplay
while play:
# Checks if window exit button pressed
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# Update
ship.update()
# Print Background/Sprites
screen.fill(WHITE)
shipGroup.draw(screen)
# Limits frames per iteration of while loop
clock.tick(fps)
# Writes to main surface
pygame.display.flip()
if __name__ == "__main__":
这就是当我添加带有相机类的矩形 map 时发生的情况,请注意该 map 的右侧有 5 个“平台”,但是游戏不允许您前往 map 的下半部分。
import sys, pygame, os, math
# Force static position of screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Constants
LEFT = 'left'
RIGHT = 'right'
BLACK = (0, 0, 0)
GRAY = (70, 70, 70)
WHITE = (255, 255, 255)
WIN_W = 500
WIN_H = 800
SHIP_WIDTH = WIN_W/15
SHIP_HEIGHT = WIN_H/15
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Ship(Entity):
def __init__(self, container):
Entity.__init__(self)
self.speed = 5
self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect.centerx = container.centerx
self.rect.y = container.centery
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.rect.centery -= self.speed
if key[pygame.K_s]:
self.rect.centery += self.speed
if key[pygame.K_d]:
self.rect.centerx += self.speed
if key[pygame.K_a]:
self.rect.centerx -= self.speed
# Ship Movement Boundaries
if self.rect.y < 0:
self.rect.y = 0
if self.rect.y > WIN_H - SHIP_HEIGHT:
self.rect.y = WIN_H - SHIP_HEIGHT
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > WIN_W - SHIP_WIDTH:
self.rect.x = WIN_W - SHIP_WIDTH
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = pygame.Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return pygame.Rect(-l+WIN_W/2, -t+WIN_H/2, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+WIN_W/2, -t+WIN_H/2, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_W), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_H), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return pygame.Rect(l, t, w, h)
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = pygame.Surface((32, 32))
self.image.convert()
self.image.fill(GRAY)
self.rect = pygame.Rect(x, y, 32, 32)
def update(self):
pass
def main():
# Initialize Everything
pygame.init()
fps = 60
clock = pygame.time.Clock()
play = True
pygame.display.set_caption('Raiden')
screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.SRCALPHA)
# Create Groups
shipGroup = pygame.sprite.Group()
backgroundGroup = pygame.sprite.Group()
# Load Level
platforms = []
x = y = 0
level = [
"PPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"PPPP P",
"P P",
"P PPPP",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P PPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPP",]
# build the level
for row in level:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
backgroundGroup.add(p)
x += 32
y += 32
x = 0
total_level_width = len(level[0])*32
total_level_height = len(level)*32
camera = Camera(complex_camera, total_level_width, total_level_height)
# Create Game Objects
ship = Ship(pygame.rect.Rect(0, 0, WIN_W, WIN_H))
# Add Game Objects to Groups
shipGroup.add(ship)
# Gameplay
while play:
# Checks if window exit button pressed
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
camera.update(ship)
# Update
ship.update()
# Print Background/Sprites
screen.fill(WHITE)
for e in backgroundGroup:
screen.blit(e.image, camera.apply(e))
shipGroup.draw(screen)
# Limits frames per iteration of while loop
clock.tick(fps)
# Writes to main surface
pygame.display.flip()
if __name__ == "__main__":
main()
最佳答案
滚动相机的工作原理是计算 Sprite 的新位置。对于背景图 block ,您可以使用 screen.blit(e.image, camera.apply(e))
,这可行,但对于船,您只需调用 shipGroup.draw(screen)
.
但是shipGroup
不知道相机,因此总是将船 Sprite 位在绝对位置,而不是相机类计算的相对位置。
我们可以通过创建一个可以处理相机的 Sprite 组来解决这个问题,例如:
class CameraGroup(pygame.sprite.Group):
def __init__(self, camera):
pygame.sprite.Group.__init__(self)
self.camera = camera
def draw(self, surface):
sprites = self.sprites()
surface_blit = surface.blit
for spr in sprites:
self.spritedict[spr] = surface_blit(spr.image, self.camera.apply(spr))
self.lostsprites = []
并使用它:
...
total_level_width = len(level[0])*32
total_level_height = len(level)*32
# Create Groups
camera = Camera(complex_camera, total_level_width, total_level_height)
shipGroup = CameraGroup(camera)
backgroundGroup = CameraGroup(camera)
# build the level
for row in level:
...
...
在你的主循环中:
while play:
# Checks if window exit button pressed
for event in pygame.event.get():
...
camera.update(ship)
# Update
ship.update()
# Print Background/Sprites
screen.fill(WHITE)
backgroundGroup.draw(screen)
shipGroup.draw(screen)
# Limits frames per iteration of while loop
...
现在滚动可以工作了。另一个问题是您的 Ship
中的此检查类:
if self.rect.y > WIN_H - SHIP_HEIGHT:
self.rect.y = WIN_H - SHIP_HEIGHT
这将防止船舶进一步向下移动 WIN_H
,但你的水平面实际上比屏幕高度还要大。
为了解决这个问题,我们创建了一个 Rect
描述整个关卡,并将其传递给 Ship
类:
total_rect = pygame.rect.Rect(0, 0, total_level_width, total_level_height)
ship = Ship(total_rect)
然后我们在 Ship
中使用它类从级别的底部开始,我们使用 clamp_ip
确保飞船无法离开屏幕:
class Ship(Entity):
def __init__(self, container):
Entity.__init__(self)
self.speed = 5
self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect.centerx = container.centerx
self.rect.y = container.bottom - self.rect.height * 2
self.container = container
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.rect.centery -= self.speed
if key[pygame.K_s]:
self.rect.centery += self.speed
if key[pygame.K_d]:
self.rect.centerx += self.speed
if key[pygame.K_a]:
self.rect.centerx -= self.speed
self.rect.clamp_ip(self.container)
此外,由于您的关卡宽度为 16 block * 32 像素,WIN_W
应该是
WIN_W = 16*32
如果您只想垂直滚动,而不想要水平滚动。
完整代码如下:
import sys, pygame, os, math
# Force static position of screen
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Constants
LEFT = 'left'
RIGHT = 'right'
BLACK = (0, 0, 0)
GRAY = (70, 70, 70)
WHITE = (255, 255, 255)
WIN_W = 16*32
WIN_H = 800
SHIP_WIDTH = WIN_W/15
SHIP_HEIGHT = WIN_H/15
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Ship(Entity):
def __init__(self, container):
Entity.__init__(self)
self.speed = 5
self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
self.rect = self.image.get_rect()
self.rect.centerx = container.centerx
self.rect.y = container.bottom - self.rect.height * 2
self.container = container
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_w]:
self.rect.centery -= self.speed
if key[pygame.K_s]:
self.rect.centery += self.speed
if key[pygame.K_d]:
self.rect.centerx += self.speed
if key[pygame.K_a]:
self.rect.centerx -= self.speed
self.rect.clamp_ip(self.container)
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = pygame.Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return pygame.Rect(-l+WIN_W/2, -t+WIN_H/2, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+WIN_W/2, -t+WIN_H/2, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_W), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_H), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return pygame.Rect(l, t, w, h)
class CameraGroup(pygame.sprite.Group):
def __init__(self, camera):
pygame.sprite.Group.__init__(self)
self.camera = camera
def draw(self, surface):
sprites = self.sprites()
surface_blit = surface.blit
for spr in sprites:
self.spritedict[spr] = surface_blit(spr.image, self.camera.apply(spr))
self.lostsprites = []
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = pygame.Surface((32, 32))
self.image.convert()
self.image.fill(GRAY)
self.rect = pygame.Rect(x, y, 32, 32)
def update(self):
pass
def main():
# Initialize Everything
pygame.init()
fps = 60
clock = pygame.time.Clock()
play = True
pygame.display.set_caption('Raiden')
screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.SRCALPHA)
# Load Level
platforms = []
x = y = 0
level = [
"PPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"PPPP P",
"P P",
"P PPPP",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"P PPPP",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P PPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPP P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPP",]
total_level_width = len(level[0])*32
total_level_height = len(level)*32
total_rect = pygame.rect.Rect(0, 0, total_level_width, total_level_height)
# Create Groups
camera = Camera(complex_camera, total_level_width, total_level_height)
shipGroup = CameraGroup(camera)
backgroundGroup = CameraGroup(camera)
# build the level
for row in level:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
backgroundGroup.add(p)
x += 32
y += 32
x = 0
# Create Game Objects
ship = Ship(total_rect)
# Add Game Objects to Groups
shipGroup.add(ship)
# Gameplay
while play:
# Checks if window exit button pressed
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
camera.update(ship)
# Update
ship.update()
# Print Background/Sprites
screen.fill(WHITE)
backgroundGroup.draw(screen)
shipGroup.draw(screen)
# Limits frames per iteration of while loop
clock.tick(fps)
# Writes to main surface
pygame.display.flip()
if __name__ == "__main__":
main()
关于python - 向顶部/向下射击器添加滚动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516364/
我正在使用面部跟踪进行 HCI(人机交互)。我正在尝试使用面部控制 PC。 我有 3x3 二维网格按钮。1 2 34 5 67 8 9 假设,当前焦点在按钮 5 上。如果我按向上箭头,则焦点将在 2
我正在为蛇和梯子制作一 block 板,到目前为止,我已经按降序打印了板。但是,我需要以正确的方式打印电路板。 编辑“螺旋下降”意味着 100...91 81...90 80...71 ...
我有一个可以响应式调整大小的菜单,因此每次调整大小时 div (#menuWFhover) 的内容都会重新排列,因此 div 根据窗口大小具有不同的高度。 当我使用 jQuery slideDown/
我们从服务器获取数据并附加 ListView 。我们的问题是,当向上/向下滚动时,它首先显示白屏,然后显示数据。向下/向上滚动时,之前出现的白屏应删除 for (var i=0; i");
我想要一个汉堡图标动态改变颜色的代码,以适应网站的黑色部分/部分和白色。它最初是 3 段白色,js 代码几乎是好的和正确的,但是当它在白色部分时它一直闪烁,并在红色和白色之间闪烁。 js var to
我对点击事件的页脚位置有疑问。我正在使用 bootstrap css,我有一个可折叠元素(bootstrap 中的 Accordion ),当它折叠时它有一个名为 .accordion-toggle.
在此代码段中,使用关键帧和动画并显示无/ block ,div 动画以在悬停时向下滑动。 h1 { padding: 20px; } div { width: 100%; background
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我正在尝试就地缩小文件。 我正在用另一个文件的内容替换一个文件的内容,完成后我想确保源文件是否小于目标文件,目标文件是否正确收缩。 (为什么:因为dest文件是一个备份,写入media的开销很大,所以
似乎每当我用一个负整数除以一个正整数时,我都需要它向下舍入 (向 -inf),而不是向 0。但 C# 和 C++ 都向 0 舍入。 所以我想我需要一个 DivideDownward() 方法。我可以用
考虑这个简单的代码: document.addEventListener( 'keypress', function() { console.log( 'press' ); } ); document
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
我见过几个recyclerview的无限滚动示例,但它们都是向下滚动的。 我想要的是一次又一次地加载我的列表,但无论用户是从上面还是从下面滚动。 正常列表 向上滚动 向下滚动 这样可以通过无限滚动一遍
我正在使用带有 View 的 drupal 7,并且我正在使用我发现的向上/向下文本幻灯片的 jquery 脚本。它有效,但是当我尝试将它与带有 ajax 的 View 公开过滤器一起使用时,它似乎不
*场景 我希望制作一个类似于本网站上的 jquery 效果,它位于主 Flash 添加的右侧: http://www.commbank.com.au/ *问题 我已经开始了,但是用很多方法遇到了一些障
互联网。如果这与其他人没有什么关系,请原谅我,但我会将其留在这里,以防这是一个有效的问题。 我正在尝试创建一个文本区域字段,其中用户每次按下键(a-z),都会触发背景颜色更改(在数组中列出)。我一直在
我正在创建一个网络前端来控制一个小型机器人。 Ajax 调用将在 keydown 上进行,以启动机器人,并在 keyup 上进行停止。 我的问题是,当按下某个键时,keyup、keydown 和 ke
我在内容容器中有两个 div,一个向左浮动,另一个向右浮动。我正在使用屏幕的整个宽度。左 div 宽度为 1290px,右 div 宽度为 625px。有时,在加载页面时,滚动条会更改可用屏幕宽度的宽
请看这个UI sketch图片,我在某个站点的侧边栏(黑框)中有这个 div,当我向下滚动或向上滚动时,我不希望它隐藏...我希望它在我向下滚动和移动时自行向下移动当我向上滚动时向上滚动,这样它就永远
我是一名优秀的程序员,十分优秀!