简体   繁体   English

screen.get_at():运行代码pygame时图像颜色发生变化

[英]screen.get_at(): colour of image changes whilst running code pygame

I'm making a revision game of the world map but am struggling with the beginning of coding this. 我正在制作世界地图的修订游戏,但我正在努力编写这个。

Each continent is a different colour (red, green, dark blue, yellow, orange, purple) and the background is a light blue. 每个大陆都是不同的颜色(红色,绿色,深蓝色,黄色,橙色,紫色),背景是浅蓝色。

When the cursor is hovering over a continent, it should blit the white version of that continent to signify user is hovering over it. 当光标悬停在一个大陆上时,它应该对该大陆的白色版本进行blit以表示用户将鼠标悬停在该大陆上。 When the cursor is on the 'water' it should just have the world map blitted. 当光标在'water'上时,它应该只有世界地图blitted。

When I run this the colour of every continent changes the slightest from what it was in GIMP, then it changes when it is running once the cursor has gone over it once. 当我运行它时,每个大陆的颜色与GIMP中的颜色稍有变化,然后一旦光标移过它就会在它运行时发生变化。

This is my code: 这是我的代码:

import pygame
pygame.init()
import time

running = False

display_width = 800
display_height = 600
screen = pygame.display.set_mode((display_width,display_height))
blue = (69,200,209)
screen.fill(blue)

africaw = pygame.image.load("africaw.png")
asiaw = pygame.image.load("asiaw.png")
australiaw = pygame.image.load("australiaw.png")
europew = pygame.image.load("europew.png")
northamericaw = pygame.image.load("northamericaw.png")
southamericaw = pygame.image.load("southamericaw.png")

These images are the continents in white with transparent backgrounds. 这些图像是白色的大陆,具有透明背景。

worldmap = pygame.image.load("worldmap.png")

screen.blit(worldmap,(0,0))
pygame.display.update()

while not running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = True
        if event.type == pygame.MOUSEMOTION:
            mousemove = pygame.mouse.get_pos()
            color = screen.get_at(mousemove)

            if color == (209,79,70,255) or color == (255,255,255,255):
                screen.blit(africaw,(0,0))
                pygame.display.update()                

            elif color == (130,209,70,255) or color == (255,255,255,255):
                screen.blit(asiaw,(0,0))
                pygame.display.update()

            elif color == (227,134,58,255) or color == (255,255,255,255):
                screen.blit(australiaw,(0,0))
                pygame.display.update()

            elif color == (148,70,209,255) or color == (255,255,255,255):
                screen.blit(europew,(0,0))
                pygame.display.update()

            elif color == (227,216,58,255) or color == (255,255,255,255):
                screen.blit(northamericaw,(0,0))
                pygame.display.update()

            elif color == (58,69,227,255) or color == (255,255,255,255):
                screen.blit(southamericaw,(0,0))
                pygame.display.update()

            elif color != (255,255,255,255):
                screen.blit(worldmap,(0,0))
                pygame.display.update()        


pygame.quit()

Also, if I add or color == (new colour rbga) to each elif , when I hover over any continent, africa always blits the white version of itself as well. 此外,如果我为每个elif添加or color == (new colour rbga) ,当我将鼠标悬停在任何一个大陆上时,非洲也总是会点亮其自身的白色版本。

Additionally, when I move the cursor between europe and asia, they both stay white most of the time. 此外,当我在欧洲和亚洲之间移动光标时,它们大多数时间都保持白色。 I will be doing countries too so this is a huge issue. 我也会做国家,所以这是一个很大的问题。

I recommend to structure the game in a different way. 我建议以不同的方式构建游戏。 Blit the worldmap every frame, then get the color of the active pixel of the worldmap , look if the color matches one of the continents and then blit the white image of this continent. BLIT的worldmap的每一帧,然后得到的有效像素的彩色worldmap ,如果颜色相匹配的大陆之一,然后这个的blit大陆的白色图像的样子。 I also suggest to assign the colors to variables, so that they are easier to distinguish. 我还建议将颜色分配给变量,以便更容易区分。

import sys
import pygame


pygame.init()

display_width = 800
display_height = 600
screen = pygame.display.set_mode((display_width, display_height))

WHITE = pygame.Color('white')

worldmap = pygame.Surface((display_width, display_height))
worldmap.fill((20, 60, 100))
# I draw these rects as a replacement for the colored continents.
pygame.draw.rect(worldmap, (209, 79, 70, 255), (300, 300, 200, 200))
pygame.draw.rect(worldmap, (130, 209, 70, 255), (500, 200, 200, 200))
pygame.draw.rect(worldmap, (227, 134, 58, 255), (500, 400, 200, 200))
pygame.draw.rect(worldmap, (148, 70, 209, 255), (300, 100, 200, 200))
pygame.draw.rect(worldmap, (227, 216, 58, 255), (30, 50, 200, 200))
pygame.draw.rect(worldmap, (58, 69, 227, 255), (30, 300, 200, 200))

africaw = pygame.Surface((200, 200))
africaw.fill(WHITE)
asiaw = pygame.Surface((200, 200))
asiaw.fill(WHITE)
australiaw = pygame.Surface((200, 200))
australiaw.fill(WHITE)
europew = pygame.Surface((200, 200))
europew.fill(WHITE)
northamericaw = pygame.Surface((200, 200))
northamericaw.fill(WHITE)
southamericaw = pygame.Surface((200, 200))
southamericaw.fill(WHITE)

clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    color = worldmap.get_at(pygame.mouse.get_pos())
    screen.blit(worldmap, (0, 0))
    if color == (209, 79, 70, 255):
        screen.blit(africaw, (300, 300))
    elif color == (130, 209, 70, 255):
        screen.blit(asiaw, (500, 200))
    elif color == (227, 134, 58, 255):
        screen.blit(australiaw, (500, 400))
    elif color == (148, 70, 209, 255):
        screen.blit(europew, (300, 100))
    elif color == (227, 216, 58, 255):
        screen.blit(northamericaw, (30, 50))
    elif color == (58, 69, 227, 255):
        screen.blit(southamericaw, (30, 300))

    pygame.display.update()
    clock.tick(30)

pygame.quit()
sys.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM