I’m a beginner when it comes to anything related to programming; I have absolutely no experience whatsoever. With that in mind, I have an assignment which I’ve been desperately trying to figure out how to complete – creating a fully-functional space-shooter game, something akin to Galaga or Space Invaders. Unfortunately, I’m stuck. (What a surprise, right?)
I have made very little progress on this, but my most recent difficulty is attempting to troubleshoot and figure out why the object, which I’ve so aptly designated as the “player” ship, will not move up and down. I’m not entirely sure what I should be looking for – is it an issue with the way the program interprets the keyboard input? Is there something wrong with the coordinates that I simply can’t recognise? Maybe it’s the scope of the variables?
In any case, the object is called with a constructor function named Ship() – the object itself is a triangle. (Have I mentioned I’d rather prefer to not waste time on trying to figure out complex shapes, because I’m kind of an idiot and probably would very well do that? But I digress.) I’ve specified coordinates in six different variables, each corresponding to an x- or a y-value for one of three vertices. On the off-chance that this has any relevance, the canvas size is 400 pixels by 400 pixels.
…In retrospect, that probably doesn’t have much significance.
Just to clarify, the following is all part of the constructors.js file, which is separate from the main code written in sketch.js. I’ve made sure to link it to the HTML file as well, in case anybody was wondering.
/*
Rather than setting the values as static numbers, I chose to
set them dynamically so that the triangle would appear in the
same relative position no matter the size of the canvas:
*/
// coordinates of first vertex – bottom left
this.x1 = width * 9/20;
this.y1 = height * 9/10;
// coords of second vertex – top center
this.x2 = width/2;
this.y2 = height * 4/5;
// coords of third vertex – bottom right
this.x3 = width * 11/20;
this.y3 = height * 9/10;
// the shape is then called via the triangle() function:
this.show = function() {
triangle(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
};
I’ve written an if statement inside of a function-containing variable, move, which is intended to correspond to the keyPressed() function in the main file:
// constructors.js
this.move = function(dir) {
if (key === 'a' || 'd') {
if (this.x1 > 0 && this.x3 < width) {
this.x1 += dir;
this.x2 += dir;
this.x3 += dir;
}
} else if (key === 'w' || 's') {
if (this.y1 < height && this.y2 > 0) {
this.y1 += dir;
this.y2 += dir;
this.y3 += dir;
}
}
};
// sketch.js
function keyPressed() {
// ship was initialised in setup() as an instance of Ship()
let left = -5, right = 5,
up = -2, down = 2;
if (key === 'a') {
ship.move(left);
} else if (key === 'd') {
ship.move(right);
} else if (key === 'w') {
ship.move(up);
} else if (key === 's') {
ship.move(down);
}
If I haven’t yet made this clear, the intention is for the program (software?) to process user input from the keyboard and move the object accordingly.
I’m not entirely sure what’s happening; for some reason when I test the W and S keys, they cause the ship to move two pixels to the left and right, respectively, instead of up and down – the intended result.
The thing is, I can’t use the developer console – the device I’m using is owned by an organisation, and they’ve restricted access to dev tools – but even if I could, I’m not exactly familiar enough with it to make use of it at the moment. However, I don’t know if I’d even receive an error message at all, considering the code (as it is now) will never cause the program to crash. It’s the main reason I’ve decided to reach out for some help.
Anything you could offer, in terms of advice or suggestions, or if you can identify the part(s) of the code I should edit, if any, I would be extremely grateful.
Thank you all so much.
(As a side note, I hope you guys understand that I’m not requesting a full solution; I don’t intend to get one, either. But, on the off-chance that someone does offer some debugged code (and, again, I’m not expecting anyone to go out of their way to do that [especially for me, of all people]), I’d have to ask that you provide a thorough explanation which outlines the differences between the two pieces of code, and which details where and why the error occurs. I would like to make this as much of a learning experience as possible for myself, as I don’t typically appreciate those who take advantage of others’ integrity to pull a quick answer out of thin air.)