0

My problem: My goal is to create a variable that is a line of code that later on I can just call the variable other than just using that line of code (making the code neater). Comment: ball_x_pos and ball_y_pos change in the code

My current code:

CASE_1 = ball_y_pos + RADIUS >= WINDOW_HEIGHT  # Hitting bottom floor(need to increase Y)
CASE_2 = ball_y_pos - RADIUS <= 0  # Hitting top floor(need to decrease Y)
CASE_3 = ball_x_pos + RADIUS >= WINDOW_WIDTH  # Hitting right side(need to decrease X)
CASE_4 = ball_x_pos - RADIUS <= 0  # Hitting left side(need to decrease X)


if CASE_1:  # Ball it hitting the bottom floor
        Y_CHANGER = 1
    if CASE_2:  # Ball is hitting the top floor
        Y_CHANGER = -1
    if CASE_3:  # Ball is hitting the right side
        X_CHANGER = -1
    if CASE_4:
        X_CHANGER = 1

What I think is happening: I'm pretty sure that right now the code is defining the values of the cases as False on assignment. I was wondering if there is any way I can still do this. Thanks in advance!

2
  • Can you update this question with examples of input data (I assume ball_y_pos and ball_x_pos) and the expected output data (I assume Y_CHANGER and X_CHANGER). Commented Nov 15, 2019 at 16:38
  • 2
    Your data and control flow are unclear. "A line of code to execute later" is usually a sign that you need a function, temporary variable, or a change in program design. Since you haven't yet specified what you need, we're left to guess. Commented Nov 15, 2019 at 16:41

3 Answers 3

2

I'm not sure if I misunderstood the question or not, but it seems you're looking to create a function, whose output changes based on inputs. Maybe something like this would help?

Based on your comment below, you're looking to inline your functions to emulate macro-esque behavior. You can't do this, but some compilers like PyPy will automatically optimize your code, so I wouldn't worry too much about it. Below are examples of functions that oculd do the trick for you:

def CASE_1():
  return ball_y_pos + RADIUS >= WINDOW_HEIGHT  # Hitting bottom floor(need to increase Y)
def CASE_2():
  return ball_y_pos - RADIUS <= 0  # Hitting top floor(need to decrease Y)
def CASE_3():
  return ball_x_pos + RADIUS >= WINDOW_WIDTH  # Hitting right side(need to decrease X)
def CASE_4():
  return ball_x_pos - RADIUS <= 0  # Hitting left side(need to decrease X)


if CASE_1():  # Ball it hitting the bottom floor
    Y_CHANGER = 1
if CASE_2():  # Ball is hitting the top floor
    Y_CHANGER = -1
if CASE_3():  # Ball is hitting the right side
    X_CHANGER = -1
if CASE_4():
    X_CHANGER = 1

This defines four functions, each of which , when called, evaluates its statement and returns True or False based on its result. Note that this implies global variables (which are poor practice) - since you also mentioned ball_x_pos and ball_y_pos change in the code, you likely want to pass the variables in. Something like this would be better practice:

def CASE_1(y_pos):
  return y_pos + RADIUS >= WINDOW_HEIGHT  # Hitting bottom floor(need to increase Y)
def CASE_2(y_pos):
  return y_pos - RADIUS <= 0  # Hitting top floor(need to decrease Y)
def CASE_3(x_pos):
  return x_pos + RADIUS >= WINDOW_WIDTH  # Hitting right side(need to decrease X)
def CASE_4(x_pos):
  return x_pos - RADIUS <= 0  # Hitting left side(need to decrease X)


if CASE_1(ball_y_pos):  # Ball it hitting the bottom floor
    Y_CHANGER = 1
if CASE_2(ball_y_pos):  # Ball is hitting the top floor
    Y_CHANGER = -1
if CASE_3(ball_x_pos):  # Ball is hitting the right side
    X_CHANGER = -1
if CASE_4(ball_x_pos):
    X_CHANGER = 1
Sign up to request clarification or add additional context in comments.

7 Comments

This would be even better if you'd mention that ideally, the functions should be named to descriptively reflect what they are testing, so one does not have to comment each if line to describe what is being tested.
I know I can do this using functions however I was looking for a way to do it without functions. Sort of like macros
@TomerCahal You can't do that. Python doesn't support macros, but some compilers will automatically inline your function for you, if you'd like.
@NickReed Got it! Thank you very much for your time.
You might want to correct the indentation of the if blocks.
|
0

. . . a variable that is a line of code that later on I can just call the variable other than just using that line of code . . .

This is essentially describing a function.

To write one, you could have, for example:

def is_hitting_bottom():
    return ball_y_pos + RADIUS >= WINDOW_HEIGHT

if is_hitting_bottom():
    Y_CHANGER = 1

The benefits of using functions here are minimal though. Really, this is just giving the code a nicer name.

5 Comments

@mcsoini Yes. You're going to have to elaborate on what you're mentioning that for though. Calling the function will account for changing values.
You degrade your answer value by saying the "benefits of using functions are minimal". Instead of that, you could show the other advantages that using functions could bring to this pattern.
I know I can do this using functions however I was looking for a way to do it without functions. Sort of like macros.
@jsbueno That would detract from the point. A whole overview of what function are beyond the scope of something like this.
@TomerCahal Python doesn't natively support macros. Afaik, functions are as close as you're going to get
0

You probably want code like the following, because if you have a ball with an x and a y position, you are likely to have more of them later on:

def get_changes(px, py):
    dx, dy = 0, 0
    if py + RADIUS >= WINDOW_HEIGHT:
        dy = 1
    if py - RADIUS <= 0:
        dy = -1
    if px + RADIUS >= WINDOW_WIDTH:
        dx = 1
    if px - RADIUS <= 0:
        dx = -1
    return dx, dy

def update_position(px, py):
    dx, dy = get_changes(px, py)
    return px+dx, py+dy

ball_x_pos, ball_y_pos = update_position(ball_x_pos, ball_y_pos)

(btw I think you have the changes the wrong way round, eg if the Y pos is so large that it is in danger of going over the window height, which is actually the floor, then I think that Y should be reduced.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.