0

I am creating a 2D dungeon crawler using Pygame, and I want to use the main character as a light source (so you can only see a small amount of the dungeon around you). I have created an alpha mask, but no matter how big I make my alpha mask, it isn't making any difference to the size of the light source and it's too small. Is there a better way to do this in PyGame, or am I overlooking something?

Here is my test code:

import pygame

pygame.init()

screen = pygame.display.set_mode((819, 614))
background = pygame.image.load('Test.png')
light = pygame.image.load('Light_Source.png')
overlay_colour = [73, 92, 115]


while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            break
    else:
        screen.blit(background, (0, 0))
        overlay = pygame.surface.Surface((819, 614))
        overlay.fill(overlay_colour)
        overlay.blit(light, (300, 250))
        screen.blit(overlay, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
        pygame.display.flip()
        continue
    break

Here is my test screen (Test.png):

Test.png

Here is what my alpha mask currently looks like:

Alpha_Mask.png

And here is my alpha mask (Light_Source.png):

Light_Source.png

As you can see, it's pretty lame. But increasing the dimensions of my Light_Source.png file has no effect, and neither does expanding the alpha gradient to fill more of the image. I cannot figure out why this won't work. Am I filling the overlay with the wrong colour? Can anyone see a problem? Or have a better way of doing this please?

Jayce
  • 539
  • 6
  • 21

1 Answers1

1

I don't know why it isn't working for you. I just changed the file to

enter image description here

and the output was

http://imgur.com/qK8D6qR

Comparison of what I changed:

http://imgur.com/F6nv5uw

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
GLaDOS
  • 308
  • 1
  • 10
  • I cannot replicate your solution, I still get the same result as before. Is it possible you changed the file format? Please be more specific with your answer. The first link seems to have exactly the same image as the second one. – Jayce May 28 '17 at 15:34
  • In the first link, there are two images. The upper one is the light source (appears totally black) as most of it is black and it's set to full opacity. Look at the third link. I have changed most of the image to full opacity. – GLaDOS May 28 '17 at 15:53
  • You've uploaded a black image on a black background? No wonder I couldn't see it. Did you change any code? – Jayce May 28 '17 at 15:58
  • Got it. I'm ashamed to admit this, but I was editing 2 versions of the alpha mask, and the one I was changing was in a different directory!!! (What a noob). Thank you for helping me figure this out :) – Jayce May 28 '17 at 16:06
  • Yes Ted, it did answer my question as I have both marked the Answer and commented as such. – Jayce May 28 '17 at 18:08
  • The code was correct since the beginning. It was just that he thought he made a mistake whereas he didn't. I merely pointed it out. BDW @Ted, the first image that you edited isn't the one I intended to add. It's a black image, so you might have missed it. Here, made a white background. http://imgur.com/ylSmjco – GLaDOS May 28 '17 at 18:14
  • @JasonVB Wow, I must have blacked out completely. My mistake. GLaDOS I added the other image in! – Ted Klein Bergman May 28 '17 at 18:26
  • It was a monumental fail - I had 2 versions of the same alpha mask saved in different folders. So no matter what I did to the one I was using, nothing happened. I later found that it was the other one that was loading into the program. – Jayce May 28 '17 at 18:30
  • tip: Blit twice on overlay for better lighting – GLaDOS May 28 '17 at 18:32