简体   繁体   中英

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. When the cursor is on the 'water' it should just have the world map 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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