Loading...
Logo
Processing Forum

Sprites as objects

in Programming Questions  •  3 years ago  

i am trying to make a galga style game but i noticed that it is hard to create sprites with code. is it possible to use a picture in processing and use it as an object( i think thats what it would be)? I am a newbie programmer so anything can help.

Replies(6)

yes you can easily load graphic files using PImage
http://processing.org/reference/PImage.html

or use PShape to use vector files (SVG)
http://processing.org/reference/PShape.html

ok i wrote the code. im trying to make it so that i havve a tank and i want it to move so i have the picture i think but whenever i try to run it i get a white screen. help?
 
final static int NORTH = 1;
final static int EAST = 2;
final static int SOUTH = 4;
final static int WEST = 8;
int result;
float x,y;//x and y for tank


int speed=5;//speed of tank

class Hero{
 
  void display(){
    background(255);
   
  }
}
   
 

void setup(){
  PImage tankup;
  tankup=loadImage("Tank Up.png");
  image(tankup,x,y);
  size(600,600);
  result=0;
  x=0;
  y=0;
}
void draw(){
 
  Hero hero;
  hero= new Hero();
  hero.display();
 
 
   
 
 
  switch(result) {
    case NORTH: y=y-speed; break;
    case EAST: x=x+speed; break;
    case SOUTH: y=y+speed; break;
    case WEST: x=x-speed; break;
  }
 
 
 
}
void keyPressed(){
  switch(key) {
    case('w'):case('W'):result |=NORTH;speed=5;break;
    case('d'):case('D'):result |=EAST;speed=5;break;
    case('s'):case('S'):result |=SOUTH;speed=5;break;
    case('a'):case('A'):result |=WEST;speed=5;break;
  }
}
 
void keyReleased(){ 
  switch(key) {
    case('w'):case('W'):result ^=NORTH;break;
    case('d'):case('D'):result ^=EAST;break;
    case('s'):case('S'):result ^=SOUTH;break;
    case('a'):case('A'):result ^=WEST;break;
  }
}

1) size() must be the first call in setup()
2) tankup variable disappears as soon as setup() ends. Put the PImage declaration outside of setup().
3) Idem, you create a new Hero on each draw(), that's costly and probably now what you want.
4) All Hero's display() does is to clear the sketch area to white, so no wonder that's the only thing you have.
Ok ive fixed some things and the basic system was working. now im trying to make bullets that shoot from the tank and kill the creeps, but i get some assignmentoperator expresion error. heres the code:

Copy code
  1. final static int EAST = 1;
  2. final static int WEST = 2;
  3. int result;
  4. float x,y;//x and y for tank


  5. int speed=5;//speed of tank



  6.     
  7.       
  8.       
  9.     class Creep{
  10.       float creepx;
  11.       float creepy;
  12.       float creepspeed;
  13.       PImage creep;
  14.       
  15.       
  16.       Creep(){
  17.         creep = loadImage("Creep1.png");
  18.         creepx=random(width);
  19.         creepy=-50;
  20.         creepspeed=4;
  21.       }
  22.       void move(){
  23.         creepy=creepy+creepspeed;
  24.         
  25.       }
  26.       
  27.       boolean reachedBottom(){
  28.         if (creepy>height) {
  29.           return true;
  30.         } else {
  31.           return false;
  32.         }
  33.       }
  34.       void display() {
  35.         image(creep,creepx,creepy);
  36.         
  37.       }
  38.       
  39.       void killed(){
  40.         creepspeed=0;
  41.         creepy=1000;
  42.         
  43.       }
  44.     }
  45.     
  46.      class Bullet{
  47.       float bx;
  48.       float by;
  49.       float bspeed;
  50.       float brad;
  51.       int bcolor;
  52.       
  53.       Bullet(){
  54.          bx=x-50;
  55.          by=y;
  56.          bspeed=speed;
  57.          bcolor=color(60,60,0);
  58.          brad=10;
  59.       }
  60.       
  61.       void shot(){
  62.         by=y+1;
  63.         bx=bx;
  64.         fill(bcolor);
  65.         ellipse(bx,by,brad,brad);
  66.       }
  67.       void hit(){
  68.         if(dist(bx,by,creeps[i].creepx,creeps[i].creepy)<44+brad){
  69.       Creep.killed;
  70.         }
  71.       }
  72.     }

  73.   
  74. Creep[]creeps;
  75. int totalCreeps=0;
  76. void setup(){
  77.   size(600,1000);
  78.   creeps=new Creep[6000];
  79.   result=0;
  80.   x=0;
  81.   y=0;
  82. }
  83. void draw(){
  84.   background(255);
  85.   hero.display();
  86.     PImage tankup;
  87.   tankup=loadImage("Tank Up.png");
  88.   image(tankup,x,850);
  89. creeps[totalCreeps]=new Creep();
  90. totalCreeps++;
  91. frameRate(60);
  92.   
  93.   
  94.   if(totalCreeps>=creeps.length){
  95.     totalCreeps=0;
  96.   }
  97.   for(int i=0;i<totalCreeps;i=i+100){
  98.     creeps[i].move();
  99.     creeps[i].display();
  100.   }
  101.     
  102.   
  103.   
  104.   switch(result) {
  105.      case EAST: x=x+speed; break;
  106.     case WEST: x=x-speed; break;
  107.   }
  108.   
  109.   
  110.   
  111. }
  112. void keyPressed(){
  113.   switch(key) {
  114.     case('d'):case('D'):result |=EAST;speed=5;break;
  115.     case('a'):case('A'):result |=WEST;speed=5;break;
  116.   }
  117. }
  118.  
  119. void keyReleased(){  
  120.   switch(key) {
  121.     case('d'):case('D'):result ^=EAST;break;
  122.     case('a'):case('A'):result ^=WEST;break;
  123.   }
  124. }
take a look at the method hit() in the class Bullet. 
There are several issues there, which prevent the code from compiling.

  1. Creep.killed - this isn't a method call, should be killed();
  2. Creep.killed - Creep is the class name, you probably meant creeps[i].killed() 
  3. i isn't defined there - I guess there should be a for loop there that you forgot to add

One more thing - I noticed you're loading an image every time draw() is called  - which is not a good idea. 
You should load it in setup(), and just draw it in draw().
i get some assignmentoperator expresion error
The majority of errors (of beginners) can be corrected without even running the code (not always possible as it needs images, libraries, etc.). So when you have an error, report it as exactly as possible, with the line where it happens;

Beside the good advices of guy, I will add that you must declare tank outside of draw() to use it in two functions. The frameRate is better in setup() too.