HOW i can generate cubes random in pygame? - python

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.

Related

Graphics library to draw images to screen for python

I would like to know if there is a good graphics library for python that will allow me to write a console/terminal application in which I can draw images and or shapes similar to libraries that are available in C++. My applications will be developed for linux primarily but will likely also be made available for windows but it ultimately not necessary.
I already used the following libraries that offer this type of resource:
Tkinter
wxPython (https://www.wxpython.org/) - you can use it to draw shapes or images and there are a lot of built ins resources to create GUI
Pygame (https://www.pygame.org/news) - it's very useful to do physics simulations, you can use built in function to draw circles, blit images into the screen, access keys inputs, etc.
Matplotlib (https://matplotlib.org/) - this is good to mathematical purposes to draw graphs, but you can animate the graphs too
The Pygame help and docs is not user friendly (my think), but the learning curve is fast.

Pygame rotation

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.

Using Pillow/PIL with Pygame

I'm currently trying to use a .png of a Tic Tac Toe board in Pygame so that the white part is transparent. However, I'm getting "libpng warning: sBIT: invalid" and the white is showing when I blit the board onto the background. The code is as follows:
background = pygame.image.load("blah.bmp")
board_surface = pygame.image.load("board.png")
board_surface.convert_alpha()
I read on the Pygame docs that only .bmp is guaranteed to work, and since image.get_extended() is returning a 1, I'm assuming that I simply don't have enough image processing support.
I've installed Pillows and imported the module, but I have no idea how to use this with Pygame. The tutorial shows how to load images as Pillow's Image class and so forth, but I need to represent images as Pygame's Surfaces.
I would appreciate any help with integrating these two things, or any other solutions on how to get my .png to work! For reference, I'm using Python3.3 and Pygame1.9.2
I have encountered this problem too. I solved it by switching to .tif (only with one f !). If you have Gimp installed use that to turn the white spaces into alpha=0 spaces (if you have problems doing that I can help with a more detailed explanation). After that import the image just as normal and use the .convert_alpha() method.

How to optimize tile rendering in pygame?

I am making a tile based game, and the map needs to be rendered every frame. Right now, each tile is 32X32, and the visible map is 28X28 tiles. The performance is dreadful. I recently made it only render the visible tiles, but this still did not improve the FPS much. Right now I'm looking for a way to speed up the rendering. I attribute the slowness to the way I am rendering ; every tile is individually blitted to the screen. What would be a more effective was of doing this?
In pygame (afaik), updating the screen is always one hell of a bottle neck. Since I could not see your code, I don't know, how you are updating the screen. Only blitting the the sprites that changed is a start, but you need to only update those parts that changed, on the screen.
Basically it is the difference between using display.flip() or using update_rects() with only the changed rects. I know, that does not help at all, when you are scrolling the map.
Take a look at this question: Why is this small (155 lines-long) Pacman game on Python running so slow?, it has a similiar topic.
One thing I tried when I had a map compiled of tiles and some sprites on it, I tried always having a precompiled image of the map for an area containing the currently displayed part and some 200 or so pixels around that, so that I could blit the prepared "ground" (still only in updated parts) without the need of blitting all those tiles contained in it. That, of course, is quite some thinking you have to put into that, espacially if you have multiple layers and parts of the map that can be above your active sprites. It is interesting to think and work that through, but I cannot tell you, how much you will gain by that.
One totally different possible solution: I began with pygame once (since I did SDL in C++ prior to that). Recently I was directed to another python gaming library: pyglet. This does not suffer from the problems of updating the whole screen as much as pygame (I think it's because of usage of OpenGL acceleration; it still works on my not at all accelerated eee-Netbook). If you are not bound to pygame in any way, it might be interesting to take a look at pyglet.

Render 3D model orthographically in Python

I want to write a tool in Python that will help me create isometric tiles from 3D-models. You see, I'm not a very proficient artist and free 3D-models are plentisome, and creating something like a table or chair is much easer in 3D than in painting.
This script will load a 3D model in orthographic projection and take pictures from four directions so it can be used in a game. I've tried this in Blender, but the results are inconsistent, very difficult to control and take very long time to create simple sprites.
Rolling my own script will probably let me do neat things too, especially batch-genetration, maybe on texture changes, shading, etc. The game itself will probably be made in Python tpp, so maybe I could generate on the fly. (Edit: and automatically creat cut out see-through walls for when they face camera)
Now my question, what Python libraries can do something like this? I've checked both Pyglet and Panda3D, but I haven't even been able to load a model, let alone set it to orthographic projection.
I found this code:
www.pygame.org/wiki/OBJFileLoader
It let me load and display an .obj file of a cube from Blender with ease. It runs PyOpenGL so it should let me do everything OpenGL can. Never knew OpenGL was so low-level, didn't realize I'd have to write my own loaders and everything.
Anyway, I'm pretty sure I can modify this to project isometrically, rotate the object and grab shots and combine them into sprites. Thanks you guys!
Since you looked at Panda3D - if you can convert your model to the 'egg' format (which blender/maya may do), then you could be able to import it.
https://www.panda3d.org/manual/index.php/Loading_Models
https://www.panda3d.org/manual/index.php/Models_and_Actors
http://www.panda3d.org/manual/index.php/Converting_from_Blender
Note: sources of this was 'python 3d mesh loader' in a popular search engine - this looks viable to me. I now need to try installing it and some code...

Categories