Colour Words by Fredrik Ramsberg, Basic Tenliner contest, 2020.
===



Program description
---

The goal of the game is to count the number of words matching the criteria, as 
quickly as possible. After six rounds, the game prints your average response 
time. If you answer a question incorrectly, you get a message saying it was 
the wrong answer, and the game starts over.

Controls: The keys 0,1,2,3,4,5,6



Instructions to start the game
---
Run in any Commodore 64 emulator which supports disk images. Vice is always nice.

1. Insert the disk in drive 8.
2. Type: load "colour words",8
3. Type: run




Code description
---

0 
	s=54259						; The base address of the SID (sound) chip, minus 13 (for space saving reasons)
	for i=0 to 4				; A loop to read data AND setup the voices of the SID chip. This will also POKE
								; some (harmless) addresses just before the SID chip.
		read p$(i),c$(i)		; Read the print control code and the colour name for a colour. If i > 3, the data
								; read is instead words for the two types of questions the game can ask.
		j=s+7*i					; Base address of the current voice + 1 (+1 is for space saving reasons)
		poke j,40				; Set the highbyte of the frequency
		poke j+4,4				; Set Attack/Decay
		poke j+5,70				; Set Sustain/Release
	next
1 
	c=.							; Set number of completed rounds to 0
	g=.							; Set accumulate # of jiffies spent anwering questions to 0
	def fn r(x)=int(rnd(1)*x)	; Define a function to draw a random integer in the range 0 to x-1
	poke 53280,0:poke 53281,0	; Set the border colour and background colour. This and the def statement
								; logically belong on line 1, but there is no room there.
	if w=0 then w=1e6			; Initialize the record time to 1000000. This logically belongs in line 0,
								; where it would just be "w=0", but there is no room there.
2 
	m=1-m						; Let question type alternate between type 0 and 1 for each round	
	f=fn r(3)					; Pick the colour the player should search for
	k=.							; Initialize the number of words which match the criteria
	print "{darkgrey}{home}{down*3}count the "p$(3+m)p$(f nr(3))c$(f)"{darkgrey}"c$(3+m)"{down*2}"	; Print the question
3 
	poke s+37,15				; Set the master volume. This really belongs on line 0, but there is no room there.
	poke s+17,17				; Start the sound effect in voice 1 (for a new round)
	for i=0 to 5				; Loop to print six coloured words
		a=fn r(3)				; Randomize the colour of the word
		b=fn r(3)				; Randomize the colour name
		print p$(a)c$(b)" {darkgrey}";	; Print the colour name using the selected colour
4 
		k=k-(m=0andb=f)-(m=1anda=f)	; Increase k (the correct answer) if this word matches the criteria
	next
	poke s+17,16				; Stop the sound effect in voice 1 (for a new round)
	poke 198,0					; Clear the keyboard buffer
	ti$="000000"				; Reset the real time clock
	c=c+1						; Increase the number of rounds completed
5 
	e=ti						; Put the number of elapsed jiffies in a variable, so we can print it now
								; an retrieve it later if the player answered correctly.
	print "{home}"e,c"/ 6"		; Print the number of jiffies elapsed and the round number
	get a$						; Get the last key pressed, if any.
	if a$<"0" or a$>"6" then 5	; Unless it's a valid answer, repeat this line.
	data"e",white,"^",green,"_"	; There was a little room left on this line, so we put some data here.
6 
	a=asc(a$)-48				; Translate the PETSCII code of the key pressed (48-54) to a value 0-6
	poke s+33,201				; Set the Sustain/Release of voice 3. This belongs logically in line 0, but
								; there is no room there.
	if a=k then 				; If the answer is correct then...
		g=g+e					; Add the # of jiffies taken to the total
		y=int(g/c)				; Calculate the average # of jiffies used. Logically this belongs in line 8,
								; but there is no room there.
		on 2+(c<6) goto 2,8		; If this was the last round, jump to line 8, otherwise jump to line 2
7 
	poke s+24,129				; Start the sound effect in voice 2 (for an incorrect reply).
	print "fail. was "k" not "a$" (ret)"	; Print a message that the answer is incorrect.
	pokes+24,128				; Stop the sound effect in voice 2 (for an incorrect reply).
	wait198,1					; Wait for a key to be pressed.
	goto1						; Go back to line 1 to start a new game
8 
	poke s+31,33				; Start the sound effect in voice 3 (for completing the final question).
	print "{home}{white}victory! avg jiffies:"y		; Print the average # of jiffies used to answer the six questions.
	if y<w then					; If this is a new record then...
		w=y						; Remember the record
		print "[down}new record!"
9 
	print "{down}(ret)"
	poke s+31,32				; Stop the sound effect in voice 3 (for completing the final question).
	wait198,1					; Wait for a key to be pressed.
	goto1						; Go back to line 1 to start a new game
	data blue,"word ",,," words"	; Last part of the data which is read on line 0.



