如何在pygame中获得屏幕外的像素颜色?

时间:2018-09-20 22:34:13

标签: python python-3.x colors pygame pixel

我知道要在Pygame中获得像素颜色,我使用get_at。 但是我需要得到一个位于屏幕外面的像素,即,我的图像已经被遮盖了,只有一部分可见。

例如,在500,500屏幕中的1000x1000图像将仅具有50%可见,其余的将在屏幕“关闭”。因此,我需要访问屏幕之外的部分。

但是,如果我引用的坐标get_at大于或小于屏幕尺寸,则Pygame会因错误而停止:

  

IndexError:像素索引超出范围

import pygame
W = 1600
H = 600
pygame.init()
screen = pygame.display.set_mode([W, H])
image = pygame.image.load('huge_background.png').convert_alpha()
screen.blit(image, [-5000, -420])
pygame.display.update()
screen.get_at([100, -5]) # generates "IndexError: pixel index out of range"

如何使背景图像像素颜色超出屏幕尺寸?


这是当前可见的pygame屏幕(1600 x 600)。 3行(洋红色)是碰撞“检测器”:

enter image description here

这是要滚动的背景(8000 x 8000):

enter image description here

因此,这是该方案的概念: enter image description here

因此,碰撞检测器行可能超出pygame屏幕的限制,但需要“读取”外部像素以检测碰撞。

1 个答案:

答案 0 :(得分:2)

这是一个独立的示例,可在较大背景上跟踪视图/相机的位置。

import pygame
W = 600
H = 400
pygame.init()
# generate repeated KEYDOWN events when a key is held
pygame.key.set_repeat(500, 50)  
screen = pygame.display.set_mode([W, H])

# image = pygame.image.load('huge_bkgnd.png').convert_alpha()

#create a large bkgnd image
bkgnd = pygame.Surface((W*10, H*10))
# draw a rainbow on the bkgnd surface
colours = ["red", "orange", "yellow", "green", "lightblue1", "blue", "purple"]
bar_width = bkgnd.get_width() // (10 * len(colours))
for x in range(0, bkgnd.get_width() // bar_width):
    rect = [x*bar_width, 0, bar_width, bkgnd.get_height()]
    bkgnd.fill(pygame.Color(colours[x % len(colours)]), rect)

position = [0, 0]
clock = pygame.time.Clock() #for limiting FPS
finished = False
while not finished:
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                finished = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    position[0] -= 100
                if event.key == pygame.K_RIGHT:
                    position[0] += 100
                if event.key == pygame.K_UP:
                    position[1] -= 100
                if event.key == pygame.K_DOWN:
                    position[1] += 100
                # for all key movement clamp position to size of background
                position[0] = max(0, min(position[0], bkgnd.get_width() - W))
                position[1] = max(0, min(position[1], bkgnd.get_height() - H))
                try:
                    x = position[0] + W*2  # just look at what's to the right
                    y = position[1]
                    print(f"At {position}: Heading for ({x},{y}) {bkgnd.get_at((x, y))}")
                except IndexError:
                    print(f"Heading for the edge! {bkgnd.get_width() - position[0]} to go!")
    # copy bkgnd to screen
    view_rect = (*position, W, H)
    screen.blit(bkgnd.subsurface(view_rect), (0,0))
    # update the screen
    pygame.display.update()
    clock.tick(60)  # limit FPS
pygame.quit()

在开始时忽略hacky位,以生成彩虹背景图像,因此水平运动显而易见。

程序将根据箭头键移动位置。使用pygame.key.set_repeat(),可以按住箭头键。

事件循环中有一些整理工作,以确保视图不会移出背景边界。然后打印远处像素的颜色。

处理事件后,根据当前位置将背景的一部分绘制到屏幕上。

如果您需要任何澄清,请告诉我。

相关问题