Python Pygame Lighting for Pong

2024/9/21 5:33:37

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

Answer

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.

https://en.xdnf.cn/q/119824.html

Related Q&A

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…

Join Operation for Dictionary in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to nest a list based on increasing sequences and ignore left overlapping ranges

This is my input mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]from 2 to 11, its increasing, so we need to grab the min max [2, 11] from 7 to 10 its increasing, but we need to ignore it because the …