Advertisement
fewfw

Untitled

May 31st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package gameobject;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8.  
  9. import userinterface.GameWindow;
  10. import util.Resource;
  11.  
  12. public class Clouds {
  13.     private List<ImageCloud> listCloud;
  14.     private BufferedImage cloud;
  15.  
  16.     private MainCharacter mainCharacter;
  17.  
  18.     public Clouds(int width, MainCharacter mainCharacter) {
  19.         this.mainCharacter = mainCharacter;
  20.         cloud = Resource.getResouceImage("data/cloud.png");
  21.         listCloud = new ArrayList<ImageCloud>();
  22.  
  23.         ImageCloud imageCloud = new ImageCloud();
  24.         imageCloud.posX = 0;
  25.         imageCloud.posY = 30;
  26.         listCloud.add(imageCloud);
  27.  
  28.         imageCloud = new ImageCloud();
  29.         imageCloud.posX = 150;
  30.         imageCloud.posY = 40;
  31.         listCloud.add(imageCloud);
  32.  
  33.         imageCloud = new ImageCloud();
  34.         imageCloud.posX = 300;
  35.         imageCloud.posY = 50;
  36.         listCloud.add(imageCloud);
  37.  
  38.         imageCloud = new ImageCloud();
  39.         imageCloud.posX = 450;
  40.         imageCloud.posY = 20;
  41.         listCloud.add(imageCloud);
  42.  
  43.         imageCloud = new ImageCloud();
  44.         imageCloud.posX = 600;
  45.         imageCloud.posY = 60;
  46.         listCloud.add(imageCloud);
  47.     }
  48.  
  49.     public void update(){
  50.         Iterator<ImageCloud> itr = listCloud.iterator();
  51.         ImageCloud firstElement = itr.next();
  52.         firstElement.posX -= mainCharacter.getSpeedX()/8;
  53.         while(itr.hasNext()) {
  54.             ImageCloud element = itr.next();
  55.             element.posX -= mainCharacter.getSpeedX()/8;
  56.         }
  57.         if(firstElement.posX < -cloud.getWidth()) {
  58.             listCloud.remove(firstElement);
  59.             firstElement.posX = GameWindow.SCREEN_WIDTH;
  60.             listCloud.add(firstElement);
  61.         }
  62.     }
  63.  
  64.     public void draw(Graphics g) {
  65.         for(ImageCloud imgLand : listCloud) {
  66.             g.drawImage(cloud, (int) imgLand.posX, imgLand.posY, null);
  67.         }
  68.     }
  69.  
  70.     private class ImageCloud {
  71.         float posX;
  72.         int posY;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement