screen.get_at():运行代码pygame

时间:2017-04-27 11:10:39

标签: python pygame

我正在制作世界地图的修订游戏,但我正在努力开始编码。

每个大陆都有不同的颜色(红色,绿色,深蓝色,黄色,橙色,紫色),背景为浅蓝色。

当光标悬停在一个大陆上时,它应该对该大陆的白色版本进行blit以表示用户将鼠标悬停在该大陆上。当光标在“水”上时它应该只有世界地图blitted。

当我运行它时,每个大陆的颜色都会与GIMP中的颜色稍有不同,然后一旦光标移过它就会在运行时发生变化。

这是我的代码:

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")

这些图像是白色的大陆,背景透明。

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()

此外,如果我向每个or color == (new colour rbga)添加elif,当我将鼠标悬停在任何一个大陆上时,非洲也总是会点击其自身的白色版本。

此外,当我在欧洲和亚洲之间移动光标时,它们大部分时间都保持白色。我也会做国家,所以这是一个很大的问题。

1 个答案:

答案 0 :(得分:0)

我建议以不同的方式构建游戏。每帧都显示worldmap,然后获取worldmap的活动像素的颜色,查看颜色是否与其中一个大陆相匹配,然后将该大陆的白色图像加入blit。我还建议将颜色分配给变量,以便更容易区分。

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()