11 Replies - 395 Views - Last Post: 19 May 2020 - 03:39 AM Rate Topic: -----

#1 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

wawa game problem

Posted 18 May 2020 - 03:22 AM

hey guys me again i hope i didint to annoying...i have a problem in this game with trail...bassicly problem thaht my trail doesnet move with the enemy but instead just stays in the top left corner...ca anyone please please help me??? tnx for answers guys(i posted my code down there but not all my classes just that ho is relavant with my problem)

trail class code
package com.company;

import java.awt.*;
import java.awt.Graphics2D;

public class trail extends GameObject{

    private float alpha=1;
    private handler handlu;
    private Color cols;
    private int width,height;
    private float life;


    public trail(int x, int y, ID i,Color c,int widther,int heighter,float l,handler h){
        super(x, y, i);
        this.cols=c;
        this.width=widther;
        this.height=heighter;
        this.life=l;
        this.handlu=h;

    }

    private AlphaComposite makeTransparent(float alphater)
    {
        int type=AlphaComposite.SRC_OVER;
        return(AlphaComposite.getInstance(type,alphater));
    }

    @Override
    public void tick() {

        if(alpha>life)
        {
            alpha-=(life-0.01f);
        }
        else
        {
            handlu.removeObject(this);
        }

    }

    @Override
    public void render(Graphics g) {

        Graphics2D g2d=(Graphics2D)g;
        g2d.setComposite(makeTransparent(alpha));
        g.setColor(cols);
        g.fillRect(0,0,width,height);
        g2d.setComposite(makeTransparent(1));



    }

    @Override
    public Rectangle getBounds() {
        return null;
    }
}





basicEnemy class code
package com.company;

import java.awt.*;

public class BasicEnemy extends GameObject {
    private handler hand;
    public BasicEnemy(int x, int y, ID i,handler h) {
        super(x, y, i);
        this.hand=h;

        velX=5;
        velY=5;
    }

    @Override
    public void tick() {
        x+=velX;
        y+=velY;

        if(y<=0 || y>=Game.HEIGHT-32)
            velY*=-1;
        if(x<=0 || x>=Game.WIDTH-16)
            velX*=-1;
        hand.addObject(new trail(x,y,ID.trail,Color.red,16,16,0.01f,hand));



    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(x,y,16,16);

    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x,y,16,16);
    }
}





gameObject class code
package com.company;

import java.awt.*;

public abstract class GameObject {

    protected int x,y;
    protected ID id;
    protected int velX,velY;

    public GameObject(int x,int y,ID i)
    {
        this.x=x;
        this.y=y;
        this.id=i;
    }

    public abstract void tick();
    public abstract void render(Graphics g);
    public abstract Rectangle getBounds();

    public void setX(int x)
    {
        this.x=x;
    }
    public void setY(int y)
    {
        this.y=y;
    }
    public void setID(ID i)
    {
        this.id=i;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }
    public ID getID()
    {
        return id;
    }

    public void setVelx(int x)
    {
        this.velX=x;

    }

    public void setVelY(int y)
    {
        this.velY=y;
    }

    public int getVelX()
    {
        return velX;
    }
    public int getVelY()
    {
        return velY;
    }
}




game class code
package com.company;


import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID=-15506910l;
    public static final int WIDTH=640,HEIGHT=WIDTH/12*9;
    private Thread tr;
    private Random rand;
    private boolean running=false;
    private handler hand;
    private HUD hud;
    private trail t;

    public Game()
    {

        hand=new handler();
        this.addKeyListener(new keyInput(hand));
        new window(WIDTH,HEIGHT,"lets build a game",this);
        hud=new HUD();
        rand=new Random();
        hand.addObject(new player(WIDTH/2-32,HEIGHT/2-32,ID.player,hand));
        hand.addObject(new BasicEnemy(rand.nextInt(WIDTH),rand.nextInt(HEIGHT),ID.BasicEnemy,hand));




    }
    public synchronized void start()
    {
        tr=new Thread(this);
        tr.start();
        running=true;



    }

    public synchronized void stop()
    {

       try
       {
           tr.join();
           running=false;
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }


    }


    @Override
    public void run() {
        this.requestFocus();
        long lastTime=System.nanoTime();
        double amouthOFtick=60;
        double ns=1000000000/amouthOFtick;
        double delta=0;
        long timer=System.currentTimeMillis();
        int frames=0;
        while(running)
        {
            long now=System.nanoTime();
            delta+=(now-lastTime)/ns;
            lastTime=now;
            while(delta>=1)
            {
                tick();
                delta--;
            }
            if(running)
                render();
            frames++;

            if(System.currentTimeMillis()-timer>1000)
            {
                timer+=1000;
                System.out.println("FPS: "+frames);
                frames=0;
            }
        }
        stop();

    }

    private void render()
    {
        BufferStrategy bs=this.getBufferStrategy();
        if(bs==null)
        {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g=bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0,0,WIDTH,HEIGHT);
        hand.render(g);
        hud.render(g);

        g.dispose();
        bs.show();

    }

    private void tick()
    {
        hand.tick();

//
    }

    public static int clamp(int var,int min,int max)
    {
        if(var>=max)
            return var=max;
        else if(var<=min)
            return var=min;
        else
            return var;
    }


    public static void main(String[] args) {
        new Game();

    }



}





handler class code
package com.company;

import java.awt.*;
import java.util.LinkedList;

public class handler {
    LinkedList<GameObject> object=new LinkedList<GameObject>();

    public void tick()
    {
        for(int i=0;i<object.size();i++)
        {
            GameObject temp=object.get(i);
            temp.tick();
        }
    }

    public void render(Graphics g)
    {
        for(int i=0;i<object.size();i++)
        {
            GameObject temp=object.get(i);
            temp.render(g);
        }
    }

    public void addObject(GameObject obj)
    {
        this.object.add(obj);

    }

    public void removeObject(GameObject obj)
    {
        this.object.remove(obj);
    }
}




ID class code
package com.company;

public enum ID {
    player(),
    trail(),
    BasicEnemy();
}




Is This A Good Question/Topic? 0
  • +

Replies To: wawa game problem

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 18 May 2020 - 08:05 AM

How can the code be compiled and executed for testing? There are missing classes.
Can you remove the references to those classes or post them?

One problem I see is the code does not follow java naming conventions making the code harder to read and understand. Class names should start with an uppercase letter.
Was This Post Helpful? 0
  • +
  • -

#3 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

Re: wawa game problem

Posted 18 May 2020 - 09:52 AM

hey i will post all code down there...i change class to to upper case first letter but stiil my trail is on top left corner

class Game
package com.company;


import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID=-15506910l;
    public static final int WIDTH=640,HEIGHT=WIDTH/12*9;
    private Thread tr;
    private Random rand;
    private boolean running=false;
    private Hanadler hand;
    private HUD hud;
    private Trail t;

    public Game()
    {

        hand=new Hanadler();
        this.addKeyListener(new KeyInput(hand));
        new Window(WIDTH,HEIGHT,"lets build a game",this);
        hud=new HUD();
        rand=new Random();
        hand.addObject(new Player(WIDTH/2-32,HEIGHT/2-32,ID.player,hand));
        hand.addObject(new BasicEnemy(rand.nextInt(WIDTH),rand.nextInt(HEIGHT),ID.BasicEnemy,hand));




    }
    public synchronized void start()
    {
        tr=new Thread(this);
        tr.start();
        running=true;



    }

    public synchronized void stop()
    {

       try
       {
           tr.join();
           running=false;
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }


    }


    @Override
    public void run() {
        this.requestFocus();
        long lastTime=System.nanoTime();
        double amouthOFtick=60;
        double ns=1000000000/amouthOFtick;
        double delta=0;
        long timer=System.currentTimeMillis();
        int frames=0;
        while(running)
        {
            long now=System.nanoTime();
            delta+=(now-lastTime)/ns;
            lastTime=now;
            while(delta>=1)
            {
                tick();
                delta--;
            }
            if(running)
                render();
            frames++;

            if(System.currentTimeMillis()-timer>1000)
            {
                timer+=1000;
                System.out.println("FPS: "+frames);
                frames=0;
            }
        }
        stop();

    }

    private void render()
    {
        BufferStrategy bs=this.getBufferStrategy();
        if(bs==null)
        {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g=bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0,0,WIDTH,HEIGHT);
        hand.render(g);
        hud.render(g);

        g.dispose();
        bs.show();

    }

    private void tick()
    {
        hand.tick();

//
    }

    public static int clamp(int var,int min,int max)
    {
        if(var>=max)
            return var=max;
        else if(var<=min)
            return var=min;
        else
            return var;
    }


    public static void main(String[] args) {
        new Game();

    }



}




class basicEnemy
package com.company;

import java.awt.*;

public class BasicEnemy extends GameObject {
    private Hanadler hand;
    public BasicEnemy(int x, int y, ID i,Hanadler h) {
        super(x, y, i);
        this.hand=h;

        velX=5;
        velY=5;
    }

    @Override
    public void tick() {
        x+=velX;
        y+=velY;

        if(y<=0 || y>=Game.HEIGHT-32)
            velY*=-1;
        if(x<=0 || x>=Game.WIDTH-16)
            velX*=-1;
        hand.addObject(new Trail(x,y,ID.trail,Color.red,16,16,0.01f,hand));



    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(x,y,16,16);

    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x,y,16,16);
    }
}




class HUD
package com.company;

import java.awt.*;

public class HUD {
    public static int health=100;

    public void thick()
    {
        health--;
        health=Game.clamp(health,0,100);
    }
    public void render(Graphics g)
    {
        g.setColor(Color.gray);
        g.fillRect(15,15,200,32);

        g.setColor(Color.green);
        g.fillRect(15,15,health*2,32);

        g.setColor(Color.white);
        g.drawRect(15,15,200,32);
    }
}




class ID
package com.company;

public enum ID {
    player(),
    trail(),
    BasicEnemy();
}




class keyinput
package com.company;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class KeyInput extends KeyAdapter {

    private Hanadler hand;

    public KeyInput(Hanadler h)
    {
        this.hand=h;
    }


    @Override
    public void keyPressed(KeyEvent e) {

        int key=e.getKeyCode();
        for(int i=0;i<hand.object.size();i++)
        {
            GameObject temp=hand.object.get(i);

            if(temp.getID()==ID.player)
            {
                if(key==KeyEvent.VK_W)
                {
                    temp.setVelY(-5);
                }
                if(key==KeyEvent.VK_S)
                {
                    temp.setVelY(5);
                }
                if(key==KeyEvent.VK_D)
                {
                    temp.setVelx(5);
                }
                if(key==KeyEvent.VK_A)
                {
                    temp.setVelx(-5);
                }

            }

            if(key==KeyEvent.VK_ESCAPE)
            {
                System.exit(1);
            }



        }


    }

    @Override
    public void keyReleased(KeyEvent e) {

        int key = e.getKeyCode();
        for (int i = 0; i < hand.object.size(); i++) {
            GameObject temp = hand.object.get(i);

            if (temp.getID() == ID.player) {
                if (key == KeyEvent.VK_W) {
                    temp.setVelY(0);
                }
                if (key == KeyEvent.VK_S) {
                    temp.setVelY(0);
                }
                if (key == KeyEvent.VK_D) {
                    temp.setVelx(0);
                }
                if (key == KeyEvent.VK_A) {
                    temp.setVelx(0);
                }

            }




        }
    }
}




class Player
package com.company;

import java.awt.*;
import java.util.Random;
import java.util.logging.Handler;

public class Player extends GameObject {
    Random rand=new Random();
    Hanadler hand;
    public Player(int x, int y, ID i,Hanadler hand) {
        super(x, y, i);
        this.hand=hand;

    }



    @Override
    public void tick() {
        x+=velX;
        y+=velY;

        x=Game.clamp(x,0,Game.WIDTH-37);
        y=Game.clamp(y,0,Game.HEIGHT-60);



        colision();
    }

    private void colision()
    {
        for(int i=0;i<hand.object.size();i++)
        {
            GameObject temp=hand.object.get(i);
            if(temp.getID()==ID.BasicEnemy)
            {
                if(getBounds().intersects(temp.getBounds()))
                {
                    HUD.health-=2;
                }
            }
        }
    }

    @Override
    public void render(Graphics g) {


        g.setColor(Color.white);
        g.fillRect(x,y,32,32);



    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x,y,32,32);
    }


}




class handler
package com.company;

import java.awt.*;
import java.util.LinkedList;

public class Hanadler {
    LinkedList<GameObject> object=new LinkedList<GameObject>();

    public void tick()
    {
        for(int i=0;i<object.size();i++)
        {
            GameObject temp=object.get(i);
            temp.tick();
        }
    }

    public void render(Graphics g)
    {
        for(int i=0;i<object.size();i++)
        {
            GameObject temp=object.get(i);
            temp.render(g);
        }
    }

    public void addObject(GameObject obj)
    {
        this.object.add(obj);

    }

    public void removeObject(GameObject obj)
    {
        this.object.remove(obj);
    }
}




class gameObject
package com.company;

import java.awt.*;

public abstract class GameObject {

    protected int x,y;
    protected ID id;
    protected int velX,velY;

    public GameObject(int x,int y,ID i)
    {
        this.x=x;
        this.y=y;
        this.id=i;
    }

    public abstract void tick();
    public abstract void render(Graphics g);
    public abstract Rectangle getBounds();

    public void setX(int x)
    {
        this.x=x;
    }
    public void setY(int y)
    {
        this.y=y;
    }
    public void setID(ID i)
    {
        this.id=i;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }
    public ID getID()
    {
        return id;
    }

    public void setVelx(int x)
    {
        this.velX=x;

    }

    public void setVelY(int y)
    {
        this.velY=y;
    }

    public int getVelX()
    {
        return velX;
    }
    public int getVelY()
    {
        return velY;
    }
}




class trail
package com.company;

import java.awt.*;
import java.awt.Graphics2D;

public class Trail extends GameObject{

    private float alpha=1;
    private Hanadler handlu;
    private Color cols;
    private int width,height;
    private float life;


    public Trail(int x, int y, ID i,Color c,int widther,int heighter,float l,Hanadler h){
        super(x, y, i);
        this.cols=c;
        this.width=widther;
        this.height=heighter;
        this.life=l;
        this.handlu=h;

    }

    private AlphaComposite makeTransparent(float alphater)
    {
        int type=AlphaComposite.SRC_OVER;
        return(AlphaComposite.getInstance(type,alphater));
    }

    @Override
    public void tick() {

        if(alpha>life)
        {
            alpha-=(life-0.01f);
        }
        else
        {
            handlu.removeObject(this);
        }

    }

    @Override
    public void render(Graphics g) {

        Graphics2D g2d=(Graphics2D)g;
        g2d.setComposite(makeTransparent(alpha));
        g.setColor(cols);
        g.fillRect(0,0,width,height);
        g2d.setComposite(makeTransparent(1));



    }

    @Override
    public Rectangle getBounds() {
        return null;
    }
}




class window
package com.company;

import javax.swing.*;
import java.awt.*;

public class Window extends Canvas {

    private static final long serialVersionUID=-240840605537L;

    public Window(int width,int height,String title,Game g)
    {

        JFrame frame=new JFrame(title);
        frame.setPreferredSize(new Dimension(width,height));
        frame.setMaximumSize(new Dimension(width,height));
        frame.setMinimumSize(new Dimension(width,height));
        frame.setVisible(true);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.add(g);
        g.start();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}



Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 18 May 2020 - 10:20 AM

Ok, it compiles and executes.
I see a white square in the center and a smaller red square bouncing around.
There is a red square in the top left corner and a green strip just below it.
Now what?

This post has been edited by NormR: 18 May 2020 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

Re: wawa game problem

Posted 18 May 2020 - 11:30 AM

that square whitch is on top of the corner this is trail and it must go with the bouncing squre
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 18 May 2020 - 11:33 AM

View Postkretze, on 18 May 2020 - 02:30 PM, said:

that square whitch is on top of the corner this is trail and it must go with the bouncing squre

Where is the code that controls where that square is drawn on the screen? Where is the code that changes the x and y values for that square?

Note: The code has NO comments so it is much harder to find what the code is trying to do and where it is going to do it.
Was This Post Helpful? 0
  • +
  • -

#7 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

Re: wawa game problem

Posted 18 May 2020 - 02:00 PM

im actualy doing that game from youtube, here is the github of that course:https://github.com/AhmedAhres/WAVE so hes got the same code as i am but for me trail is stuck on the top of left corner and for him works perfectly....am doing this because im total noob annd i wonna learn how to create project like this
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 18 May 2020 - 02:06 PM

Quote

i wonna learn how to create project like this

Are you saying that you did not write the code you have posted?

To learn what the code is doing, try adding comments to the parts of the code that you do understand.
The more comments that you can add will help you understand what each part of the code is doing.
Try making some small changes to the code to see what happens. For example
change the colors of the different shapes.
change the speed the squares move.

There are a lot of sections of the code that I have not analyzed yet. I didn't think the code was written by a beginner.
Was This Post Helpful? 0
  • +
  • -

#9 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

Re: wawa game problem

Posted 18 May 2020 - 03:37 PM

yes i change code...i adding second player more enemyes and thaht stuf(colors shapes) i understand what code doing i undestand loops classes functions getter/setter methods constructores...but i stiil dont know wwhy the trail doesent work the right way
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 18 May 2020 - 04:16 PM

Quote

wwhy the trail doesent work the right way

Where is the code that controls where that square is drawn on the screen? Where is the code that changes the x and y values for that square? Look at that code to see why the square does not move.
Was This Post Helpful? 0
  • +
  • -

#11 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 22-October 18

Re: wawa game problem

Posted 19 May 2020 - 02:14 AM

so i have a question...i have a trail class and this trail class is implement in class basicenemy...basicenemy class is implementing in main game class...so why does trail class does not moves equals basicenemy class?? because trail class got the same movement and same x,y position like basicenemy class
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: wawa game problem

Posted 19 May 2020 - 03:39 AM

Quote

why does trail class does not moves

You need to look at the code to see why not. What statement(s) in the Trail class moves its square?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1