Is it possible to scale all the items of a pygame program?
I am running the AdvancedDemo from with pygame project:
http://www.pygame.org/project-PaintBrush-1280-.html
But it is too large for my resolution. Is it possible to scale everything that is displayed to fit on the screen, without having to transform all the objects individually?
Thanks
Tom
Not sure how your advance demo works, but you can use
pygame.transform.scale
to scale down the resulting screen. This might be a bit slow though.
Related
I am creating a game in python using Tkinter canvases, and I need to rotate a sprite in the game, I have attempted using PIL's
'image'.rotate()
however the loss of quality is far too high and it wont perform more than about 20 transforms before it starts to lag badly. Can this be fixed or are there any other options aside from making 100s of images by hand?
Here is my code:
shipPIL = shipPIL.rotate(math.degrees(increment), expand=True)
shipTex = ImageTk.PhotoImage(shipPIL)
canvas.itemconfig(self.object, image=shipTex)
increment = pi/20
You really should be saving the original image as a clean copy, and rotating it starting from the original each time. Depending on what you're doing, it may also be a good idea to cache the various rotations somewhere instead of calculating it each time.
EDIT: and TKinter is for GUI applications, not games. I recommend Pygame instead.
I'm making a simple game, whereby I want my characters quite customizable. I want my characters to be able to be fully edited in colours, for example, if a player wants their character to have cyan skin, they just put into the sliders, or what I choose to use, "0,255,255", or purple "255,0,255", or something random "25,125, 156", and have their character be that colour. I haven't even started creating the game, but I've got the basis down and I know exactly what I must do for pretty much every EXCEPT this.
I done a search in Google, and it turns out, I need numerical python for this? Well this is a whole new package, and in order for the average player to play, I must change it to EXE form... (or have python, pygame and numerical python installed onto their PC, which will be a problem if they have a later version...). Now, it's already getting complex with just pygame, but with numerical python as well, is there even a tutorial on how to do this?
Any suggestions? Thanks!
Of course you can use brute force method to do this. Here is function to replace color with another in pygame surface.
def color_replace(surface, find_color, replace_color):
for x in range(surface.get_size()[0]):
for y in range(surface.get_size()[1]):
if surface.get_at([x, y]) == find_color:
surface.set_at([x, y], replace_color)
return surface
I assume by image you mean pygame.Surface. You have several options:
pygame.Surface.set_at(...)
Use a palettized surface. Then change the palette. Based on your use case, this is actually probably what I'd suggest.
Use the pygame.PixelArray PyGame module. I don't think it requires NumPy.
Just use NumPy. It's really not that huge of a requirement; lots of projects require it and it's simple to set up. You get access to the powerful pygame.surfarray module
I have a wx.ScrolledWindow where is drawn on using cairo. I have implemented a zoom-functionality which right now redraws the whole content.
But as there will be up to 200 curves to draw I should consider a more performant solution.
I have thought of these:
Buffering images for the zoom factors -1/+1 (Memory consuming)
Using librsvg and buffer an SVG image (I have read something about this. Does librsvg work under Windows too?)
Storing the cairo.Context after drawing groups of curves, and on zoom restoring it (just an idea.. is that possible?)
Are there other possibilities, and: what is the best solution?
Thanks a lot
Not really a concrete answer to your question, but I was faced with the same problem and just switched to matplotlib where a zoom and pan function is already implemented. I am not sure though if it is super performant. I have the feeling my program was running more smoothly before.
I also tried out floatcanvas and floatcanvas2 but was not really happy with both of them.
If you're double-buffering anyway, why not do a quick bitmap scale as a "preview" while waiting for the newly redrawn vector image? I confess I don't know how to do this. But if you can make it work, it should work! :)
Working on my Major project for software design and development and have run into the hurdle that when using pygame.gfxdraw.aacircle to draw big circles, the output goes screwy as seen here
the window in the picture is showing a section of a circle with a radius of size 1561
if no-one can suggest a fix or alternate way of drawing aa circles i will probably just use the regular circle function as it doesn't look to bad at sch a large radius.
I would suggest not to use pygame.gfxdraw, since it is clearly marked as experimental.
From the documentation:
EXPERIMENTAL!: meaning this api may change, or dissapear in later
pygame releases. If you use this, your
code will break with the next pygame
release.
Just stick with the regular circle function.
EDIT:
Maybe you should open a bugreport on the pygame bugtracker or the pygame mailinglist.
I am currently using rsvg in Python to separate Svggroups.
I got it working so that rsvg loads a single group ... alas all the transparent space around that still remains.
Is there a gtk functionallity to cut away all this space?
Thanks for all the answers!
There's nothing built-in but it's fairly simple to code it (walk over pixels, find the transparent ones). Unfortunately walking over pixels in python is probably slow.