HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Game map transitions edge cases

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
transitionsmapedgegamecases

Problem

I am working on a simple 2-D game right now in Python. I was wondering
if there was a better way than the below to write the game logic to check if the camera is near the edge of the player view, or near the map edge. The logic is getting pretty big, and it's making it much harder to check fringe cases, such as when the character tries to go through one of the corners of the screen rather than just left or right.

I'm not asking for anybody to comb through the code, but to just suggest better ways to structure it so I can handle the increasing if statement complexity.

Most of the logic I want to look at is in Game.py
I have started seperating it out into a camera class. But, it's not complete, and the complex (increasingly) map crossing logic is still there.
In fact, I haven't gotten all of the edge cases correct yet, so baring those, I'd like to focus on structure.

Game Loop - Game.py

```
"""
Author - Thomas Just
Improvements - Seperate constants into a constants file, it's getting kind of large
- Need a way to store the map in a different file, and pre-load it
- Need to improve controls, there's a little bit of funny business sometimes
depending on what order buttons are pushed, the character will stop for a second
- Optimize so only the character is redrawn on the tiles he's currently on, not redraw
the entire screen, too consuming
- start cleaning up code and seperating into classes, including camera, and input code
classes to create: player,time input, camera, and screen. Later on, can add collision
Top priority
- Fix code near left and up boundary, current != 0 isn't allowing sprite to move up or left
- There is some odd behavior near the edges of the screen, including bouncing of sprite
and when sprite is going in two directions at once, the screen occasionally shifts to another screen
"""
import pygame,

Solution

[…] suggest better ways to structure it so I can handle the increasing if statement complexity.

Some suggestions:

  • Replace the whole while running loop content with a generic function or method, such as handle_input or Game.handle_input.



  • Replace long if..else..[…]..elif blocks with chains of maybe_x functions, such as maybe_no_input or maybe_left or ….



  • Extract everything common to the various movement functions into a separate move function.

Context

StackExchange Code Review Q#132802, answer score: 2

Revisions (0)

No revisions yet.