Quiz

In this chapter, you’ll create a multiple choice quiz. It’s intended as an introduction to data and file handling. You do not need to understand every single line of the code; just try to understand the concepts being taught. This game will allow question

  • PDF / 2,400,987 Bytes
  • 19 Pages / 504 x 720 pts Page_size
  • 82 Downloads / 149 Views

DOWNLOAD

REPORT


Quiz In this chapter, you’ll create a multiple choice quiz. It’s intended as an introduction to data and file handling. You do not need to understand every single line of the code; just try to understand the concepts being taught. This game will allow questions to be loaded from code or downloaded from a website as a text file and imported into the game, with the player aiming to get as many correct answers as possible. So, start GameMaker Studio 2, start a new GML project, and name it something like Quiz. If you completed Chapter 1 successfully, you should now know how to create and name sprites and load images in. You can also create a new sprite by right-clicking in an empty area of the workspace and selecting the resource that you wish to create. Load in from the Resources folder and name the following sprites: •

spr_button with an origin of middle center



spr_clock with an origin at the middle of the clock’s face



spr_clock_hand with an origin at position 6x4



spr_from_code with an origin of middle center



spr_from_web with an origin of middle center



spr_question_bg with an origin of middle center

The sprite named spr_badge is a strip consisting of three frames. Create it and click Edit ➤ Image ➤ Import From Strip Image. Set the number of frames and frames per row to 3, frame width to 249, and frame height to 209, as shown in Figure 2-1.

© Ben Tyers 2018 B. Tyers, Practical GameMaker Projects, https://doi.org/10.1007/978-1-4842-3745-8_2

49

Chapter 2

Quiz

Figure 2-1.  Import settings for the sprite strip The first object is obj_add_from_code, which has no sprite. Add the following code to the Create Event. It sets up some initial game values and data structures known as arrays, which hold the question, answer options, and the correct answer: /// @description Set For Data global.current_question=1; global.number_of_questions=5; global.correct=0; global.question[1]="What is the capital of England?"; global.option1[1]="London"; global.option2[1]="Paris"; global.option3[1]="New York"; global.answer[1]=1; global.question[2]="What is a female swan called?"; global.option1[2]="Sow"; global.option2[2]="Pen"; global.option3[2]="Kitten"; global.answer[2]=2; 50

Chapter 2

Quiz

global.question[3]="How many legs does a dog have?"; global.option1[3]="1"; global.option2[3]="2"; global.option3[3]="4"; global.answer[3]=3; global.question[4]="What is the square root of 16?"; global.option1[4]="2"; global.option2[4]="4"; global.option3[4]="8"; global.answer[4]=2; global.question[5]="What colour is the moon?"; global.option1[5]="Blue"; global.option2[5]="Gray"; global.option3[5]="Purple"; global.answer[5]=2; room_goto(room_quiz); That is all for this object. The next object is obj_add_from_web. It has no sprite. Add a Create Event with the following code. It downloads a file from a website that holds the questions, answer options, and correct answer in a text file. This is an asynchronous event, which basically means it works in the background while the game continues. /// @description Get File 7 S