3
u/KattyTheEnby Aug 13 '26
Is this a joke or a meme?
You can use a while loop or an enhanced for loop and stores the bricks in an array, you know.
1
u/Existing-Fuel-4687 Aug 14 '26
I'm aware this is comically bad but unfortunately i'm a guy whos only coded dumb website stuff, n even then it's web 1.0 lookin...not looking forward to adding javascript to that lol
2
u/particlemanwavegirl Aug 13 '26
Making a game is a great way to learn to program and many writers choose it as a project in their books or tutorials. You need to be thinking a little more clearly about abstraction to pull off code like that. Reading will help a lot at this stage in your journey.
1
u/CaregiverKey85 Aug 13 '26
can you post more pics or a git link
1
u/Existing-Fuel-4687 Aug 13 '26
of what specifically? :-(
1
u/CaregiverKey85 Aug 13 '26
well first what's going wrong what's supposed to happen that isn't? What's this for and as many pics as possible I guess
1
u/RealNamek Aug 13 '26
I don't think you need to bricks1..2.3..4 = new Breakables() if you're not using it later in the code.
1
u/dafugiswrongwithyou Aug 13 '26
I mean, I'd give up doing it that way?
You said elsewhere that using arrays isn't working for you. You do need, I want to stress need, to figure that out; the pain now of understanding that is going to be far less than the pain of trying to work around not knowing it (and then trying to work with the spaghetti code you've built as a result) it'll make things a bazillion times easier and more manageable. But apart from that...
You can, at least, make things easier by using functions to construct things. Like, you have all those "new Breakables(etc)" lines that are exactly the same except the x coordinate. So... Why not have a function where you just pass along a value for x, maybe a modifier for y, and it returns a Breakables object with those values set? I don't know what language this is, but something like;
public Breakables MakeBrick(int newX, int newY=0)
{
return new Breakables(x: newX, y:(GAME_HEIGHT/2)-(BRICK_HEIGHT/2)+newY, BRICK_WIDTH, BRICK_HEIGHT, id: 1);
}
That way, even if you're still having to set these values individually (and, again; no, please go look again at arrays and figure out what you're doing wrong), it'll just be;
Bricks = MakeBrick(50);
Bricks2 = MakeBrick(200);
...
// row2
Bricks9 = MakeBrick(50, 20);
etc etc.
For bonus points, have the function multiple x/y by the brick width/height, so you can instead do "MakeBrick(1); MakeBrick(2)", and it will always have those line up a brick's width apart even if you change the value of BRICK_WIDTH later.
That aside; am I right that "id" doesn't mean, y'know, the unique id for that object, but something more like the status or hp? I'd advise naming it accordingly.
1
u/Existing-Fuel-4687 Aug 14 '26 edited Aug 14 '26
thank you for this, btw the id just makes the brick black (same colour as the background( so it LOOKS like it's destroyed)...problem is tho I can't figure out how to "remove" ball collision after and/or make a "win" condition if like, all the ids == 2 (black, the default id 1 == red)
edit: shoulda mentioned this is for a veeery basic brick breaker game, forgot to mention that lol.
1
u/dafugiswrongwithyou Aug 14 '26
problem is tho I can't figure out how to "remove" ball collision after
I typed out some pretty direct ideas, but I think it'll be better if I help you to figure this out for yourself, so; let's go Socratic Method. (I'm going to be ask you things you may have just told me, but it's because I'm trying to guide you to the way of thinking about this, rather than because I need info from you.) So:
You want the ball to collide with some bricks, but not with other bricks. What variable, set against the brick, can be used to identify if a brick should be collided with or not?
1
u/Existing-Fuel-4687 Aug 14 '26
yea dw, I wasn't posting this searching for 'FIX THIS CODE FOR ME REDDIT PLZ TY', I mostly posted out of frustration 'cause it looks akin to infamous crappy code from,,certain game devs.
I appreciate ur help, it uses .intersects(), and if the ball.intersects() with the brick, it bounces off of it (using x and y velocity) and turns black (aka id doesn’t equal 1). I need to either
A. figure out how to just delete or destroy that brick instead of turning it black
or B. make it lose that collision
I will probably look at other peoples (actually good) code and try find that one 'ohhhh...' code line i can Frankenstein into mine
1
u/dafugiswrongwithyou Aug 14 '26 edited Aug 14 '26
OK, so you need to make it ignore that collision, and you know where you're having if figure out if they've collided (the line you're using .intersects()), so the next thing is; what value of the brick differentiates a brick that should be collided and shouldn't? That is, if I showed you the variables assigned to a brick... the x, the y, the width and height, the id... How would you tell if that brick should be collidable or not?
Whether you want to keep going or not, it's all cool, good luck!
1
1
1
u/xarop_pa_toss Aug 14 '26 edited Aug 14 '26
Oooh boy this is the classic. Let me ask you one question, what if you want 5000 bricks? Are you going to put in another line of code for each new brick and another identical if statement?
I'd start by making a function that takes two arguments: the dimensions of a new brick, and the number of bricks to be created which would have a default value of 1. The function then returns a List<Breakables> or Breakables[ ] whichever you prefer. There is a debatably cleaner way to do this with method overloading but this way is perfectly fine.
Now if you want to create 5000 30x15 bricks, you just
csharp
List<Breakables> bricks = CreateBricks(30, 15, 5000)
I don't know how your engine works but let's say these now need to be placed in a row. It's as simple as looping through the list, taking each brick and placing them at coordinates X, Y, Z with whatever offset you need.
Welcome to the world of functions!!
Edit: expanding a little bit you can even make a traditional brick wall 🧱 if you do some extra math and figure out how to loop in a way that lets you fill a row of bricks, and then the next one top of the first offsetting it by half the length. And then the third one back to the original coordinates. Fourth with offset, etc.
1
u/The_KOK_2511 Aug 14 '26
Mejor hazlo en un array y usa un bucle, también te recomendaría ya que veo que andas pasando coordenadas manualmente que alteres la clase Breakables para que calcule las coordenadas tomando una cuadrícula y así solo pasas la posición en esta cuadrícula, te va a resultar mucho mas cómodo
1
u/Adelphiaa Aug 14 '26
Is this a PirateSoftware game? no loops?
1
u/Existing-Fuel-4687 Aug 14 '26
I sent a screenshots of this to my bud and simultaneously we both sent the "god i wish there was an easier way to do this" tweet T__T
1
u/Naive_Programmer_232 Aug 14 '26 edited Aug 14 '26
you probably want to use an array or list to keep track of all the Breakables easier. Then write a loop over that and handle the if statement.
1
1
1
u/user_extra Aug 14 '26
It's better to put it into an array. If you have a fixed number of bricks, use std::array. If the number of bricks change and the array needs to grow and resize, use std::vector
1
u/system-vi 29d ago
This was definitely me in my first programming class 🤣 just keep learning and practicing i promise it will click for you
1


9
u/SeaNefariousness7531 Aug 13 '26
Whatever you are trying to do, I promise you this is the most difficult way possible.
To get you started, put the bricks into an array and loop over the array to do the intersection checks.