Pygame rotation - python

I'm implementing scorched earth in python 3.4 using pygame. My problem is that I don't know how to effectively implement barrel so that I can easily rotate it.
I already tried using rect and image but I can't rotate any of them.
Is there a better way or I should just try with image or rect?

You can rotate images using pygame.transform.rotate. I suggest using a cache for the various degrees you need.

Related

HOW i can generate cubes random in pygame?

i need code
How can a randomly generated 3dcube using just pygame be created from the top of the screen and then fall from the top to the bottom of the screen
please help me
using python pygame
As mentioned by #The_spider , pygame is a 2D Game engine. It cannot load 3D files or do any 3D stuff. A few recommended modules would be Panda3d and Ursina(which is a simplified version of Panda3d) install them via pip.
If you really need to use pygame, you'll have to save an image of the cube at every angle and Blit it on the screen using an extremely complicated algorithm. Moreover, it will be extremely slow. Thus, use a python 3D game engine.

Python - Pygame - Rotation not very smooth? [I know why, just can't think of solution]

I'm making a simple game to get my knowledge of Python and Pygame going, but, since I haven't used rotation before, I am encountering a problem. Every time my rectangle rotates, it gets bigger and smaller, and my game needs a centered rotating object.
I have two solutions, maybe three. Will any of these work? If not, do you have a solution of your own?
Move the coods of my rectangle back and forth depending on the angle - This would be hard work.
Is there a way to blit an object by the middle of it, rather than the top left corner? This would be perfect
Use sprites??? I'm not sure if it would help at all, I haven't looked into or learnt anything about sprites at all yet.
It would clarify your question if you post some code. Not having seen your code, I suggest the following: draw the rectangle as usual onto a Surface and then rotate the Surface using pygame.transform.rotate

Rendering image independent of size?

I'm currently working on a small game using pygame.
Right now I render images the standard way, by loading them and then blitting them to my main surface. This is great, if I want to work with an individual image size. Yet, I'd like to take in any NxN image and use it at an MxM resolution. Is there a technique for this that doesn't use surfarray and numeric? Something that already exists in pygame? If not, do you think it would be expensive to compute this?
I'd like to stretch the image. So, upscale or downscale the image. Sorry I wasn't clearer.
There is no single command to do this. You will first have to change the size using pygame.transform.scale, then make a rect of the same size, and set its place, and finally blit. It would probably be wisest to do this in a definition.

Native PyGame method for automatically scaling inputs to a surface resolution?

This question is related to this other one.
In my program (which uses pygame to draw objects on the video) I have two representation of my world:
A physical one that I use to make all the calculations involved in the simulation and in which objects are located on a 1000x1000 metres surface.
A visual one which I use to draw on the screen, in which my objects are located in a window measuring 100x100 pixels.
What I want to achieve is to be able to pass to my pygame drawing functions (which normally accept inputs in pixels) my physical/real-word coordinates. In other words, I would like to be able to say:
Draw a 20m radius circle at coordinates (200m, 500m)
using the precise pygame syntax:
pygame.draw.circle(surface, (255,255,255), (200,500), 20)
and get my circle of 2px radius at centred on pixels (20,50).
Please note that this question is about a native pygame way to do this, not some sort of workaround to achieve that result (if you want to answer that, you should take a look to the question I already mentioned) instead.
Thanks in advance for your time and support.
There is no native pygame way to do this.
You may be misunderstanding the function of pygame. It is not for drawing vector objects. It is for writing pixels into video surfaces.
Since you have vector objects, you must define how they will be converted into pixels. Doing this is not a workaround - it's how you are intended to use pygame.
Since it seems that PyGame developers do not hang around here too much, I brought the question to the Pygame mailing list where it originated a monster thread and the issue has been debated at large.
The summary would be:
At present there is not such a feature.
There is interest to implement it, or at least to try to implement it...
...although is not a priority of the core devs in any way
There is more than one way to skin a cat:
should be the scaling happen both ways (inputting coordinates and reading them)?
how to deal with lines that have no thickness but that should be visible?
how to deal with visibility of objects at the edge of the image? which of their points should be taken as reference to know if a pixel should be lit or not for them?
and more (see linked thread).

draw rotated font with pygame

What's the best way to draw rotated text rendered with fonts with pygame? I can just draw the font then rotozoom it, but it seems better result would be gotten if it were possible to draw the glyphs directly rotated, especially taking AA into account?
Pygame does not have the functionality you are looking for.
You will have to use the method you mention, i.e rotozoom on an the already rendered font. The quality of this method isn't all that bad actually. And concerning speed, you don't want to be rendering the fonts all the time anyway.
If you are desperate for speed, and the text isn't dynamic, you can always make a cache of i.e 32 directions for each text you want displayed. Pyglet will allow you to rotate sprites for "free", i.e hardware accelerated.
Or you can go look at the raw SDL libraries pygame uses and see if you can find something that draws rotated fonts directly and hack pygame to use it.
You can use a pyOpenGL surface in pygame, if that simplifies it for you.
Pyglet also uses OpenGL.
(for pygame surfaces) You can cache the rendered text, using that to rotate. Or pre-cache all 32 directions. [ see other answer ]

Categories