This program renders a procedurally generated landscape in 3D. The perspective is off because I have to lower the fov(field of view) and shrink the size of the screen because repl runs pygame much slower and only allows the screen to be up to 800x 800 not the usual size I use 1200x 750. Also you will have to wait a moment for the program to create a map every time you start the game. I'm also sorry for any lag as pygame just isn't designed for 3D combined with using an online programing website to run it. The program also uses color blending and a basic shader based on the angle of the triangle being colored. Finally, I would recommend making the program window as big as possible for the best viewing experience.
WASD(forward, left, backward, right)
Right Shift(Not left)(left)
There are 22500 polygons(triangles) in each map
There are 67500 3D points the program has to convert to 2D
Some images of the program running on my computer(not running on repl and without a max render distance):
And here's some more images of the world(not ran on repl) using my ray tracing lighting system that traces a ray from each point to the sun and check to see if it collides with anything. This feature is to slow for repl and will not be implemented. In this image I was getting around 0.3 fps:
How to project 3D objects using math and code:
If you want to make a 3D game, first, I wouldn't recommend pygame or repl. The best way to go about it would be to use one of the following, Open GL, Unreal Engine, Unity, and Godot. There are other libraries and game engines that will also allow for 3D games but those are some popular ones that are a great starting point. But if you do want to use pygame or another python3 library(with some minor modifications to the code), some of the code is shown bellow starting with the most complicated snippet of code that calculates the position to draw each 3D point on the screen:
def convert_3D_to_2D(vec3_pos): vec3_dist = [vec3_player_pos - vec3_pos, vec3_player_pos - vec3_pos, vec3_player_pos - vec3_pos] vec2_intercection_point = [0, 0] try: m = vec3_dist / vec3_dist except ZeroDivisionError: m = 0 y = m * player_fov + vec3_pos vec2_intercection_point = y try: m2 = vec3_dist / vec3_dist except ZeroDivisionError: m2 = 0 y2 = m2 * player_fov + vec3_pos inwerds = y2 vec2_intercection_point = inwerds return vec2_intercection_point
Than from there, if you want to get the color and some other data about the object the code to do that it:
new_points =  for triangle in mesh: if int(triangle) - int(vec3_player_pos) in range(-100, render_dist) and int(triangle) - int(vec3_player_pos) in range(-100, render_dist2): vec2_1 = convert_3D_to_2D(triangle) vec2_2 = convert_3D_to_2D(triangle) vec2_3 = convert_3D_to_2D(triangle) # draw the triangle if int(vec2_1) in range(0, width * 3) and int(vec2_1) in range(1, height * 3) and int(vec2_2) in range(0, width * 3) and int(vec2_2) in range(1, height * 3) and int(vec2_3) in range(0, width * 3) and int(vec2_3) in range(1, height * 3): # shading the color based on the angle on the triangle shaded2 = (triangle - triangle) * 2 # the next line is the color of your mesh, change it to be any rgb color you want object_color = (0, 180, 0) color = (clamp(clamp(object_color - shaded2, object_color] - 25, object_color + 25), 0, 255), clamp(clamp(object_color - shaded2, object_color - 25, object_color + 25), 0, 255), clamp(clamp(object_color - shaded2, > object_color - 25, object_color + 25), 0, 255))
And the code to draw the new object using the data obtained from the code above is bellow:
for n in new_points: pygame.draw.polygon(screen, n, [n, n, n])
To make both these functions work, you will need to have the var mesh be a list of list with each of the lists in the mesh variable being a list of three vector3(x, y, z) positions aka the positions of each of the 3 points on a triangle. You can also change the objects color by changing object_color to any rgb(red, green, blue) value you want.
One more function needed to make the functions above work is bellow:
def clamp(value, min_, max_): return min(max(value, min_), max_)
If you need any help getting this code to work than you can ask in the comments and I will try to get back to you and help you make your own(mostly) 3D game. I have not implemented collision detection and do not plan on doing and the same for some other features so I will only be able to help you to a certain level before your going to have to go off on your own. I hope this code will help you and here's a link to a more in depth tutorial I posted:
I hope you enjoy!
@JonDoeBeep Yes, 3D graphics are 2D graphics. You just have to convert the 3D point to a 2D point. There's a link to a tutorial I posted that explains the math and how it works. You basically create two 1D lines and find the intersection point of the players eye at z 30. The number 30 would be the players fov aka field fo view.