簡體   English   中英

pygame中如何在屏幕外獲得像素顏色?

[英]How to get a pixel color outside of screen in pygame?

我知道要在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行(洋紅色)是碰撞“檢測器”:

在此處輸入圖片說明

這是將滾動(8000 x 8000)的背景:

在此處輸入圖片說明

因此,這是該方案的概念: 在此處輸入圖片說明

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

這是一個獨立的示例,可在較大背景上跟蹤視圖/相機的位置。

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()可以按住箭頭鍵。

事件循環中有一些整理工作,以確保視圖不會移出背景邊界。 然后,它打印遠處像素的顏色。

處理事件后,根據當前位置將背景的一部分繪制到屏幕上。

讓我知道您是否需要任何澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM