Tak tady je není úplně dořešený ještě se mi blbě pohybuje panák
p.s.: Ano vím je to prasácky napsané:)
Teď je to udělané tak že hráč kliká k okrajům a to vytváří posun..
když jsem to dal při posunu panáka tak se obraz posouval strašně rychle... ale aspoň všechno fungovalo :|
package rpg;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
public class Hrac {
int x,y,z = 3;
int err;
int e2;
Bod konec = new Bod(0,0);
Bod klik = new Bod(0,0);
int naboje = 50;
int zivoty = 5;
int zivotyMax = 5;
int skore = 0;
boolean ukaz = false;
boolean test = true;
static Font text = new Font("Tahoma",Font.BOLD,12);
Image left,right,rightA,leftA,vyb,klikl;
int sx,sy;
int stredX = 12, stredY = 50;
boolean select = false;
boolean move = false;
boolean kliknul = false;
boolean kolize = false;
int j=0;
Start s;
Dum d;
int offsetX,offsetY;
boolean open = false;
public boolean start= true;
public Hrac(){
x = 500-stredX+offsetX;
y = 300-stredY+offsetY;
konec.setX(x+offsetX+stredX) ;
konec.setY(y+offsetY+stredY);
offsetX = 0;
offsetY = 0;
ImageIcon v = new ImageIcon("src/pohyb/oznaceni.png");
vyb = v.getImage();
ImageIcon k = new ImageIcon("src/pohyb/klik.gif");
klikl = k.getImage();
ImageIcon l = new ImageIcon("src/pohyb/left.png");
left = l.getImage();
ImageIcon la = new ImageIcon("src/pohyb/left.gif");
leftA = la.getImage();
ImageIcon r = new ImageIcon("src/pohyb/right.png");
right = r.getImage();
ImageIcon rA = new ImageIcon("src/pohyb/right.gif");
rightA = rA.getImage();
}
public void pohyb(){
int dx = java.lang.Math.abs(konec.getX()-stredX - x);
int dy = java.lang.Math.abs(konec.getY()-stredY - y);
sx = (x < konec.getX()-stredX) ? 1 : -1;
sy = (y < konec.getY()-stredY) ? 1 : -1;
if(x+offsetX == konec.getX()-stredX){
e2 = 0;
err = dx - dy;
}
int i = 0;
while (i != 1 ) {
if(x+24+offsetX >= 400+offsetX && x+offsetX <= 628+offsetX && y+50+offsetY>=0+offsetY && y+50+offsetY<=200+offsetY ){
kolize = true;
if(x+24 >= 400+offsetX )
x = x-2;
if(x <= 628+offsetX && x>=620+offsetX && y+50 >= 0+offsetY )
x = x+3;
if(y+50 >= 0+offsetY )
y = y+3;
if(y+25<=200+offsetY && y+25>=180+offsetY)
y = y-3;
j=0;
break;
}
if(x+offsetX == konec.getX()+offsetX-stredX && y+offsetX == konec.getY()+offsetY-stredY){
break;
}
e2 = 2 * err;
if (e2 > -dy) {
err = err - dy;
x = x + sx;
//offsetX = offsetX - sx ;
}
if (e2 < dx) {
err = err + dx;
y = y + sy;
//offsetY = offsetY - sy;
}
i++;
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getZivoty(){
return zivoty;
}
public void setZivoty(int x){
zivoty = zivoty - x;
}
public void vykresli(Graphics g2d,int x,int y){
if(select == true){
g2d.drawImage(vyb, x-4, y+28, null);
}
if( konec.getX()+offsetX-stredX != x && konec.getY()+offsetY-stredY != y){
g2d.drawImage(klikl,klik.getX()-17, klik.getY()-20, null);
}
if(x == konec.getX()+offsetX)move = false;
if(y == konec.getY()+offsetY)move = false;
if(konec.getX()+offsetX-stredX == x && konec.getY()+offsetY-stredY == y && z == 3)g2d.drawImage(left,x,y,null);
if(konec.getX()+offsetX-stredX == x && konec.getY()+offsetY-stredY == y && z == 4)g2d.drawImage(right,x,y,null);
if(move == false){
if(select == false && z == 4)g2d.drawImage(right,x,y,null);
if(select == false && z == 3)g2d.drawImage(left,x,y,null);
}
if(konec.getX()+offsetX-stredX < x && select == true){
g2d.drawImage(leftA,x,y,null);
z = 3;
}
if(konec.getX()+offsetX-stredX > x && select == true){
g2d.drawImage(rightA,x,y,null);
z = 4;
}
if(konec.getX()+offsetX-stredX < x && select == false && konec.getX()+offsetX != getX()+offsetX && konec.getY()+offsetY != getY()+offsetY){
g2d.drawImage(leftA,x,y,null);
z = 3;
move = true;
}
if(konec.getX()+offsetX-stredX > x && select == false && konec.getX()+offsetX != getX()+offsetX && konec.getY()+offsetY != getY()+offsetY){
g2d.drawImage(rightA,x,y,null);
z = 4;
move = true;
}
if(ukaz == true){
g2d.setFont(text);
g2d.setColor(Color.WHITE);
g2d.drawString(zivoty + " / " + zivotyMax,getX()-10,getY()-10 );
}
if(open == true && x > 400+offsetX && x < 500+offsetX && y > 160+offsetY && y+50 < 240+offsetY){
g2d.setFont(text);
g2d.setColor(Color.RED);
g2d.drawString("VSTOUPIT? Y/N",getX()-10,getY()-10 );
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_A)
test = true;
if(key == KeyEvent.VK_SPACE)
ukaz = true;
}
public void keyRelesed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_A)
select = true;
if(key == KeyEvent.VK_SPACE)
ukaz = false;
}
public void mousePressed(MouseEvent evt) {
int button = evt.getButton();
if(button == MouseEvent.BUTTON1){
if(evt.getX() > 980+offsetX) offsetX = offsetX - 8 ;
if(evt.getX() < 20+offsetX) offsetX = offsetX + 8 ;
if(evt.getY() > 580+offsetY) offsetY = offsetY - 8 ;
if(evt.getY() < 20+offsetY) offsetY = offsetY + 8 ;
if(evt.getX() >= 400+offsetX && evt.getX() <= 628+offsetX && evt.getY() >=0+offsetY && evt.getY()<=200+offsetY){
if(open == false && select == true){
open = true;
}
}else{
if( evt.getY() < y+2*stredY+offsetY && evt.getY() > y+offsetY && evt.getX() < x+2*stredX+offsetX && evt.getX() > x+offsetX){
select = true;
}
if(select == true){
konec.setX(evt.getX());
konec.setY(evt.getY());
}
if( evt.getY() < y+2*stredY+offsetY && evt.getY() > y+offsetY && evt.getX() < x+2*stredX+offsetX && evt.getX() > x+offsetX){
kliknul = false;
}else{
kliknul = true;
}
}
klik.setX(evt.getX());
klik.setY(evt.getY());
}
if(button == MouseEvent.BUTTON3){
if( evt.getY() > 0 && evt.getX() > 0){
if(evt.getX() >= 400+offsetX && evt.getX() <= 628+offsetX && evt.getY()+50>=0+offsetY && evt.getY()<=200+offsetY){
if(select == true)open = false;
}else {
select = false;
}
}
}
}
public void mouseRelesed(MouseEvent evt) {
int button = evt.getButton();
if(button == MouseEvent.BUTTON1){
kliknul = false;
}
}
}