1,076,466 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by mxa92

Basically, the game goes like so-

User types a word they want player to guess (I've done this)
each letter in the word is displayed by "-"'s (I've done this)
Player guesses a letter in the word (I've done this)
Check guessed letter with each position in the word (I've done this)
Replace "-" for the letter per correct position (Stuck here....)

here's my code:

import java.util.*;

public class Hangman
{
    public static void main(String[]args)
    {
        Scanner kybd = new Scanner(System.in);
        int guesses = 0;
        
        System.out.print("Enter a word to be guessed: ");
        String word = kybd.nextLine();
        
        for (int i = 1; i <= word.length(); i++)
        {
            System.out.print("-");
        }
        System.out.println("");
        System.out.println("");
        
        System.out.print("Enter a letter to be guessed: ");
        String letter = kybd.nextLine();
        char letterchar = letter.charAt(0);
        
        for (int i = 1; i <= word.length(); i++)
        {
            if (letterchar == word.charAt(i))
            {
                //TRIED EVERTYTHING I COULD THINK OF.. ANY IDEAS? T_T
            }
        }
        
    }
}

Thank you

mxa92
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I keep running into errors when I try to draw an Array onto the screen using a specific section of my Sprite-sheet.

Here's the code I have to actually create my sprite-sheet:

#fields
// Fireball variables
        private Point frameSize6 = new Point(48, 47);      // Fireball image size
        private Point currentFrame6 = new Point(0, 0);     // Start frame
        private Point sheetSize6 = new Point(1, 6);        // Spritesheet size
        private int noOfFireballs = 15;                    // Total number of fireballs to display
                                       
        Vector2[] fireballPos;                            // Position of fireball(s)

        private int timeSinceLastFrame = 0;
        private int milliSecondsPerFrame = 500;           // 2 Frames Per Second (fps)


#initialisation
// Setup Fireballs
            fireballPos = new Vector2[noOfFireballs];         // Position of fireballs

private void resetGame()
{
            for (int i = 0; i < noOfFireballs; i++)          // Keep looping until each Fireball has been given a position                                   
            {
                fireballPos[i].X = Math.Max(0, rand.Next(gameWidth) - frameSize6.X);             
                fireballPos[i].Y = Math.Max(-500, rand.Next(gameHeight) - frameSize6.Y);                 
            }
}

        public override void LoadContent()
{
spriteBatch = ScreenManager.SpriteBatch;
fireballs = content.Load<Texture2D>("Graphics/Sprites/fireballs");
}

#update and draw
...

I can draw the entire sprite-sheet onto the screen by using:

for (int i = 0; i < noOfFireballs; i++)
            {
                    spriteBatch.Draw(fireballs, fireballPos[i], Color.White);                       
            }

but it doesn't seem to use the current frame I specified.. :S
How would I go about doing this?
Thank you

mxa92
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Just a side note, make sure you don't check for a collision with itself. (ie if i != j then do the check).

Ah, that got it! Thank you, and thank you gusano79 for the help :)

// Munchie colliding with the munchies
                for (int i = 0; i < noOfMunchies - 1; i++)
                {
                    for (int j = i; j < noOfMunchies; j++)
                    {
                        if (i != j)
                        {
                            if (CollisionCheck(munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize, munchiePos[j].X, munchiePos[j].Y, munchieSize, munchieSize))
                            {
                                munchiePos[j].X = Math.Max(0, rand.Next(gameWidth) - munchieSize);
                                munchiePos[j].Y = Math.Max(-500, rand.Next(0) - munchieSize);
                            }
                        }
                    }
                }
mxa92
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I've just tried that, and I have tried a variation of that in previous attempts; it just seems to keep the munchies constantly re-spawning at the top of the screen as if they're constantly colliding with one another... =S

mxa92
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
public Boolean CollisionCheck(float x1, float y1, float width1, float height1, float x2, float y2, float width2, float height2)
        {
            float left1 = x1;
            float left2 = x2;
            float right1 = x1 + width1;
            float right2 = x2 + width2;
            float top1 = y1;
            float top2 = y2;
            float bottom1 = y1 + height1;
            float bottom2 = y2 + height2;

            if (bottom1 < top2)
                return false;
            if (top1 > bottom2)
                return false;
            if (right1 < left2)
                return false;
            if (left1 > right2)
                return false;

            return true;
        }

That's my collision detection code, which should return true is 2 boxes are touching in any direction.

It works if I'm comparing 2 different images (Ie. the floor tiles and the character (Pacman - if Pacman isn't touching the floor tile he falls, dies, and respaws:

for (int i = 0; i < noOfTiles; i++)
                {
                    if (CollisionCheck(pacmanPos.X, pacmanPos.Y, frameSize.X, frameSize.Y, tilePos[i].X, tilePos[i].Y, frameSize2.X, frameSize2.Y))
                    {
                        //continue as normal
                    }
                    else
                    {
                        pacmanPos.Y ++;
                    }

                }

However, I have an array of enemies (munchies) and trying to stop them spawning on top of each other is proving difficult (They re-spawn in a random x and y coordinate):

for (int i = 0; i < noOfMunchies; i++)
                    if (CollisionCheck(munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize, munchiePos[i].X, munchiePos[i].Y, munchieSize, munchieSize))
                    {
                        // change the position of the munchie that collided
                        munchiePos[i].X = Math.Max(0, rand.Next(gameWidth) - munchieSize);
                        munchiePos[i].Y = Math.Max(-500, rand.Next(gameHeight) - gameHeight);
                    }
                }

I understand the problem, it's just checking a collision with the munchie at position i, with the same munchie at position i... but I don't understand how to fix it :/
Thank you to anyone who can help, i'm truly stuck T_T

mxa92
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.0505 seconds using 2.51MB