Processing: move an image and shooting
in
Programming Questions
•
5 months ago
Hi, I'm trying to get an image (in the pokemon class) to move left and right with arrow keys, I was wondering if anyone could help me? I also want to make the image shoot at the targets falling(the pokeball class), if anyone can show me how to code it, I would be grateful.
Main class:
- PFont font;
- PImage bg;
- ArrayList pokeball = new ArrayList();
- boolean createBall = true;
- boolean shoot = false;
- int delayBall = 0;
- int life = 3;
- int speed=-2;
- Pokeball poke;
- Pokemon pika;
- void setup()
- {
- size(500,500);
- bg = loadImage("bg.jpg");
- font = loadFont("Aparajita-Bold-48.vlw");
- frameRate (60);
- smooth();
- pika = new Pokemon(450,0);
- }
- void draw()
- {
- background(bg); //background
- pika.pika();
- //text
- text("Lives remaining:"+life, width-150,height-30);
- fill(0);
- if(life<=0){
- textFont(font, 30);
- fill(0);
- text("Gave Over!", width/2-100, height/2);}
- //Pokeball falling
- for(int i=0;i<pokeball.size();i++){
- poke=(Pokeball)pokeball.get(i);
- poke.changeY(speed);
- poke.newBall();
- }
- if (createBall&&pokeball.size()<7){
- pokeball.add(new Pokeball(random(0,480),0));
- createBall = false;
- delayBall = 0;
- }
- delayBall++;
- if (delayBall >=50){
- createBall=true;
- }
- }
- Pokeball class:
- class Pokeball{
- float x;
- float y;
- PImage img;
- Pokeball(float pokeX, float pokeY){
- x = pokeX;
- y = pokeY;
- img = loadImage("Pokeball2.png");
- }
- void newBall(){
- image (img, x,y);
- }
- void changeY(int y) {
- this.y-=y;
- if (this.y>500){
- this.y=0;
- }
- }
- void changeX(int x){
- this.x+=x;
- }
- }
- Pokemon class:
- class Pokemon{
- PImage pika;
- float x;
- float y;
- Pokemon(float pokeX, float pokeY){
- x = pokeX;
- y = pokeY;
- pika = loadImage("pika.gif");
- }
- void pika(){
- image (pika, 250,400);
- }
- }
1