#How to make a randomly generated math quiz game with space shooter game mechanic? (asking for help)

75 messages · Page 1 of 1 (latest)

edgy ember
#

I'm gonna be very active at times, but I really been struggling with this (mainly because idk math that well and idk how to code and this is my first time). I've been asking chatgpt because my brother recommends me to if idk stuff, but it just doesn't work. I'm gonna focus on other stuff for a game for my college essay so if anyone knows about this, please tell me... show me and explain how it works... I really want to learn but felt like an idiot ;-;

minor wraith
#

Ok, for starters it'd be good to share what you progress you allready have made on this

#

And what specifically you are struggling with

#

Or are you looking for more of a step by step walk through?

edgy ember
#

a step by step walkthrough sounds better. I really not sure of anything anymore after a few weeks struggling with this...

Right now, I've done the player movement, set up collision, put some ui with what available in the editor, drawn some sprite, I had some videos of how to implement bullets and enemies, but the randomly generated math question is... confusing i guess, no progress at all. too little info on the internet. I thought it would be simple, but I guess it's not

#

Sorry for not replying early, I only open discord at night most of the time 😓

minor wraith
#

Ok that's fine, one more thing, what language are you using?

edgy ember
#

GDScript

#

my college teachers who are supposed to teach me code, didn't and only give assignments (basically me and my brother look up online for all of them). The same goes when I was at highschool.

So I go and learn some gdscript from gdquest... tutorial? idk, it was helpful to some extend at least. So most things I know aren't much and just simple.

minor wraith
#

Ok heres roughly how id go about implementing this. If you have any questions about any of this don't hesitate to ask.

First I would suggest defining a few internal classes. one as an abstract base class to define the interface and common functionally, and any number of inherited classes for each equation type. Here as an example, ive only got a ABS and one for a + b = ? equations.

# abstract base class for defining a math question
class MathQuestionABS:
    var solution
    func verify_answer(answer): # fixed method name
        # checks an answer, returns true if correct false otherwise
        return float(answer) == solution
    func get_question_as_string():
        # returns equation formatted as a string
        var txt = "Error Unimplemented."
        print(txt)
        return txt

# concrete class for math questions of the form a + b = ?
class MathQuestionAPlusB extends MathQuestionABS:
    var a
    var b
    func _init():
        # initialize variables
        a = randi_range(0, 10)
        b = randi_range(0, 10)
        solution = a + b # calculate answer
    func get_question_as_string():
        # returns equation formatted as a string
        return "%d + %d = ?" % [a, b]

Then wherever you wanna process these from, you could define a similar method that checks the answer and generates another.

func next_question(answer = null):
    if current_question != null: # only check answer if there is a question
        if current_question.verify_answer(answer): # check solution
            score += 1 # increment score if correct
    answer_line.text = "" # reset answer field
    score_label.text = "Score %d" % score # update score label
    current_question = MathQuestionAPlusB.new() # get new question
    question_label.text = current_question.get_question_as_string() # display new question

Here this method could be connected and called from the awnser_line LineEdit via the text_submitted(new_text:String) signal.

edgy ember
#

I never understand how classes works...

There's a lot i didn't know about here like the func next_question(answer = null), didn't know that's possible. Also does the classes just being put on a unattached script or something? I don't see the contents in the classes connectes with that last function...?

Man I hate not knowing anything

dull lagoon
#

Even if your teacher isnt doing anything, it can be easily replaced by youtube tutorials and asking people on the internet

#

programming is a pretty unique subject

#

(not saying your teachers are doing the right thing though)

minor wraith
#

Instead of trying to explain classes here in text, this guy seems to do a good job of explaining the concept https://www.youtube.com/watch?v=8yjkWGRlUmY&list=PLBZBJbE_rGRV8D7XZ08LK6z-4zPoWzu5H&index=3. Try reimplementing his classes in godot as you follow along. Also, you should also read over https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html. This documents all of the gdscript language features. I'd highly suggest setting up a sandbox and just trying out the stuff it mentions.

Object oriented programming tutorial! Java & Python sample code available below.

Check out Brilliant.org (https://brilliant.org/CSDojo/), a website for learning math and computer science concepts through solving problems. First 200 subscribers will get 20% off through the link above.
Special thanks to Brilliant for sponsoring this video.

Find ...

▶ Play video
#

as for func next_question(answer = null) the answer = null is just supplying a default if you don't provide one when calling that function. that way you can either call next_question(10) to pass in an answer or next_question() to call it with out giving an answer. speaking of which, should really be handled somewhere in there as float(null) throws an error. lets just call fixing that extra credit.

#

and yes MathQuestionABS and MathQuestionAPlusB are defined in the same script as next_question()

#

Where they connect is the current_question variable. current_question starts off as null to represent no question, then when we run current_question = MathQuestionAPlusB.new() we create a new instance of MathQuestionAPlusB and assign it to current_question. after we have done this, we can call the methods that we defined on that object such as in the lines current_question.verify_answer(answer) and question_label.text = current_question.get_question_as_string()

#

And don't worry to much about not knowing anything. programming is more about being able to read documentation and learn something new than already knowing everything. this stuff is complicated and varied enough that no one will never know it all.

#

in other words, its more about knowing how to learn, instead of just knowing

hollow solar
edgy ember
#

God... it felt like I'm gonna need a lot more time to understand all this 💦
Thanks for the help for now, I'll ask more question if I have any after... this

minor wraith
#

Don't worry, yea coding is complicated, but you got this.

edgy ember
#

I have a question of the code you showed me. I've studied something at least (a bit), and I'm trying out the codes... and Idk what to do with the current_question. I just put it as var current_question and nothing else since I have no idea what it is. and I got an error that says Invalid call. Nonexistent function 'verify_answer' in base 'RefCounted (MathQuestionAPlusB)'

minor wraith
# edgy ember I have a question of the code you showed me. I've studied something at least (a ...

var current_question implies var current_question = null which is fine since there is a check for null in next_question() so thats all good. null is being used as the "no question" value. As for the error you are seeing. it is saying that MathQuestionAPlusB does not have a method named verify_answer. that's my bad. looks like when I renamed MathQuestionAPlusB.check_answer() to MathQuestionAPlusB.verify_answer() in that example, I did not update that everywhere. i've corrected in the above code.

#

so current_question is either and instance of a sublcass of MathQuestionABS or null if there there is no question

edgy ember
#

Now I got the error Invalid call. Nonexistent 'float' constructor. I honestly want to learn this on my own, but maybe later. Right now I'm very low on time and I just want to finish this and finish my essay so I'm not stressing over paying more for college ;-; (Sorry) at least I understand what I'm seeing, even if I'm slow

minor wraith
#

the issue here is in the line return float(answer) == solution where awnser is set to some invalid value. ex null. this was what I was talking about above with the extra credit.

#

to fix this you need to add a check somewhere before that float conversion that ensures that answer is not null, just like we did with current_question

#

fyi you can pass any string into a float() and not get an error. if the sting is not a valid number it just returns 0.0 ex: float("a") == 0.0

edgy ember
#

I tried making it like this... I think I made it worse

#

idk what happened, but it's still the same error and I had no idea how or where do I convert the thing.

Edit: Why is converting something into other things so hard???
I feel like an idiot already

edgy ember
#

a different error...? (I was trying searching the internet for help for this)

raven glade
#

As of now, what type is answer?

#

And where are you calling verify_answer?

edgy ember
#

Verify is being called in the function next_question like this (I still don't know some of this yet.) and I just connect it to lineedit like i was told. The value for answer... I think at first it's just null and then at the return it's being changed to float using float() constructor (I think that's what it's called).

minor wraith
#

personally I think the best plac4e to add the check would be up with the check for current question

func next_question(answer = null):
    if current_question != null and answer != null: # only check answer if there is a question and an awnser
        if current_question.verify_awnser(answer): # check solution
            score += 1 # increment score if correct
    # more code

but really checking awnser != null needs to be done before you try to convert it to a float

#

seccond, you want to pass new_text into next_question so it actually has the answer instead of just using the default answer null

#

or you could just reconfigure the signal to call next_question directly

#

but that where that null gets introduced from, when you call next_question with out any arguments

#

just as an example, if you have this function

func foo(arg_with_default = null):
    print(arg_wiht_default)
#

calling foo("123") would print 123

#

calling foo() would print <null>

#

you can also set the default to anything you like

func bar(arg_with_default = "No arg was provided"):
    print(arg_wiht_default)
#

calling bar("123") would print 123

#

calling bar() would print No arg was provided

#

the reason to use null is its kind of the nothing value. its not an int its not an object, its nothing, so it works well to use as the state for "I don't have whatever this is"

#

in this case, it lest us not have an answer, and just continue to the next question, which is nice because we could then use it as an initializer for the question as well

minor wraith
#

also fyi string_var.to_float() and float(string_var) in this case effectively do the same thing. float() is just a bit more flexible as it can work on floats and ints too.

#

ok so in summary

  1. pass the text received from the signal into next_question()
  2. ensure answer != null before before passing it into the float constructor
edgy ember
#

Ngg... I'll try to make sense of this later while writing the essay, for now at least it didn't give me errors. Now I have to figure out how to make the points get added (because right now it doesn't do that apperently even though I put in the right answer) and I have to work on... some kind of power ups and a difficulty scaling with faster enemy spawn, which I'll think about later

minor wraith
#

Ok just make sure you do, there's little point in learning how to code, if you just skip over the details

edgy ember
#

I plan on learning a lot about godot and gdscript... But I give up on making big games and will probably make text adventure game instead because of this

#

I'm right now just using ai, not very helpful but helps shows me how to structure code, and then I fix them with my best abilities (but it fails when it comes to... this quiz... 9 months wasted)

#

...okay, back to quiz I guess. I tried putting print() on this. I test and put in the right answer... and nothing. it just generate a new question.... I feel like throwing something hard right now ;-;
I'm gonna reread some chat history for now... maybe I miss some of your explanation

minor wraith
#

if you don't know how to use the debugger, now would be a good time to learn

#

you can get code to pause when execution reaches a line with a breakpoint by cliching here in the gutter

#

while code is stopped at a breakpoint you can see the values of all your variables

#

this can make it real easy to see where your code is going wrong

#

there are also these buttons, which let you step through your code so you can see exactly where execution of your code goes

edgy ember
#

apperently answer never got a value...? do I haveto add some kind of funtion to make my answer from LineEdit (or maybe some kind of var that acts like that node but using buttons as keyboard). to get it receive the value?

minor wraith
#

When you call next_question you need to pass in the value of the line edit

#

Ex: next_question(new_text)

#

Otherwise it uses the default value null

edgy ember
#

ah, I'm gonna try

#

...oh, lol

#

why the error didn't appear before running the scene? It's been showing one for every spelling mistake...
anyways, thanks for the help. I'm gonna keep this open in case I have question about the other part of this project... I hope I finish this soon

minor wraith
#

no problem

edgy ember
#

How to make a randomly generated math quiz game? (asking for help)

#

...Yea, it didn't go well for me. I'm trying to code the enemy... on something. I've got some code from someone's video for the bullets, but the enemy's ship movement from spawn, patrolling left and right in the gameplay area (or the box where the player can move) while moving down each time it sort of touch the wall of the box thing (idk what to call it).

...I'm losing track on what I'm actually trying to code...

#

Hmm... Since I'm just gonna record it, might as well improvise.

...I'll f---ing spam them in the gameplay screen after figuring out how to make them move and patrol... and shoot... urgh I really should've went with something simpler for my essay proposal...

edgy ember
#

Okay, Question to anyone reading this... how do I spawn something... or at least what am I supposed to do with this instance()? Because I have no clue what I'm doing and it just works and keeps saying Invalid call. Nonexistent function 'instance' in base 'PackedScene'.