I am working on a basic side scroller platformer in Pygame. I want to add a camera system that tracks the camera and scrolls the world and I want to make the camera systems without using Classes. Thanks in advance.
\$\begingroup\$
\$\endgroup\$
2
New contributor
Eren Bayrak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
-
1\$\begingroup\$ Why do you not want to use classes? Is there a specific source for this constraint? Questions with a yes or no answer like "can I?" aren't a great fit for our format here. To get better answers, try showing how you've tried thinking about / planning / making your camera system so far, and highlight where specifically you've gotten stuck and need assistance. That helps focus answers on helping you with the right things. \$\endgroup\$– DMGregory ♦Commented Apr 28 at 13:47
-
\$\begingroup\$ Actually the reason is I am taking a python course but we didnt get to the classes yet but I still want to have a full fledged out game. And so far I got a camera system that decreases amount which the player moved from the x positions of the rects and images. Thanks for the warning \$\endgroup\$– Eren BayrakCommented 2 days ago
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
1
Yes, you can use a variable that can project to the positions of real world blocks on the screen:
WIDTH = 600
HEIGHT = 600
cam_pos = [0, 0]
smoothness = 0.5 # 0. -> smoother, 1 -> rigid
target_pos = player.rect.center
# in update
# update camera pos
'''dividing width and height by 2 so that it will center the target'''
cam_pos[0] += (target_pos[0] - cam_pos[0] - WIDTH/2) * smoothness
cam_pos[1] += (target_pos[1] - cam_pos[1] - HEIGHT/2) * smoothness
# player projection pos
proj_x = player.rect.x - cam_pos[0]
proj_y = player.rect.y - cam_pos[1]
screen.blit(player.image, (proj_x, proj_y))
Apply this projection code to anything you want, tiles, entities, anything.
New contributor
Zaw Ye Yaint Naing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
-
\$\begingroup\$ thanks for the answer and sorry for not being more explicit about what I wanted \$\endgroup\$ Commented 2 days ago