I'm stuck,visual studio 22, I need help making my invaders/enemies move left to right then down
I’m trying to get my invaders/enemies to move left to right then down like in the game space invaders. but I can’t for the life of me figure it out.
Invader class
https://pastebin.com/a14y0aHS
Program class
https://pastebin.com/aLHFM9ZW
Not needed to change but here is the player class
https://pastebin.com/CygjmEYr
If there’s a better way to post my code her please let me know
Pastebin
Invader class - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
Program class - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
Player Class - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
18 Replies
i do not recommend the way you set the locations of your objects along with how you print them
its very complicated, there's actually a better and an easier way
I just updated the pastebin links with the additions I added is it still bit messy!
Like 3ish minutes ago
first, an object's location should not be an int alone
in any 2d game, an object has a horizontal, and a vertical location
we always represent them as x and y variables
you either define an int
X
and an int Y
for your player and invader class
or make your own Struct that has X and Y fields
and this is the recommended way
we usually call the struct Vector2
second, the way where ur trying to print the objects is too complicated
its actually simpler than that
there is a method called Console.SetCursorPosition
basically you use it to specify where in the command prompt window to print a string
your invaders doesnt need to be stored in a 2d array, just a 1d array is enough, or a listHow would you go about making them come down after hitting the right side
I also added the SetCursorPosition works great!
thats simple, iterate through each invader
if one of them is on the right side, increment all the Y of all invaders by 1
seems your Location variable is still an int
which isnt recommended
and ur invaders field is still a 2d array
I see thank you I’ll try to figure that but I have one question how come when I run my code it closes after like 6 seconds is there a way for it to last longer?
did u update ur code?
Not yet but it always closes when the invaders hit the middle of the screen
well update and let me see
Or when I move left or right immediately then it closes faster than that
Fix it on program class line 62 I just put 8 instead of 5 that fixed that issue now to figure out to move down with out destroying everything
I also need figure what I did do to make my player put in the middle I wanted it to be on left side like when I started where the invaders are
Also I updated the links with additions
thats easy just increment y of each invader, or
currentLine
as u call itIf I couldn’t change the code I have now (example the invader class) how can I use the increment without the x and y solution 😦
I would use this!?
Interesting
well, in ur code, Location represents the
x
and CurrentLine
represents the y
I didn’t add the x and y in current line in the invader class that what I mean how to implement the downwards without changing it
I know it’s not the ideal method to go about the invader class
ur not making any sense
y represents the vertical position
we just call it
y
your variable CurrentLine
represents the vertical position
you want the invaders to move down, ud have to change the value of CurrentLineOhhhh I’m sorry i got confused my bad but thank you for clarifying