Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:40:20+00:00 2026-06-11T02:40:20+00:00

How do I use a single PNG image for multiple sprites? I’m trying to

  • 0

How do I use a single PNG image for multiple sprites?
I’m trying to make a simple 2d game and I don’t want to have like 20+ different image files.
I just want to put them on one PNG file.

Example

The terrain.png (and items.png) from minecraft has different tiles on it and each of the 16×16 pixel areas are used for a different texture of a block.

Can someone provide some code and an explanation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:40:21+00:00Added an answer on June 11, 2026 at 2:40 am

    I wrote a Java game a few years back so hopefully can give you some useful advice and code samples..

    You can organise your sprites in a single image like this:

    https://github.com/mikera/tyrant/blob/master/src/main/resources/images/creature32.png

    The example above uses 32×32 sprites, you can use any size you like but it helps to keep them regular.

    Then when you draw the screen during the game, you just pick off the respective sprite and draw in in the right location.

    Code might look something like this:

    public void drawImage(Graphics g,double x, double y, int image) {
        int px = (int)((x - scrollx) * TILEWIDTH);
        int py = (int)((y - scrolly) * TILEHEIGHT);
        int sx = (image%20) * TILEWIDTH;
        int sy = TILEHEIGHT * (image/20);
        g.drawImage(sourceImage, px, py, px + TILEWIDTH,
                py + TILEHEIGHT, sx, sy, sx + TILEWIDTH, sy + TILEHEIGHT,
                null);
    }
    

    Here the int image is an index into the position on the sprite sheet to use. Increment by 1 to move one sprite to the right, increment by 20 to move one sprite down in the sprite sheet.

    More complete source code available at: https://github.com/mikera/tyrant/blob/master/src/main/java/mikera/tyrant/MapPanel.java

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a UI like this image https://i.stack.imgur.com/bB8UM.png .I use the basic
I have a png and a jpg image on disk. I'd like to use
I want to use a single Doctrine install on our server and serve multiple
I'm trying to use one single image sprite for typical male / female radio
I want to use a single image sprite for my web page, where should
I want use a single php file to handle all of my voting requests.
How do you use a single character PHP Wildcard to make a String Replacement?
I want to use a single font named Algerian across my whole website. So,
I am trying to use a single RouteURL to route to different pages dependent
I have multiple images and I want to bind draggable events to each of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.