#Survival Game Help

31 messages · Page 1 of 1 (latest)

jovial mural
#

I'm new to Java, and just trying to create a simple, fun zombie survival game. One of the things I want to do is to create an array of things in the users backpack, but I'm wondering how I can put a certain quantity of something inside the backpack, so users can only use it so many times. An example would be arrows. Users would have arrows in their backpack, but how can I make it so the user can only use the arrows x amount of times before it's removed from the backpack?

cyan kelpBOT
#

This post has been reserved for your question.

Hey @jovial mural! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tepid jewel
jovial mural
#

so "item" : 1, or something like that?

young forge
#

not variables, just values

#

it's called a key here, so it maps keys to values

cyan kelpBOT
young forge
#

there's a section on the collections api there ^

jovial mural
# young forge there's a section on the collections api there ^

Gotcha gotcha, ty. I had a question regarding giving users lives in the game too. I've written plenty of programs like story games in Python, but Im wondering how I can give users, say, 2 lives, and while the user still has at least 1 life, the game can be played, but when they get to 0 lives, the game ends. Do you know how I could write that in Java?

young forge
#

are there multiple players at once? or is it singleplayer

jovial mural
young forge
#

right that would just be a field

#

you would have checks to the current life count whenever you lose a life

#

or any other method, really

#

there's no logical difference from python, just semantic difference

#

you could really just translate the python into java if you want to learn its syntax. it wouldn't necessarily be following java patterns, but it could be reasonably done.

jovial mural
young forge
#

arrays don't really have variables

#

arrays have values, but yeah you can't remove them outright, since they're fixed size

jovial mural
young forge
#

list -> List
dict -> Map
set -> Set
tuple -> array

use this mapping for data types (for now)

young forge
jovial mural
young forge
#

List is an interface, it just describes the data structure
ArrayList is a class implementing List, it's an implementation of that data structure

#

usually the "main" implementations are ArrayList (sometimes LinkedList), HashMap, HashSet

hollow patrol
#

In addition to what others have said here, you could use a more data-oriented approach (for info on what that is, google data-oriented design) and simply make two arrays, for example:
String[] items = new String[]{...};
int[] itemCount = new int[]{...};
And then access both using the same integer index. Whether you do it this way, or using more abstract data, is up to you and your design.

tepid jewel
hollow patrol
#

it does look a little messy, but it's also simpler to understand. It's almost definitely going to be more optimized (arrays take advantage of contiguous memory, which is nicer on the CPU during data iterations), but that probably doesn't matter if someone is just making a text game. If you're wondering what it would look like, here's some sample code for checking a player's inventory to see if they have enough items, given the above data setup:
static boolean checkInv (int selectedItem){
if (itemCount[selectedItem] > 0) {return true;}
else {return false;}
}

hollow patrol