I'm programing a game with mario in Java. When I run the code, it does not give an error, but the character does not move. I checked every bit but I could not find the error. I left the codes down. What should I do?
I'm programing a game with mario in Java. When I run the code, it does not give an error, but the character does not move. I checked every bit but I could not find the error. I left the codes down. What should I do?
1 hour ago, Simla said:I'm playing a game with mario in Java. When I run the code, it does not give an error, but the character does not move. I checked every bit but I could not find the error. I left the codes down. What should I do?
You're going to have to post the code, and not complied files to get additional help.
I also noticed you said "I'm playing a game", is this not something you programmed?
Programmer and 3D Artist
2 hours ago, Rutin said:You're going to have to post the code, and not complied files to get additional help.
I also noticed you said "I'm playing a game", is this not something you programmed?
I am programming a game. I have just begun to learn English yet, I may have misread it.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.JPanel;
import javax.swing.Timer;
class Ates{
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Ates(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Oyun extends JPanel implements KeyListener,ActionListener {
Timer timer = new Timer(5, this);
private int gecenSure=0;
private BufferedImage image;
private ArrayList<Ates> atesler = new ArrayList<Ates>();
//ateş etme aktif olduğu zaman x ekseninde hareket etmesini sağlayacak değişken
private int atesEtX = 1;
private int düsman = 0;
private int düsmanHareket = 2;
//Marionun nereden başalayacağınnı belirliyoruz
private int marioX = 0;
//Sola veya sağa bastığımz zaman hareket etmesini sağlayacak
private int XMario = 20;
public Oyun() throws IOException {
try{
//Yukarıda tanımladığımız image değişkenine png dosyasını atıyoruz.
image = ImageIO.read(new FileImageInputStream(new File ("up.png")));
}
catch(IOException ex){
Logger.getLogger(Oyun.class.getName()).log(Level.SEVERE, null, ex);
}
setBackground(Color.DARK_GRAY);
timer.start();
}
@Override
public void repaint() {
super.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.MAGENTA);
g.fillOval(düsman, 0, 20, 20);//topun büyüklüğü
//Oluşturduğumuz karakterin konumunu ve boyutunu ayarladık
g.drawImage(image, marioX, 500, image.getWidth()*2, image.getHeight()*2,this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT){
if(marioX <=0){
marioX = 0;
}
else{
marioX -= XMario;
}
}
else if(c==KeyEvent.VK_RIGHT){
if(marioX >=1000){
marioX = 1000;
}
else{
marioX += XMario;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
//Düşmanı hareket ettirmek için
@Override
public void actionPerformed(ActionEvent e) {
}
}
-------------------------------------------------------------------------
import java.awt.HeadlessException;
import java.io.IOException;
import javax.swing.JFrame;
public class OyunEkrani extends JFrame {
public OyunEkrani(String title) throws HeadlessException {
super(title);
}
public static void main(String [] args) throws IOException{
OyunEkrani oe = new OyunEkrani("Super Mario");
oe.setResizable(false);
oe.setFocusable(false);
oe.setSize(1000, 700);
oe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Aşağıdki klavye işlemleri ile ilgili olan kodların sıralamaları önemli. Yreleri değiştiğinde çalışmayabilir.
Oyun oyun = new Oyun();
//fokusu isitiyoruz
oyun.requestFocus();
//klavyeden yapılan ilemleri anlamak için lullanıyoruz
oyun.addKeyListener(oyun);
//Odağı JFrame üzerinden çektik ve Jpanel üzerine verdik.
oyun.setFocusable(true);
//klavye işlemlerini anlayabilmesi için false yaptık
oyun.setFocusTraversalKeysEnabled(false);
oe.add(oyun);
oe.setVisible(true);
}
}
I might be missing something when I looked at your code above, but are you using a Game Loop? I cannot see one. All I can see if you have an event listener checking to see if LEFT or RIGHT is pressed, then you increase the X or decrease the X variable by 20. I see a PAINT class, but are you calling logic and drawing (x) times per second?
Again, sorry if I missed something, but if you're increasing and decreasing the x value with LEFT and RIGHT, but not updating screen you will not see movement even if it has occurred. You should also run your debugger to stop at each LEFT or RIGHT to confirm the change was really made.
If you're not using a Game Loop, your draw frame will look the same until updated regardless of logic changes.
If you're going to use JAVA to make games, I would suggest using a Library designed for game development such as: https://libgdx.badlogicgames.com/
Programmer and 3D Artist
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT){
if(marioX <=0){
marioX = 0;
}
else{
marioX -= XMario;
}
}
else if(c==KeyEvent.VK_RIGHT){
if(marioX >=1000){
marioX = 1000;
}
else{
marioX += XMario;
}
}
}
I move when I remove the 'if else', but this time I do not check whether the character goes out of the screen.
33 minutes ago, Simla said:public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT){
if(marioX <=0){
marioX = 0;
}
else{
marioX -= XMario;
}
}
else if(c==KeyEvent.VK_RIGHT){
if(marioX >=1000){
marioX = 1000;
}
else{
marioX += XMario;
}
}
}
I move when I remove the 'if else', but this time I do not check whether the character goes out of the screen.
Based on the above, if "Mario" starts at an X cord of 0, you will never be able to move because of the <= condition check and being an If Else it will completely skip the else part.
Keep the same code but replace it with:
if(marioX < 0){
marioX = 0;
}
else{
marioX -= XMario;
}
It would be a better practice to check if the move would be even possible before allowing it.
Example:
newPosX = marioX - XMario;
if (newPosX >= 0) {
marioX = newPosX;
}
Programmer and 3D Artist
1 hour ago, Rutin said:Based on the above, if "Mario" starts at an X cord of 0, you will never be able to move because of the <= condition check and being an If Else it will completely skip the else part.
Keep the same code but replace it with:
if(marioX < 0){
marioX = 0;
}
else{
marioX -= XMario;
}
It would be a better practice to check if the move would be even possible before allowing it.
Example:
newPosX = marioX - XMario; if (newPosX >= 0) { marioX -= XMario; }
it was done in this way. Thank you for your helping.