Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLASS PLAYER:
- package monster.defense;
- import java.awt.*;
- /**
- *
- * @author Morgan
- */
- public class player extends Entity
- {
- private int xdir;
- private int ydir;
- public player(int x, int y) {
- super(x, y);
- w = 16; h=16;
- }
- public void draw(Graphics g)
- {
- move();
- g.setColor(Color.RED);
- g.fillOval(x, y, w, h);
- }
- private void move()
- {
- if (this.getX()>=890&&this.getXDir()==1||this.getX()<=0&&this.getXDir()==-1)
- {
- this.setXDir(0);
- }
- if (this.getY()>=510&&this.getYDir()==1||this.getY()<=0&&this.getYDir()==-1)
- {
- this.setYDir(0);
- }
- x+=xdir;
- y+=ydir;
- }
- public void setXDir(int value)
- {
- xdir = value;
- }
- public void setYDir(int value)
- {
- ydir = value;
- }
- public int getXDir()
- {
- return xdir;
- }
- public int getYDir()
- {
- return ydir;
- }
- }
- CLASS ENTITY:
- package monster.defense;
- import java.awt.*;
- public abstract class Entity {
- protected int x, y, w, h;
- protected boolean removed = false;
- public Entity(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- public void draw(Graphics g)
- {
- }
- public int getX() {return x;}
- public int getY() {return y;}
- public int getW() {return w;}
- public int getH() {return h;}
- }
- CLASS GAME:
- package monster.defense;
- import java.awt.Graphics;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- public class game extends javax.swing.JPanel implements KeyListener{
- private int x = 20;
- private int y = 20;
- private int cnt = 0;
- private player Player;
- private player maximus;
- Graphics g;
- public game() {
- player[] towers = new player[30];
- setFocusable(true);
- addKeyListener(this);
- addMouseListener(new MouseAdapter(){
- public void mousePressed(MouseEvent m)
- {
- int mx=m.getX();
- int my=m.getY();
- towers[cnt]= new player(mx,my);
- g.setColor(Color.ORANGE);
- g.fillOval(x, y, 20,20);
- towers[cnt].draw(g);
- repaint();
- cnt++;
- }
- });
- Player = new player(100,100);
- maximus = new player(490,300);
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- setBackground(new java.awt.Color(200, 22, 0));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 907, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 521, Short.MAX_VALUE)
- );
- }// </editor-fold>
- public void paint(Graphics g)
- {
- //g.setColor(Color.gray);
- //g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.ORANGE);
- g.fillOval(x, y, 20,20);
- Player.draw(g);
- maximus.draw(g);
- this.draw(g);
- repaint();
- }
- public void paintComponent(Graphics g)
- {
- g.setColor(Color.ORANGE);
- g.fillOval(x, y, 20,20);
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- int c = e.getKeyCode();
- if(c==KeyEvent.VK_W)
- {
- y-=1;
- Player.setYDir(-1);
- }
- if(c==KeyEvent.VK_S)
- {
- y+=1;
- Player.setYDir(1);
- }
- if(c==KeyEvent.VK_A)
- {
- x-=1;
- Player.setXDir(-1);
- }
- if(c==KeyEvent.VK_D)
- {
- x+=1;
- Player.setXDir(1);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- Player.setXDir(0);
- Player.setYDir(0);
- }
- // Variables declaration - do not modify
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement