-
-
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 |
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:
-
First of all, rename your variables so they more clearly indicate what they are doing. What I would suggest:
fishX
becomesfishTopX
fishC
becomesfishBottomX
fishY
becomesfishLeftY
-
Also add a new variable called
fishRightY
(you were usingfishY
as the location for both of the up/down fish) and use this one instead offishY
in the lastif
statement on lines 54-59 -
Now, make four separate variables for direction, renaming the one you have now and adding there new ones:
fishDirection
becomesfishTopDirection
- add
fishBottomDirection
- add
fishLeftDirection
- add
fishRightDirection
-
Modify your
if
statements so that each one is adjusting a different direction variable. It looks like you have fiveif
statements right now, so you can probably delete one. I'd suggest lines 30-36 since you've already commented out theimage()
command in that chunk of code. -
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.
@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