Question & Answer: I'm trying to make a Mario clone, and I can make sprites move with a keyListener, but I want to make sprites anim…..

I’m trying to make a Mario clone, and I can make sprites move with a keyListener, but I want to make sprites animate as they move. When the sprite moves, I want this:

https://i.stack.imgur.com/e9SuM.png

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: I'm trying to make a Mario clone, and I can make sprites move with a keyListener, but I want to make sprites anim…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

it starts as the left one, and when I hit the move right key, it shows the middle, and when it gets to the right, is the one on the right.

[![the middle sprite is shown once the move right button is hit, and it finishes when then sprite is two pixels right and goes back to original][1]][1]

So far, I have:

I’m trying to animate the sprite in my game when a button is pressed, but when I press the button, it skips the animation. Its supposed to go one pixel, change sprites, and then go one more pixel and change back. Here is the code

//for all
import java.nio.file.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.image.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.invoke.MethodHandles.*;
import java.awt.event.*;

//my Mario class (cut down a lot)
class Mario {
// all numbers multiplied by 2 from OG game

protected Direction dir;
protected int x, y;
protected BufferedImage sprite;
protected String currentSpriteName;

public Mario() {

this.x = 54;
this.y = 808;
dir = Direction.RIGHT;
setSprite(MVCE.SMALLSTANDFACERIGHT);
currentSpriteName = MVCE.SMALLSTANDFACERIGHT;
}

public void moveRight(){
if(this.dir == Direction.LEFT){
this.dir = Direction.RIGHT;
}
else if(this.dir == Direction.RIGHT){
this.x+=2;
}
}

public void jump() {
this.y -= 46;
}

public void setSprite(String spriteName) {
URL spriteAtLoc = MVCE.urlGenerator(spriteName);
this.sprite = MVCE.generateAndFilter(sprite, spriteAtLoc);
}

public void getSprite(){
System.out.println(this.currentSpriteName);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(sprite, 0, 0, null); // DO NOT SET x and y TO ANYTHING,
// this sets 0,0 to top left!!
}

}

<?– –>

// my MarioRender class:
public class MarioRender extends JLabel{

protected Mario marioSprite;

public MarioRender(){
marioSprite = new Mario();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
marioSprite.paint(g2);
setBounds(marioSprite.x, marioSprite.y, marioSprite.sprite.getWidth(), marioSprite.sprite.getHeight());
}

public void moveMarioRight(){
marioSprite.moveRight();
marioSprite.setSprite(MVCE.SMALLSTANDFACERIGHT);
setLocation(this.marioSprite.x, this.marioSprite.y);
repaint();
}

public void jumpMario(){
marioSprite.jump();
marioSprite.setSprite(MVCE.SMALLJUMPFACERIGHT);
setLocation(this.marioSprite.x, this.marioSprite.y);
repaint();

}

}

<?– –>

// direction class, solely for moving
enum Direction {
LEFT, RIGHT
}

<?– –>

// my calling class, which I called MVCE where I make the frame
public class MVCE extends JFrame{

MarioRender m = new MarioRender();
JLabel bg;

public MVCE(){
bg = new JLabel();
this.setSize(868, 915);
this.setVisible(true);
this.add(bg, BorderLayout.CENTER);
bg.setLayout(null);

bg.add(m);
m.setBounds(m.marioSprite.x, m.marioSprite.y, m.marioSprite.sprite.getWidth(), m.marioSprite.sprite.getHeight());
KeyListener kl = new MoveListener();
this.addKeyListener(kl);
this.setFocusable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static final String SMALLSTANDFACERIGHT = “SmallStandFaceRight.bmp”; //30 x 32
public static final String SMALLJUMPFACERIGHT = “SmallJumpFaceRight.bmp”; //32 x 32
public static final String SMALLWALKFACERIGHT = “SmallWalkFaceRight.bmp”;

//generate URL
public static URL urlGenerator(String name){
URL u = lookup().lookupClass().getResource(name);
return u;
}

//return image with filtered color
public static BufferedImage generateAndFilter(BufferedImage b, URL u){

try{
b = ImageIO.read(u);
int width = b.getWidth();
int height = b.getHeight();
int[] pixels = new int[width * height];
b.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
//System.out.println(pixels[i]);
if (pixels[i] == 0xFFff00fe) {
pixels[i] = 0x00ff00fe;
}
}
BufferedImage newSprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
newSprite.setRGB(0, 0, width, height, pixels, 0, width);
b = newSprite;
}
catch(IOException e){
System.out.println(“sprite not found”);
e.printStackTrace();
}
return b;
}

//key listener
class MoveListener implements KeyListener{
public void keyPressed(KeyEvent k){

if((k.getKeyCode() == 39)){
m.moveMarioRight();
m.marioSprite.getSprite();
System.out.println(m.marioSprite.getCoordinates());
}

if(k.getKeyCode() == 83){ //S key

m.jumpMario();
m.marioSprite.getSprite();
System.out.println(m.marioSprite.getCoordinates());
}

}
public void keyReleased(KeyEvent k){}
public void keyTyped(KeyEvent k){}
}

public static void main(String[] args){
MVCE m = new MVCE();
}

}

in my game, each sprite is its own file. I will not, under any circumstance, use a sprite sheet.

Expert Answer

 

Try adding this code to your above program.Hope it will run well.

MovingSprites.java

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class MovingSprites {

private int x-co;
private int y-co;
private int x;
private int y;
private Image images;

public MovingSprites() {

initSprite();
}

private void initSprite() {

ImageIcon img = new ImageIcon(“MovingSprites.png”);
images = img.gettingImages();
x = 40;
y = 60;
}

public void moving() {
x += x-co;
y += y-co;
}

public int getXco() {
return x;
}

public int getYco() {
return y;
}

public Image gettingImages() {
return images;
}

public void keyPressed(KeyEvent e) {

int keyss = e.getKeyCode();

if (keyss == KeyEvent.VK_LEFT) {
x-co = -1;
}

if (keyss == KeyEvent.VK_RIGHT) {
x-co = 1;
}

if (keyss == KeyEvent.VK_UP) {
y-co = -1;
}

if (keyss == KeyEvent.VK_DOWN) {
y-co = 1;
}
}

public void keyReleased(KeyEvent e) {

int keyss = e.getKeyCode();

if (keyss == KeyEvent.VK_LEFT) {
x-co = 0;
}

if (keyss == KeyEvent.VK_RIGHT) {
x-co = 0;
}

if (keyss == KeyEvent.VK_UP) {
y-co = 0;
}

if (keyss == KeyEvent.VK_DOWN) {
y-co = 0;
}
}
}

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.