How many times have you had the following situation?
You have a switch or multiple if else like the following:
if (thisIsTrue)
{
x = 1;
}
else if (thisIsTrueInstead)
{
x = 2;
}
else if (thisIsSometimesTrue)
{
x = 3;
}
else
{
x = 4;
}
And you want to comment out nonadjacent code; like the following x = 2 and x = 4 are commented:
if (thisIsTrue)
{
x = 1;
}
else if (thisIsTrueInstead)
{
//x = 2;
}
else if (thisIsSometimesTrue)
{
x = 3;
}
else
{
//x = 4;
}
Is there a way to do a nonadjacent multi select in the IDE?
I find myself clicking, commenting, clicking, commenting, clicking, commenting a lot. Any help would be much appreciated. Thank you. :)
You can enter multiple comments on different lines at once with the MultiEditing extension.
Related
I'm trying to make a simple maze generator using the simplest algorithm I know of.
This is what my code looks like right now but there is almost never a path from start to finish and I don't know what I'm doing wrong. Here is the algorithm page
function generateMaze() {
let cells = []
for (var i=0;i<nodes.length;i++){
let cell = new Cell(nodes[i].x, nodes[i].y)
cells.push(cell)
//obstacles.push([nodes[i].x, nodes[i].y])
}
for (var i=0;i<cells.length;i++){
if (i % 2 == 0){
if (round(random(1, 2)) == 1){
obstacles.push([cells[i].x, cells[i].y-base])
}
else{
obstacles.push([cells[i].x-base, cells[i].y])
}
}
}
}
class Cell{
constructor(x, y){
this.x = x
this.y = y
this.has_visited = false
this.neigbors = []
}
}
Obstacles is just an empty array to hold the walls and base is how big each block is.
Im trying to add settings to a snake game made in processing. I want to have something like easy, normal and hard or something along the lines of that and change the speed and maybe size of the grid. If anyone coudl explain how to id greatly appreciate it!
ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();
int w = 30, h = 30, bs = 20, dir = 2, applex = 12, appley = 10;
int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};
boolean gameover = false;
void setup() {
size(600,600);
x.add(5);
y.add(5);
}
void draw() {
background(255);
for(int i = 0 ; i < w; i++) line(i*bs, 0, i*bs, height); //Vertical line for grid
for(int i = 0 ; i < h; i++) line(0, i*bs, width, i*bs); //Horizontal line for grid
for(int i = 0 ; i < x.size(); i++) {
fill (0,255,0);
rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
}
if(!gameover) {
fill(255,0,0);
rect(applex*bs, appley*bs, bs, bs);
if(frameCount%5==0) {
x.add(0,x.get(0) + dx[dir]);
y.add(0,y.get(0) + dy[dir]);
if(x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) gameover = true;
for(int i = 1; i < x.size(); i++) if(x.get(0) == x.get(i) && y.get(0) == y.get(i)) gameover = true;
if(x.get(0)==applex && y.get(0)==appley) {
applex = (int)random(0,w);
appley = (int)random(0,h);
}else {
x.remove(x.size()-1);
y.remove(y.size()-1);
}
}
} else {
fill(0);
textSize(30);
text("GAME OVER. Press Space to Play Again", 20, height/2);
if(keyPressed && key == ' ') {
x.clear(); //Clear array list
y.clear(); //Clear array list
x.add(5);
y.add(5);
gameover = false;
}
}
if (keyPressed == true) {
int newdir = key=='s' ? 0 : (key=='w' ? 1 : (key=='d' ? 2 : (key=='a' ? 3 : -1)));
if(newdir != -1 && (x.size() <= 1 || !(x.get(1) ==x.get(0) + dx[newdir] && y.get (1) == y.get(0) + dy[newdir]))) dir = newdir;
}
}
You need to break your problem down into smaller steps:
Step one: Can you store the difficulty in a variable? This might be an int that keeps track of a level, or a boolean that switches between easy and hard. Just hardcode the value of that variable for now.
Step two: Can you write your code so it changes behavior based on the difficulty level? Use the variable you created in step one. You might use an if statement to check the difficulty level, or maybe the speed increases over time. It's completely up to you. Start out with a hard-coded value. Change the value to see different behaviors.
Step three: Can you programatically change that value? Maybe this requires a settings screen where the user chooses the difficulty, or maybe it gets more difficult over time. But you have to do the first two steps before you can start this step.
If you get stuck on a specific step, then post an MCVE and we'll go from there.
int x = 31;
int y = 31;
int x_dir = 4;
int y_dir = 0;
void setup ()
{
size (800, 800);
}
void draw ()
{
background (150);
ellipse (x,y,60, 60);
if (x+30>=width)
{
x_dir =-4;
y_dir = 4;
}
if (y+30>=height)
{
x_dir=4;
y_dir = 0;
}
if (x+30>=width)
{
x_dir = -4;
}
x+=x_dir;
y+=y_dir;
println(x,y);
}
Hi,
I have to create this program in processing which produces an animation of a ball going in a Z pattern (top left to top right, diagonal top right to bottom left, and then straight from bottom left to bottom right) which then goes backwards along the same path it came.
While I have the code written out for the forward direction, I don't know what 2 if or else statements I need to write for the program so that based on one condition it goes forwards, and based on another condition it will go backwards, and it will continue doing so until it terminates.
If I am able to figure out which two if statements I need to write, all I need to do is copy and reverse the x_dir and y_dir signs on the forward loop.
There are a ton of different ways you can do this.
One approach is to keep track of which "mode" you're in. You could do this using an int variable that's 0 when you're on the first part of the path, 1 when you're on the second part of the path, etc. Then just use an if statement to decide what to do, how to move the ball, etc.
Here's an example:
int x = 31;
int y = 31;
int mode = 0;
void setup ()
{
size (800, 800);
}
void draw ()
{
background (150);
ellipse (x, y, 60, 60);
if (mode == 0) {
x = x + 4;
if (x+30>=width) {
mode = 1;
}
} else if (mode == 1) {
x = x - 4;
y = y + 4;
if (y+30>=height) {
mode = 2;
}
} else if (mode == 2) {
x = x + 4;
if (x+30>=width) {
mode = 3;
}
} else if (mode == 3) {
x = x - 4;
y = y - 4;
if (y-30 < 0) {
mode = 2;
}
}
}
Like I said, this is only one way to approach the problem, and there are some obvious improvements you could make. For example, you could store the movement speeds and the conditions that change the mode in an array (or better yet, in objects) and get rid of all of the if statements.
I am doing with following code, where samosarect is one object which coming from right to left with some speed(suppose 10px per loop), tonguerect is my player's tongue which increases up to 200 width and again decreases to 0. Tongue only increases when tongue is true,tongue boolean becomes true when I press shoot button.So my problem is sometimes my player(tonguerect) don't eat(doesnt intersect with samosarect) the samosa(I watch with my eyes that rectangles are intersecting but my code is not getting that),sometimes it(intersecting with proper anim ) eats the samosa without any problem.
if (Texture.samosarect.Contains(Texture.tonguerect) || Texture.tonguerect.Intersects(Texture.samosarect) && tongue)
{
Texture.tonguerect.Y = -50;
System.Diagnostics.Debug.WriteLine("eated samosaaa");
Texture.samosarect.X = -400;
eateds = true;
jumpframe = 17;
tonguereached = true;
caught = true;
if (MainPage.togkey == 1)
eateffect.Play();
if (!catchedd)
{
score += 30; catched++; catchedd = true;
showpoint = true;
}
else { catchedd = false; }
}
For general case, this or this can help you.
Algorithm is described here.
If to talk exactly about xna, take a look here
if (ballRect.Intersects(bat1Rect))
{
ballVelo.X = Math.Abs(ballVelo.X) * -1;
}
if (ballRect.Intersects(bat2Rect))
{
ballVelo.X = Math.Abs(ballVelo.X);
}
I have programmed a small Java application for fun, and it all works well. My problem is that when I was working on my method to parse the command-line parameters, I just felt that there should be a much more efficient way of doing it. My program accepts:
-l I (I is an integer for a new lower bound)
-u I (I is an integer for a new upper bound)
-? (Outputs available command-line options)
-v (Activates Verbose output for the main class)
I implemented my parseArgs method in the following way:
private static void parseArgs(String[] arguments) {
String t = "";
for (int c = 0; c < arguments.length; c++) t += arguments[c];
if (t.contains("-")) {
String[] params = t.split("-");
for (int c = 0; c < params.length; c++) params[c] = params[c].trim();
for (int c = 0; c < params.length; c++) {
if (params[c].startsWith("?") && !docOnly) {
docOnly = true;
printHelp();
}
if (params[c].startsWith("l") && startPoint == 1) {
try {
startPoint = Integer.parseInt(params[c].substring(1));
if (startPoint < 0) {
startPoint = 0;
}
} catch (NumberFormatException e) {
error = true;
System.out.println("\tParameter Error: " + params[c]);
}
}
if (params[c].startsWith("u") && endPoint == 1000) {
try {
endPoint = Integer.parseInt(params[c].substring(1));
if (endPoint < 0) {
endPoint = 1000;
}
} catch (NumberFormatException e) {
error = true;
System.out.println("\tParameter Error: " + params[c]);
}
}
if (params[c].startsWith("v") && !verbose) {
verbose = true;
}
}
} else {
error = true;
System.out.println("\tError in Parameters. Use -? for available options.");
}
}
As I said, my code all works fine and you can take a look at it if you'd like to verify that. I'm just curious how other programmers, professional or not, would tackle the situation of passing a variable number and order of parameters and having an efficient codepath that would parse them accurately. I know mine isn't the best, which is why I'm wondering. Any language will be okay for an answer, I'm just curious on procedure. I can adapt any language necessary to learn a bit more from it.
Thanks in advance.
~ David Collins
String t = "";
for (int c = 0; c < arguments.length; c++) t += arguments[c];
Use a StringBuilder instead. Or even easier, join() provided by any of a number of popular libraries.
if (t.contains("-")) {
String[] params = t.split("-");
Better:
String[] params = t.split("-");
if (params.length > 1) {
While that works for your particular case (arguments are non-negative integers), in general it will be problematic. You should look for - only at the beginning of parameters, so that someone can request a log file named my-log.txt.
startPoint = Integer.parseInt(params[c].substring(1));
if (startPoint < 0) {
startPoint = 0;
}
All - signs got eaten by split, so startPoint < 0 will never be true.
Why do you set an error flag for non-numeric data, silently ignore numbers out of range or repeated arguments?