자바 게임 인벤토리 - jaba geim inbentoli

Java lovers! I am posting a code that I quickly created regarding the ability to keep track of a players inventory in Java during a text adventure. There are many other uses for this type of code (as experts know), but today I decided to help out the beginners and post this code for critique and use in a text adventure if you are having trouble. This is not a tutorial on how to make a text adventure, only add an inventory function. So here we go:

First thing we need to do is import the necessary pre-requisites:

import java.util.ArrayList; import java.util.List;

Obviously, the scanner will already be imported. Next, we have to create the list:

List<String> inv = new ArrayList<String>();

The "List" function creates a new list. This will store all data for the inventory; I called my list 'inv'. Now we have our first action! Mine is hypothetical (remember), but you can do whatever you'd like.

System.out.println("You see a rusty axe on the floor next to a can of pepsi."); choice = scan.nextLine();

This is a very simple "scan for input" kind of code. Nothing new, I assume. Now comes the fun part:

if (choice.equals("take axe")) { System.out.println("You pick up the axe"); inv.add("axe");

Did you see that there? We just added the axe to the players inventory! It's that simple. We can display the inventory now using:

System.out.println(inv);

Done! Now for using up an item. I will use a different example where the player picked up the pepsi:

System.out.println("You walk for awhile and start to become thirsty"); System.out.println(inv); choice = scan.nextLine();

We print the inventory so the player knows what is available to use. Now watch this:

if (choice.equals("drink")) { if (inv.contains("pepsi")) { System.out.println("You drink a can of pepsi"); inv.remove("pepsi");

Thats right! All we do is use the remove function. But now let's say the player decided NOT to take the pepsi. We are going to make an 'else if' to the previous if statement:

if (inv.contains("pepsi")) { System.out.println("You drank pepsi"); inv.remove("pepsi"); } else if (!inv.contains("pepsi")) { System.out.println("You have nothing to drink!"); }

Awesome! All we did was put an exclamation mark (meaning opposite) in front of the conditional statement. If you don't understand, check out down below.

Anyways, glad I could help you out (there is a reason it is called easy inventory!). If you have any questions, reply asking whatever you wish (even if it's for me to make another tutorial). Leave feedback if this was no use to you (because you already know what you're doing) and tell me how to get better. Thanks!

자바 스레드를 사용한 게임 만들어 보기

가장냥이의 생선가게 털러가기

[게임 설명]

  • 길고양이인 가장냥이는 자식냥이들을 먹여 살리기 위해 생선을 털어 자식냥이들을 키워야합니다.

  • 7일 안에 바퀴벌레, 쥐, 뱀 등 적들과 싸우거나 구걸을 하여 레벨업을 하고, Level 4 이상이 되면 사장의 눈을 피해 최종 미션인 생선가게 털기 게임을 완료하면 자식냥이들을 먹일 생선을 확보하여 해피엔딩으로 끝나게 됩니다.

[기능]

  1. 인벤토리 : 상점에서 구매한 물품을 확인, 사용 할 수 있습니다.

  2. 냥이 상점 : 물건을 사거나, 전리품을 팔아 돈을 획득할 수 있습니다.

  3. 사냥하기 : 바퀴벌레 < 쥐 < 뱀들과 싸워 경험치를 획득해 레벨업을 합니다.

  4. 생선가게 털러가기

    • LEVEL 4 이상이 되면 생선가게를 털러 갈 수 있습니다.

    • 밤에는 생선가게가 문을 닫기 때문에 낮에만 미션 수행이 가능합니다.

    • 사장이 생선가게를 지키지 않을 때 미션 수행이 가능합니다.

    • 제한시간 10초 동안 10번을 실수 없이 연속으로 생선이미지를 클릭한다면 미션성공입니다.

  5.  구걸하기 : 랜덤으로 착한 사람과 나쁜 사람, 도둑 냥이를 만날 수 있습니다.
    • 착한 사람 : 참치캔을 얻어 HP를 회복합니다.
    • 나쁜 사람 : 돌을 맞아 HP가 감소합니다.
    • 도둑냥이 : 도둑냥이와 싸움을 해 HP가 감소합니다.
  6. 상태 확인하기 : 가장냥이의 현재 상태를 알 수 있습니다.

[Thread]

  1. 텍스트 Thread
    • 프롤로그, 게임 설명 등 출력 시 생동감있는 표현을 위해서 한줄, 혹은 한글자씩 출력합니다.
  2.  로딩 Thread
    • 캐릭터 생성, 미션 장소 입장 시 실제 로딩이 되는 것처럼 표현하기 위해 사용합니다.
  3. 음악 Thread
    • 각 상황마다 적합한 음악, 효과음을 재생합니다.
    • 배경음악 : 사냥터 BGM, 사냥터를 제외한 나머지 BGM
    • 효과음 : 타이핑 효과음, 버튼 클릭음, 아이템 획득/사용 효과음, 미션 실패/성공 효과음, 타격음 등등
  4. 날짜 Thread
    • 하루를 2분으로 가정하고, 1분마다 낮과 밤이 반복됩니다.
    • 밤에는 생선가게가 문을 닫아 미션을 수행할 수 없으며, 7일이 지나면 미션 실패로 끝이납니다.
  5. 생선잡기 게임 제한시간 Thread
    • 제한 시간 10초 안에 생선잡기 미션을 수행하지 못하게 미션실패입니다.
  6.  사냥터 제한 시간 Thread\
    • 사냥을 시작하고 10초안에 몬스터를 죽이지 못하면 사냥에 실패합니다.
  7. 몬스터 자동공격 Thread
    • 몬스터가 2초마다 자동으로 가장냥이를 공격합니다.

[GUI]

  1. 날짜 알림 GUI : 시간과 날짜, 생선가게 운영시간을 보여줍니다.
  2. 생성잡기 게임 GUI : 랜덤으로 출력되는 생선 이미지를 10초 안에 연속으로 10번 맞추는 게임입니다.

[ 시연영상 ]

//github.com/jungeun960/Java-game

jungeun960/Java-game

자바 게임 만들어보기. Contribute to jungeun960/Java-game development by creating an account on GitHub.

github.com

Toplist

최신 우편물

태그