Why won't image 1 move to the right? - processing

I'm very new to coding and was wondering how I could make this image move to the right. I added all of my code so that it would be a bit more understandable for what's going on. The moveRight command will not actually move the png to the right which is why I need help fixing and understanding why it won't work. If someone can please help me it would be greatly appreciated.
PImage background, backgroundGameState1, headbasketballbackground, player1, player2;
boolean moveRight, moveLeft;
int canvasSizeX= 1000;
int canvasSizeY = 600;
int mainBackgroundX = 1000;
int mainBackgroundY = 600;
int gameState1 = 1;
int player1X = 100;
int player1Y = 200;
int player2X = 100;
int player2Y = 200;
int backgroundGameState1X= 1000;
int backgroundGameState1Y=600;
int time;
int player1MovmentX = 100;
int player2MovmentX = 700;
void setup() {
//size of canvas
size(1000, 600);
//Loaded images and called them, also made sure to resize them in order to match the canvas size or to make program more asthetically pleasing
background = loadImage("headbasketballbackground.png");
background.resize(mainBackgroundX, mainBackgroundY);
backgroundGameState1 = loadImage("backgroundgamestate1.png");
backgroundGameState1.resize(backgroundGameState1X, backgroundGameState1Y);
player1 = loadImage("steph.png");
player1.resize(player1X, player1Y);
player2 = loadImage("paul.png");
player2.resize(player2X, player2Y);
time=millis();
}
void draw() {
if (gameState1 == 1) {
background(backgroundGameState1);
if (millis() > time + 1000) {
text("Click On Space To Enter The Game!", 100, 100);
textSize(50);
// delay(3000);
}
drawGameState1();
}
if (gameState1 == 2) {
background(background);
image(player1, player1MovmentX, 300);
image(player2, player2MovmentX, 300);
}
// if (gameState2 == 3) {
// text("Congrats you won!");
//}
}
void drawGameState1() {
}
void drawGameState2() {
drawPlayer1Movment();
}
void drawPlayer1Movment(){
if(moveRight){
player1MovmentX += 25;
}
}
void drawGameState3() {
}
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
}
else if(gameState1 == 2){
if(keyCode == RIGHT){
moveRight = true;
}
}
}
void keyReleased(){
if(keyCode == RIGHT){
moveRight = false;
}
}

I don't think you ever called drawPlayer1Movement(). Add it to keyPressed() and player1 should move to the right when you hit the right arrow.
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
} else if (gameState1 == 2) {
if (keyCode == RIGHT) {
moveRight = true;
drawPlayer1Movment();
}
}
}

Related

I'm trying to get pvectors used in the second frame, but nothing pops up anywhere for the pvector (Used circle too)

I can't see where the circle and not sure what's wrong with my code to make it so I can't see the circle with gravity that I coded. Wondering where I went wrong and what I could do to improve it. Can't seem to see what I've done wrong and I put the circle in gameState2 because I'm making the game in that gameState, gameState1 is for the introduction screen. I want to know why the circle won't appear when I use pvectors. If someone can please help me it would be greatly appreciated(this is an assignment for my class). I hope the comments help show the area in which I need help. (Also if someone could look over my code just for confirmation that I won't get bugs while trying to make the gravity.)
PImage background, backgroundGameState1, headbasketballbackground, player1, player2;
boolean moveRight, moveLeft, moveRight2, moveLeft2;
int canvasSizeX= 1000;
int canvasSizeY = 600;
int mainBackgroundX = 1000;
int mainBackgroundY = 600;
int gameState1 = 1;
int player1X = 100;
int player1Y = 200;
int player2X = 100;
int player2Y = 200;
int backgroundGameState1X= 1000;
int backgroundGameState1Y=600;
int time;
int player1MovmentX = 700;
int player2MovmentX = 100;
PVector Position = new PVector(250, 400);
PVector Velocity = new PVector(0, 0);
PVector Acceleration = new PVector(0, 5);
float elasticity = 0.8;
float airResistance = 0;
void setup() {
//size of canvas is 1000 on the X-axis by 600 on the y-axis
size(1000, 600);
//Loaded images and called them, also made sure to resize them in order to match the canvas size or to make program more asthetically pleasing
background = loadImage("headbasketballbackground.png");
background.resize(mainBackgroundX, mainBackgroundY);
backgroundGameState1 = loadImage("backgroundgamestate1.png");
backgroundGameState1.resize(backgroundGameState1X, backgroundGameState1Y);
player1 = loadImage("steph.png");
//resized image for the steph curry png
player1.resize(player1X, player1Y);
player2 = loadImage("kobe.png");
//resized the png
player2.resize(player2X, player2Y);
time=millis();
}
void draw() {
if (gameState1 == 1) {
background(backgroundGameState1);
if (millis() > time + 1000) {
text("Click On Space To Enter The Game!", 100, 100);
textSize(50);
// delay(3000); Used as a test to see how the delay would work and found it to be extremely slow so I increased it to 1000 milliseconds
}
drawGameState1();
}
if (gameState1 == 2) {
background(background);
image(player1, player1MovmentX, 300);
image(player2, player2MovmentX, 300);
}
if (player1MovmentX < 30) {
player1MovmentX = 40;
}
if (player1MovmentX > 930) {
player1MovmentX = 920;
}
if ( player2MovmentX < 30) {
player2MovmentX = 40;
}
if (player2MovmentX > 930) {
player2MovmentX = 920;
}
// if (gameState2 == 3) {
// text("Congrats you won!");
//}
}
void drawGameState1() {
}
void drawGameState2() {
drawBall();
checkIfBallHitEdge();
moveBall();
drawPlayer1Movment();
drawPlayer2Movment();
//drawBoundaries();
}
void drawGameState3() {
}
void drawPlayer1Movment() {
if (moveRight) {
player1MovmentX += 25;
}
if (moveLeft) {
player1MovmentX -= 25;
}
}
void drawPlayer2Movment() {
if (moveRight2) {
player2MovmentX += 25;
}
if (moveLeft) {
player2MovmentX -= 25;
}
}
//pvectors used here
void drawBall() {
ellipse(Position.x, Position.y, 30, 30);
}
void moveBall() {
Velocity = Velocity.add(Acceleration);
Velocity = Velocity.mult((1-airResistance)); // This slows the ball down a little bit each instant
Position = Position.add(Velocity);
}
//pvectors end here
void checkIfBallHitEdge() {
if (Position.x < 0 || Position.x > width) {
Velocity.x *= -1;
}
if (Position.y < 0) {
Velocity.y *= -1;
}
if (Position.y > height) { // Strikes the ground
Velocity.y *= -elasticity;
Position.y = height;
}
}
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
} else if (gameState1 == 2) {
if (keyCode == RIGHT) {
moveRight = true;
player1MovmentX += 25;
}
if (keyCode == LEFT) {
moveLeft = true;
player1MovmentX -= 25;
}
if (keyCode == 68) {
moveRight2 = true;
player2MovmentX += 25;
}
if (keyCode == 65) {
moveLeft2 = true;
player2MovmentX -= 25;
}
if (keyCode == UP) {
Velocity.y = -70;
}
}
}
void keyReleased() {
if (keyCode == RIGHT) {
moveRight = false;
}
if (keyCode == LEFT) {
moveLeft = false;
}
if (keyCode == 68) {
moveRight2 = false;
}
if (keyCode == 65) {
moveLeft2 = false;
}
}
I used the processing IDE for java.

I was wondering if someone could help me re-format my code to fix the loop I made

I was wondering what mistake I made in the line that is formatted like this "''" because I was wondering why my code wouldn't move back to the right to bounce back and forth between walls. I put the code that I need help in with single quotations. This is my frist time on stack, so any tips would be appreciated.
PImage invader, invader2, invader3, invader4, invader5, space, tank;
int tankX = 400;
PImage [] picArray = new PImage [7];
int invaderState = 2;
int timer = 0;
int lap = 0;
int [] alienXPos = {100, 180, 260, 340, 420, 500, 580, 660, 740, 820};
//had to add a few zeros on alienYPos otherwise the loop would bug out and wouldn't work
int[] alienYPos = {40, 140, 240, 340, 0, 0, 0, 0, 0, 0};
boolean moveLeft, moveRight;
int paddleSpeed = 3;
int gameState = 1;
String message1 = "Welcome to space invaders!";
String message2 = "Click space to start!";
boolean movingLeft, movingRight;
void setup() {
size(1000, 1000);
invader = loadImage("invader.jpg");
invader.resize(70, 50);
invader2 = loadImage("invader2.png");
invader2.resize(70, 50);
space = loadImage("space.png");
space.resize(1000, 1000);
tank = loadImage("tank.png");
tank.resize(150, 100);
}
void draw() {
background(space);
timer = millis();
if (timer-lap>800) {
lap = timer;
' **for (int m=0; m <10; m++) {
if (movingRight == false) {
alienXPos[m] += 40;
} else {
alienXPos[m] -= 40;
}
if (alienXPos [m] > 900 && movingRight == false) {
movingRight = true;
for (int l=0; l<10; l++) {
alienYPos[l] += 75;
println("movingleft : " + movingLeft);
println("Moving right : " + movingRight);
}
// if(movingLeft == false){
// alienXPos[m] -= 55;
//}
//else{
// alienXPos [m] += 40;
//}
if (movingLeft == false) {
//alienXPos[m] -=55;
} /*else {
alienXPos[m] ;
}*/
if (alienXPos[m] < 100 && movingLeft == true) {
movingLeft = false;
movingRight = true;
/*for (int l=0; l<10; l++) {
alienYPos[l] += 75;
}** '
println("movingLeft : " + movingLeft);
*/
}
}
}
/* if (alienXPos[m] > 0 && movingLeft == true) {
alienXPos[m] -= 55;
}*/
if (invaderState == 1) {
invaderState = 2;
} else {
invaderState = 1;
}
}
if (tankX > width) {
tankX = 0;
}
if (tankX < 0) {
tankX = 1000;
}
if (gameState == 1) {
drawGameState1();
} else if (gameState == 2) {
drawGameState2();
}
}
void drawGameState1() {
background(#222222);
fill(#000000);
textSize(36);
fill(130, 130, 130);
text(message1, 300, 450);
textSize(20);
text(message2, 430, 600);
}
void drawGameState2() {
background(space);
drawSpaceInvader1();
drawTank();
drawTankMovement();
}
void drawSpaceInvader1() {
for (int i=0; i< 10; i++) {
if (invaderState == 1) {
image(invader, alienXPos[i], alienYPos[0]);
image(invader, alienXPos[i], alienYPos[1]);
image(invader, alienXPos[i], alienYPos[2]);
image(invader, alienXPos[i], alienYPos[3]);
} else if (invaderState == 2) {
image(invader2, alienXPos[i], alienYPos[0]);
image(invader2, alienXPos[i], alienYPos[1]);
image(invader2, alienXPos[i], alienYPos[2]);
image(invader2, alienXPos[i], alienYPos[3]);
}
}
}
void drawTank() {
image(tank, tankX, 700);
}
void drawTankMovement() {
if (moveLeft) {
tankX -= 25;
}
if (moveRight) {
tankX += 25;
}
}
void keyPressed() {
if (gameState == 1) {
if (keyCode == 32) {
gameState = 2;
}
}
if (gameState == 2) {
if (keyCode == LEFT) {
moveLeft = true;
}
if (keyCode == RIGHT) {
moveRight = true;
}
}
}
void keyReleased() {
if (keyCode == LEFT) { // Left Key
moveLeft = false;
}
if (keyCode == RIGHT) { // Right Key
moveRight = false;
}
}
A few things:
Next time, please try and provide a minimal, reproducible example, you were almost there, but you forgot to attach the image files that you use to draw your game out. Running this code gave me errors saying I was missing a few files. I took the liberty of importing some nonsense image and reusing that as your missing assets.
Also missing from your answer is a more implicit title. I think you misunderstood the term "reformatting" code, as that generally refers to the formatting of code. To make your answer likely to be found by others with the same problem, please consider changing the title to what your question is actually about: You can't figure out why your code doesn't reverse the movement of your row of space invaders. Next time, please read this guide on how to write a proper question.
Now to answer your question:
I ran your code and noticed that movingLeft was false whilst movingRight was true. Yet your invaders seem to be running towards the left side of the screen. One of your mistakes was probably getting confused by these variables.
In your code you do
if (movingRight == false) {
alienXPos[m] += 40;
} else {
alienXPos[m] -= 40;
}
Which effectively does the opposite of what you want. In coordinates are oriented like this, assuming we have a 800x400 canvas:
So if you want your invaders to move right, you should be adding to the coordinates when the bool is true, not when it's false. Your logic which flips the invaders direction should be sound:
if (alienXPos[m] < 100 && movingLeft == true) {
movingLeft = false;
movingRight = true;
}
But since in your program due to the above logic error movingLeft is never true, this statement will never run.
One suggestion is that in the simple case of space invaders you don't really need to keep track of two seperate moving variables for each direction, after all, invaders either:
Move to the right
or
Move to the left
Thus you could ditch having two variables and just check, for example, movingRight. If that is true, the invaders should be moving to the right, and if it's false, they should be moving to the left.

Processing: Multiple bullets in top down shooter

I am trying to make a simple top down shooter. When the user presses W, A, S or D a 'bullet' (rectangle) will come out of the 'shooter'. With my code, you can only shoot one bullet per direction until it reaches the end of the screen. Is there a way to make it so they (the user) can shoot multiple bullets in one direction?
Here's my code:
package topdownshooter;
import processing.core.PApplet;
import processing.core.PImage;
public class TopDownShooter extends PApplet {
PImage shooter;
float shooterX = 400;
float shooterY = 300;
float u_bulletSpeed;
float l_bulletSpeed;
float d_bulletSpeed;
float r_bulletSpeed;
boolean shootUp = false;
boolean shootLeft = false;
boolean shootDown = false;
boolean shootRight = false;
public static void main(String[] args) {
PApplet.main("topdownshooter.TopDownShooter");
}
public void setup() {
shooter = loadImage("shooter.png");
}
public void settings() {
size(800, 600);
}
public void keyPressed() {
if(key == 'w') {
shootUp = true;
}
if(key == 'a') {
shootLeft = true;
}
if(key == 's') {
shootDown = true;
}
if(key == 'd') {
shootRight = true;
}
}
public void draw() {
background(206);
imageMode(CENTER);
image(shooter, shooterX, shooterY);
if(shootUp == true) {
rect(shooterX, shooterY-u_bulletSpeed, 5, 5);
u_bulletSpeed += 2;
if(u_bulletSpeed > 300) {
u_bulletSpeed = 0;
shootUp = false;
}
}
if(shootLeft == true) {
rect(shooterX-l_bulletSpeed, shooterY, 5, 5);
l_bulletSpeed += 2;
if(l_bulletSpeed > 400) {
l_bulletSpeed = 0;
shootLeft = false;
}
}
if(shootDown == true) {
rect(shooterX, shooterY+d_bulletSpeed, 5, 5);
d_bulletSpeed += 2;
if(d_bulletSpeed > 300) {
d_bulletSpeed = 0;
shootDown = false;
}
}
if(shootRight == true) {
rect(shooterX+r_bulletSpeed, shooterY, 5, 5);
r_bulletSpeed += 2;
if(r_bulletSpeed > 400) {
r_bulletSpeed = 0;
shootRight = false;
}
}
}
}
The language is processing and I am using the eclipse IDE.
Thanks!
Here's what I would do if I were you. First I'd encapsulate your bullet data into a class, like this:
class Bullet{
float x;
float y;
float xSpeed;
float ySpeed;
// you probably want a constructor here
void drawBullet(){
// bullet drawing code
}
}
Then I'd create an ArrayList that holds Bullet instances:
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
To add a bullet, I'd create a new instance and add it to the ArrayList like this:
bullets.add(new Bullet(bulletX, bulletY));
Then to draw the bullets, I'd iterate over the ArrayList and call the corresponding function:
for(Bullet b : bullets){
b.drawBullet();
}
Shameless self-promotion:
Here is a tutorial on creating classes.
Here is a tutorial on using ArrayLists.

Processing generating a random amount of text

I am trying to generate some random text using processing, what I want is that everytime I press the mouse new text is generated and is displayed on the screen. As of now the text is simply generated than it gets removed due to the looping of draw() any way to fix this?
int click = 0;
void setup() {
String alfabet = "abcdefghijklmnopqrstuvw";
size(1000,1000);
textSize(64);
textAlign(CENTER);
}
void draw() {
background(0);
if(click==1) {
click = 0;
genereren();
}
}
void genereren() {
String alfabet = "abcdefghijklmnopqrstuvw";
int x = 10;
for(int i = 0; i < 15; i = i+1) {
float r = random(24);
if(r < 1) {
r = r+1;
}
int d = int(r);
String EersteLetter = alfabet.substring(d-1,d);
if ( i <= 4) {
text(EersteLetter, 60+(x*3*i), 80);
}
if ( i <= 8) {
text(EersteLetter, 60+(x*3*i), 120);
}
if ( i <= 12) {
text(EersteLetter, 60+(x*3*i), 160);
}
if ( i <= 16) {
text(EersteLetter, 60+(x*3*i), 200);
}
}
}
void mouseClicked() {
click = 1;
}
try this example, if you click the mouse it will display or not, if you hold the mouse button you will freeze the current text.
boolean click = false;
void setup() {
String alfabet = "abcdefghijklmnopqrstuvw";
size(1000, 1000);
textSize(64);
textAlign(CENTER);
background(0);
}
void draw() {
if (click) {
genereren();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
genereren();
}
}
void genereren() {
background(0);
String alfabet = "abcdefghijklmnopqrstuvw";
int x = 10;
for (int i = 0; i < 15; i = i+1) {
float r = random(24);
if (r < 1) {
r = r+1;
}
int d = int(r);
String EersteLetter = alfabet.substring(d-1, d);
if ( i <= 4) {
text(EersteLetter, 60+(x*3*i), 80);
}
if ( i <= 8) {
text(EersteLetter, 60+(x*3*i), 120);
}
if ( i <= 12) {
text(EersteLetter, 60+(x*3*i), 160);
}
if ( i <= 16) {
text(EersteLetter, 60+(x*3*i), 200);
}
}
}
void mouseReleased() {
clear();
}
void mouseClicked() {
click=!click;
}
The easiest way to do this would be just by not using the "background(0);", this way the text would stay for ever. Then you could add a button that runs a method whit the "background(0);" to erase all text.

XNA SpriteSheet not working

I have added logic to show the Sprite animating but it's simply not showing on the screen. What am I doing wrong?
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
namespace MyXNAGame.Game_Classes
{
public class Player : Sprite
{
private int _frameCount = 6;
private int _frameIndex;
private Rectangle[] _frames;
public float _jumpVelocity = 12f;
public PlayerState _playerState;
public Rectangle BoundingBox
{
get { return new Rectangle {X = (int) Position.X, Y = (int) Position.Y, Height = Texture.Height, Width = Texture.Width}; }
}
public void Initialize()
{
_frames = new Rectangle[_frameCount];
int width = Texture.Width / _frameCount;
for (int i = 0; i < _frameCount; i++)
{
_frames[i] = new Rectangle(i*width, 0, width, Texture.Height);
}
}
public void Load(ContentManager contentManager, string assetName)
{
Texture = contentManager.Load<Texture2D>(assetName);
}
public void Update(GameTime gameTime)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gestureSample = TouchPanel.ReadGesture();
if (gestureSample.GestureType == GestureType.Tap)
{
if (_playerState == PlayerState.Running)
{
_playerState = PlayerState.NormalJump;
}
}
if (gestureSample.GestureType == GestureType.Hold)
{
if (_playerState == PlayerState.Running)
{
_playerState = PlayerState.LongJump;
}
}
}
// NormalJump Logic
switch (_playerState)
{
case PlayerState.NormalJump:
Position.Y -= _jumpVelocity;
_jumpVelocity -= 0.5f;
if (_jumpVelocity == 0)
{
_playerState = PlayerState.Falling;
}
break;
case PlayerState.LongJump:
Position.Y -= _jumpVelocity;
_jumpVelocity -= 0.5f;
if (_jumpVelocity == 0)
{
_playerState = PlayerState.Falling;
}
break;
case PlayerState.Falling:
Position.Y += _jumpVelocity;
_jumpVelocity += 0.5f;
break;
case PlayerState.Running:
_frameIndex++;
if (_frameIndex > 5)
{
_frameIndex = 0;
}
break;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, _frames[_frameIndex], Color.White, 0, new Vector2(0, 0), new Vector2(0, 0), SpriteEffects.None, 0);
}
}
}`
Can anyone see the obvious mistake? I am using WP7
I changed the 'Scale' parameter in the Draw() method from new Vector(0,0) to new Vector(1,1) as obviously, having a Scale of 0 will not show anything at all.

Resources