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!

Tomer Cahal
  • 442
  • 1
  • 5
  • 14
  • 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`). – quamrana Nov 15 '19 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. – Prune Nov 15 '19 at 16:41

3 Answers3

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
Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • 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. – jsbueno Nov 15 '19 at 16:43
  • I know I can do this using functions however I was looking for a way to do it without functions. Sort of like macros – Tomer Cahal Nov 15 '19 at 16:47
  • @TomerCahal [You can't do that.](https://stackoverflow.com/a/6442497/7431860) Python doesn't support macros, but some compilers will automatically inline your function for you, if you'd like. – Nick Reed Nov 15 '19 at 16:49
  • @NickReed Got it! Thank you very much for your time. – Tomer Cahal Nov 15 '19 at 16:52
  • You might want to correct the indentation of the `if` blocks. – Matthias Nov 15 '19 at 18:01
  • Thanks, fixed. Must have happened when copying code over. – Nick Reed Nov 15 '19 at 18:02
  • @TomerCahal You're welcome - please consider accepting the answer if it addresses your question. Good luck! – Nick Reed Nov 15 '19 at 23:16
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.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • @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. – Carcigenicate Nov 15 '19 at 16:41
  • 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. – jsbueno Nov 15 '19 at 16:45
  • I know I can do this using functions however I was looking for a way to do it without functions. Sort of like macros. – Tomer Cahal Nov 15 '19 at 16:46
  • @jsbueno That would detract from the point. A whole overview of what function are beyond the scope of something like this. – Carcigenicate Nov 15 '19 at 16:47
  • 2
    @TomerCahal Python doesn't natively support macros. Afaik, functions are as close as you're going to get – Carcigenicate Nov 15 '19 at 16:47
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.)

quamrana
  • 37,849
  • 12
  • 53
  • 71