Skip to content

Instantly share code, notes, and snippets.

@gretaf007
Created October 27, 2020 04:08
Show Gist options
  • Save gretaf007/99913aa4e367616977cc12b4e4f9ffb7 to your computer and use it in GitHub Desktop.
Save gretaf007/99913aa4e367616977cc12b4e4f9ffb7 to your computer and use it in GitHub Desktop.
fishX = 1
fishC = 1
fishY = 1
fishDirection = 1
def setupObstacles():
global photo
photo = loadImage("swordfish2_smally.png")
def drawObstacles():
global fishX
global fishDirection
global fishC
global fishY
#fish bottom
image(photo, fishX, 500)
fishX = fishX + fishDirection
if fishX > 600:
fishDirection = -3
if fishX < 0:
fishDirection = 3
#fish top
# image(photo, fishX + 400, 50)
#fishX = fishX + fishDirection
if fishX > width:
fishDirection = -3
if fishX < 0:
fishDirection = 3
#fish top
image(photo, fishC + 14, 40)
fishC = fishC + fishDirection
if fishC > width:
fishDirection = -3
if fishC < 0:
fishDirection = 3
#fishhorizantal
image(photo, 100, fishY + 100)
fishY = fishY + fishDirection
if fishY > width:
fishDirection = -3
if fishY < 0:
fishDirection = 3
image(photo, 500, fishY + 157)
fishY = fishY + fishDirection
if fishY > width:
fishDirection = -3
if fishY < 0:
fishDirection = 3
@gretaf007
Copy link
Author

@rors
This is my code for my obstacles in my game. The fish are not starting and ending in the same place each time they bounce back and forth. I would like the fish that are moving horizontally to move only between 0 and 750 on the x axis and the ones that move vertically to move between 0 and 750 on the y axis. How can I fix that?

Also, I would like each fish to start in a different place so they will bounce off the walls at different times. Every time I have a fish start in a different place it no longer sees the edge of the game the same as the others and goes beyond the size of the game.

I also am confused about how to make an if statement, so that when the turtle collides with a fish it has to go back to where it started. But maybe that is a question I can ask and have answered in class?

Thank you, Greta

swordfish2_smally

@rors
Copy link

rors commented Nov 2, 2020

Hi Greta,

I think the main issue that you're grappling with at this point is that you one variable (fishDirection) which is controlling the movement of all your sharks. All of your if statements are checking conditions based on the location of the different sharks, but all are ultimately updating that same one fishDirection variable, meaning they will all be moving in an identical way (same timing, same direction value: either up/right together, or down/left together). Here's what I think you should do:

  1. First of all, rename your variables so they more clearly indicate what they are doing. What I would suggest:

    • fishX becomes fishTopX
    • fishC becomes fishBottomX
    • fishY becomes fishLeftY
  2. Also add a new variable called fishRightY (you were using fishY as the location for both of the up/down fish) and use this one instead of fishY in the last if statement on lines 54-59

  3. Now, make four separate variables for direction, renaming the one you have now and adding there new ones:

    • fishDirection becomes fishTopDirection
    • add fishBottomDirection
    • add fishLeftDirection
    • add fishRightDirection
  4. Modify your if statements so that each one is adjusting a different direction variable. It looks like you have five if statements right now, so you can probably delete one. I'd suggest lines 30-36 since you've already commented out the image() command in that chunk of code.

  5. After you have all that in place, you can adjust the numbers in your if statements so that the various "greater than" and "less than" checks are asking questions about the movement patterns of each of the different sharks, and since they are each changing different direction variables, you should be able to tweak the behavior as you described above.

Give that a try and if you get stuck let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment