Logical Error in my program

Monkey Forums/Monkey Programming/Logical Error in my program

ewancordiner(Posted 2017) [#1]
'Importing Modules
Import mojo
Import math
Import angelfont
'GameStates
Const VERSION_NUMBER:Float = 0.5
Const STATE_MENU:Int = 0
Const STATE_GAME:Int = 1
Const STATE_UPGRADES:Int = 2
Const STATE_SHOP:Int = 3
Class ClickingGame Extends App

'Defining variables
	Field font:AngelFont	
	'Product Image in Game Library
	Field shoe:Image
	Field rollerblades:Image
	Field scooter:Image
	Field bike:Image
	Field vespa:Image
	Field motorbike:Image
	Field tricycle:Image
	Field cheapcar:Image
	Field honda:Image
	Field mustang:Image
	Field image:Image

	'Shop Image Library
	Field america:Image
	'Product Placement
	Field productX:Float = 325.0
	Field productY:Float = 250.0
	
	'Upgrade Cost Location
	Field upgrcostX:Float = 320.0
	Field upgrcostY:Float = 400.0
	
	'Upgrade Placement
	Field shopX:Float = 320.0
	Field shopY:Float = 250.0
	'Shop Item bought
	Field currentupgradelevel:String = ("shoe")	

	
	'Maths Variables (Upgrades and Shops etc).
	
	'Rollerblade Variables
	Field rollerbladesupgradecost:Int = 100
	Field rollerbladesclick:Int
	
	
	'Scooter Variables
	Field scooterupgradecost:Int = 250
	Field scooterclick:Int
	
	
	'Bike Variables
	Field bikeupgradecost:Int = 625
	Field bikeclick:Int
	
	
	'Vespa Variables
	Field vespaupgradecost:Int = 1562
	Field vespaclick:Int
	
	
	'Motorbike Variables
	Field motorbikeupgradecost:Int = 3905
	Field motorbikeclick:Int
	
	
	'Tricycle Variables
	Field tricycleupgradecost:Int = 9762
	Field tricycleclick:Int
	
	
	'Cheap Car Variables
	Field cheapcarupgradecost:Int = 24405
	Field cheapcarclick:Int
	Field cheapcarupgrade:Bool
	
	
	'Honda Variables
	Field hondaupgradecost:Int = 61012
	Field hondaclick:Int
	
	
	'Mustang Variables
	Field mustangupgradecost:Int = 152530
	Field mustangclick:Int

	'Integer Variables (((((((((Consistent Numbers Throughout Code))))))))))
	Field mouseclicks:Int = 1
	Field clicktotal:Int = 1
	
	'Sound variables
	Field ding:Sound
	
	Field gameState:Int = STATE_MENU

	Method OnCreate()
		'Loading Gamestate
		gameState = STATE_MENU
		SetUpdateRate(60)
		
		'Loading Sounds
		ding = LoadSound("ding.wav")
		'Loading Images
		shoe = LoadImage("shoe.png", 1, Image.MidHandle)
		rollerblades = LoadImage("rollerblades.png", 1, Image.MidHandle)
		scooter = LoadImage("scooter.png", 1, Image.MidHandle)
		bike = LoadImage("bike.png", 1, Image.MidHandle)
		vespa = LoadImage("vespa.png", 1, Image.MidHandle)
		tricycle = LoadImage("tricycle.png", 1, Image.MidHandle)
		cheapcar = LoadImage("cheapcar.png", 1, Image.MidHandle)
		honda = LoadImage("honda.png", 1, Image.MidHandle)
		motorbike = LoadImage("motorbike.png", 1, Image.MidHandle)
		mustang = LoadImage("mustang.png", 1, Image.MidHandle)
		image = shoe
		
		'AngelFont
    	font = New AngelFont()
    	font.LoadFontXml("angel_verdana")
    	
    	font.LoadFontXml("pagedfont")	
	End
	
	
		
	Method OnUpdate()
		Select gameState
			Case STATE_MENU
			'Registering menu hotkeys'
				If KeyHit(KEY_ENTER)
					gameState = STATE_GAME
				End
			
			
			
			
			
			
			
			Case STATE_GAME
				'Implementing base upgrade system	
				If KeyHit(KEY_ESCAPE)
					gameState = STATE_MENU
				Else If KeyHit(KEY_U)
					gameState = STATE_UPGRADES
				Else If KeyHit(KEY_S)
					gameState = STATE_SHOP
				Else If MouseHit(MOUSE_LEFT)
					mouseclicks = mouseclicks + clicktotal
					PlaySound(ding,0, 0)
				Endif
			
			
			
			
			
			
			
			Case STATE_UPGRADES
				'Cycling Right Through the Products
				If KeyHit(KEY_RIGHT) And image = shoe
					image = rollerblades

				Elseif KeyHit(KEY_RIGHT) And image = rollerblades
					image = scooter
				ElseIf KeyHit(KEY_RIGHT) And image = scooter
					image = bike				
				ElseIf KeyHit(KEY_RIGHT) And image = bike
					image = vespa			
				ElseIf KeyHit(KEY_RIGHT) And image = vespa
					image = motorbike					
				ElseIf KeyHit(KEY_RIGHT) And image = motorbike
					image = tricycle
				ElseIf KeyHit(KEY_RIGHT) And image = tricycle
					image = cheapcar
				ElseIf KeyHit(KEY_RIGHT) And image = cheapcar
					image = honda
				ElseIf KeyHit(KEY_RIGHT) And image = honda
					image = mustang
				Endif
				
				'Cycling Left Through the Products
				If KeyHit(KEY_LEFT) And image = rollerblades
					image = shoe
				Elseif KeyHit(KEY_LEFT) And image = scooter
					image = rollerblades				
				Elseif KeyHit(KEY_LEFT) And image = bike
					image = scooter			
				Elseif KeyHit(KEY_LEFT) And image = vespa
					image = bike					
				Elseif KeyHit(KEY_LEFT) And image = motorbike
					image = vespa
				Elseif KeyHit(KEY_LEFT) And image = tricycle
					image = motorbike
				Elseif KeyHit(KEY_LEFT) And image = cheapcar
					image = tricycle
				Elseif KeyHit(KEY_LEFT) And image = honda
					image = cheapcar
				Elseif KeyHit(KEY_LEFT) And image = mustang
					image = honda
				Endif
				
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Rollerblades Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				If image = rollerblades And mouseclicks >= rollerbladesupgradecost And KeyHit(KEY_SPACE)
					currentupgradelevel = ("rollerblades")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - rollerbladesupgradecost
				
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Scooter Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
				Elseif image = scooter And mouseclicks >= scooterupgradecost And KeyHit(KEY_SPACE)
					currentupgradelevel = ("scooter")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - scooterupgradecost
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Bike Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
				Elseif image = bike And mouseclicks >= bikeupgradecost And KeyHit(KEY_SPACE)
					currentupgradelevel = ("bike")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - bikeupgradecost
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Vespa Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
				Elseif image = vespa And mouseclicks >= vespaupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("vespa")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - vespaupgradecost
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Motorbike Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''		
				Elseif image = motorbike And mouseclicks >= motorbikeupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("motorbike")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - motorbikeupgradecost
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Tricycle Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''		
				Elseif image = tricycle And mouseclicks >= tricycleupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("tricycle")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - tricycleupgradecost				
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Cheap Car Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''		
				Elseif	image = cheapcar And mouseclicks >= cheapcarupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("cheapcar")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - cheapcarupgradecost
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Honda Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
				Elseif image = honda And mouseclicks >= hondaupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("honda")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - hondaupgradecost
				
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				'Mustang Programming Upgrade'
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
				Elseif image = mustang And mouseclicks >= mustangupgradecost And KeyHit(KEY_SPACE) 
					currentupgradelevel = ("mustang")
					clicktotal = clicktotal*2
					mouseclicks = mouseclicks - mustangupgradecost	
					
				Endif					
				
				If KeyHit(KEY_ESCAPE)
					gameState = STATE_GAME
				Elseif KeyHit(KEY_S)
					gameState = STATE_SHOP
				Endif
				
			
			
			
			
			
			
			Case STATE_SHOP
				If KeyHit(KEY_ESCAPE)
				gameState = STATE_GAME
				Elseif KeyHit(KEY_U)
				gameState = STATE_UPGRADES
				Endif
			
		End	
	
	End	
	
	Method OnRender()
		Cls(0, 0, 0)
		
		
		Select gameState
		'Inserting graphics within game states
			
			
			Case STATE_MENU
				font.DrawText("Conquer the Market!", 320, 100, AngelFont.ALIGN_CENTER)
				DrawText(VERSION_NUMBER, 580, 450)
				PushMatrix()	
					Scale 0.75,0.75
					font.DrawText("Press Enter to Play!", 430, 500, AngelFont.ALIGN_CENTER)
				PopMatrix()
				
				
			
			
			
			
			Case STATE_GAME
				Cls(255,255,255)
				PushMatrix()
					SetColor(0,0,0)
					font.DrawText("$" +mouseclicks, 300, 50, AngelFont.ALIGN_CENTER)
					PushMatrix()
						Scale 0.9,0.9
						SetColor(0,0,0)
						font.DrawText("$ Per Click: $" +clicktotal, 340, 425, AngelFont.ALIGN_CENTER)
					PopMatrix()
				PopMatrix()
				SetColor(255,255,255)
				
				'Drawing Correct Upgrade to Screen
				''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				If currentupgradelevel = ("shoe")
					DrawImage(shoe, productX, productY, 0.5)
				Elseif currentupgradelevel = ("rollerblades")
					DrawImage(rollerblades, productX, productY)
				Elseif currentupgradelevel = ("scooter")
					DrawImage(scooter, productX, productY)
				Elseif currentupgradelevel = ("bike")
					DrawImage(bike, productX, productY)
				Elseif currentupgradelevel = ("vespa")
					DrawImage(vespa, productX, productY)
				Elseif currentupgradelevel = ("motorbike")
					DrawImage(motorbike, productX, productY)
				Elseif currentupgradelevel = ("tricycle")
					DrawImage(tricycle, productX, productY)
				Elseif currentupgradelevel = ("cheapcar")
					DrawImage(cheapcar, productX, productY)
				Elseif currentupgradelevel = ("honda")
					DrawImage(honda, productX, productY)
				Elseif currentupgradelevel = ("mustang")
					DrawImage(mustang, productX, productY)
				Endif
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			
			
			
			
			
			
			Case STATE_UPGRADES
				Cls(255,255,255)
				SetColor(0,0,0)
				font.DrawHTML("<b>Upgrades</b>", 300, 25, AngelFont.ALIGN_CENTER)
				font.DrawHTML("$" +mouseclicks, 300, 75, AngelFont.ALIGN_CENTER)
				SetColor(255,255,255)
				
				
				'Drawing Images to Upgrade Store
				'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				If image = shoe 
					DrawImage(shoe,shopX,shopY)
					SetColor(0,0,0)
					If currentupgradelevel = ("shoe") Or ("rollerblades") Or ("scooter") Or ("bike") Or ("vespa") Or ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
						PushMatrix()
							SetColor(255,0,0)
							font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
						PopMatrix()
					Endif
				
				
				'Rollerblades Code														
				Elseif image = rollerblades
					DrawImage(rollerblades,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= rollerbladesupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
						If currentupgradelevel = ("rollerblades") Or ("scooter") Or ("bike") Or ("vespa") Or ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
							PushMatrix()
								SetColor(255,0,0)
								font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
							PopMatrix()	
						Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ rollerbladesupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					Endif
				
				
				'Scooter Code
				Elseif image = scooter
					DrawImage(scooter,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= scooterupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("scooter") Or ("bike") Or ("vespa") Or ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ scooterupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					Endif
				'Bike Code
				
				
					
				Elseif image = bike
					DrawImage(bike,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= bikeupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("bike") Or ("vespa") Or ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ bikeupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
				
				'Vespa Code
				
				
					
				Elseif image = vespa
					DrawImage(vespa,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= vespaupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("vespa") Or ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ vespaupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
					
				
				'Motorbike Code
				
				
				Elseif image = motorbike
					DrawImage(motorbike,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= motorbikeupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("motorbike") Or ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ motorbikeupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
				
				
				'Tricycle Code	
				Elseif image = tricycle
					DrawImage(tricycle,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= tricycleupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("tricycle") Or ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ tricycleupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
				
				
				'Cheap Car Code	
				Elseif image = cheapcar
					DrawImage(cheapcar,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= cheapcarupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("cheapcar") Or ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ cheapcarupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
				
				'Honda Code	
				Elseif image = honda
					DrawImage(honda,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= hondaupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("honda") Or ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ hondaupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
				
				'Mustang Code
				
					
				Elseif image = mustang
					DrawImage(mustang,shopX,shopY)
					SetColor(0,0,0)
					If mouseclicks >= mustangupgradecost
						font.DrawText("Press Space to Purchase", 320, 350, AngelFont.ALIGN_CENTER)
							If currentupgradelevel = ("mustang")
								PushMatrix()
									SetColor(255,0,0)
									font.DrawText("$-Purchased-$",300,400,AngelFont.ALIGN_CENTER)
								PopMatrix()	
							Endif
					
					Else
						font.DrawText("Upgrade Cost:$"+ mustangupgradecost, upgrcostX, upgrcostY, AngelFont.ALIGN_CENTER)
					EndIf
					
					
				Endif
			''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
			
			
			
			Case STATE_SHOP
				Cls(255, 255, 255)
				SetColor(0,0,0)
				font.DrawHTML("<b>Shop</b>", 300, 50, AngelFont.ALIGN_CENTER)
				font.DrawText("$" +mouseclicks, 300, 100, AngelFont.ALIGN_CENTER)
				SetColor(255,255,255)
				
				
		End
	End
End

Function Main:Int()	
		 
	New ClickingGame()
End


The game runs well, however during the shop screen, if the user has enough or more than enough money to buy the upgrade, Purchased comes up on the screen although they have not yet bought the upgrade. After it is purchased the "$-Purchased-$" disappeared although this should remain on the screen and font.DrawText draws the upgrade cost again and I'm not sure why. If anyone could help this would be great, I also want to but don't know how to implement a way in which to make sure an upgrade can only be bought once.


Xaron(Posted 2017) [#2]
Could you post the complete project as a zip? Hard to debug without all those images. ;)


Gerry Quinn(Posted 2017) [#3]
Your Update() code looks okay, i.e. nothing should happen unless SPACE is pressed - I don't really understand what's happening in OnRender(). I suspect the error is here somewhere.


ewancordiner(Posted 2017) [#4]
Sorry, I have sorted this issue now, however I have ran into another issue which I will upload in a couple of minutes.