Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosen't support this effects and make solid block's out of it. Is there a way to handle that with lighting?

thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sadly, pygame itself does not offer any built-in lighting/glowing effects. However, I have discovered a nice effect, that with tweaking, might even pass as glowing. The idea is that you draw many concentric circles around your ball, each with a diminishing light value. This looks like it is casting a small sphere of light around it. Example:

>>> import pygame
>>> screen = pygame.display.set_mode((400, 300))
>>> pygame.display.set_caption("Glowy ball")
>>> screen.fill([0, 0, 0])
<rect(0, 0, 400, 300)>
>>> circs = [17, 17, 17, 17, 17, 16, 16, 16, 17, 17, 17, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 2, 1, 1, 1]
>>> for i in range(len(circs)):
        pygame.draw.circle(screen, [circs[i]*15]*3, (200, 150), i+1, 1)

The result:

An example of a glowing ball

As this is not proper lighting, it will get drawn on top of walls, or other objects. The simplest way to work around this is to draw the lighting separately, and first, so that everything else will be drawn on top of it. If you are looking for a slightly more professional quality, some ray tracing could go a long way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...