-----------------------------------------------------------------------------
-- Cargame Version 0.2 by Nick Kochurov (dalejrrocks)
-- E-mail: dalejrrocks@rambler.ru or nascar_sk8ing_technology@yahoo.com
-----------------------------------------------------------------------------
--colors
black     = Color.new(0, 0, 0)
grey      = Color.new(94,97,111)
orange    = Color.new(223,88,6)
blue      = Color.new(0,0,255)
orangeb = Color.new(255,100,0)
green=Color.new(0,255,0)
white     = Color.new(255,255,255)
screen:clear()
screen:print(220,135,"Loading",white)
screen:print(275,135,".",green)
screen:flip()
screen:waitVblankStart()
dofile("settings.txt")
screen:clear()
screen:print(220,135,"Loading",white)
screen:print(275,135,"..",green)
screen:flip()
screen:waitVblankStart()
--load pictures
screen:clear()
screen:print(220,135,"Loading...",white)
screen:print(275,135,"...",green)
p1w=Image.load("images/win/wina.png")
p2w=Image.load("images/win/winb.png")
pole = Image.load("images/race start/pole.png")
redlight = Image.load("images/race start/redlight.png")
greenlight=Image.load("images/race start/greenlight.png")
yellowlight=Image.load("images/race start/yellowlight.png")
upblue=Image.load("images/gears/upblue.png")
upred=Image.load("images/gears/upred.png")
screen:clear()
screen:print(220,135,"Loading",white)
screen:print(275,135,"....",green)
screen:flip()
screen:waitVblankStart()
tiles = Image.load("images/tiles/coolmovespace.png")
g1=Image.load("images/gears/1.png")
g2g=Image.load("images/gears/2.png")
g3=Image.load("images/gears/3.png")
g4=Image.load("images/gears/4.png")
g5=Image.load("images/gears/5.png")
g6=Image.load("images/gears/6.png")
g7=Image.load("images/gears/7.png")
gn=Image.load("images/gears/neutral.png")
screen:clear()
screen:print(220,135,"Loading Done!",white)
screen:flip()
screen:waitVblankStart()
--draw the map limit area 
maplimitmin_x = 1
maplimitmax_x = 15
maplimitmin_y = 1
maplimitmax_y = 35
--draw current show veiw limit
current_drawviewmin_x = 0
current_drawviewmin_y = 0
current_drawviewmax_x = 0
current_drawviewmax_y = 0

--border --offsrceen black
--does not go off too much
mapbordermin_x = 0
mapbordermax_x = 0
mapbordermin_y = 0
mapbordermax_y = 0

--read controls
oldpad = Controls.read()
--variables
sc0re1=0
sc0re2=0
--maps
mapSource1 = {  
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaabaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaaaaaaaaac",
  "caaaaaabaaaaaac"   
}
  
mapSource2 = {  
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaaaaaaaaae"
  
}

mapSource3 = {  
  "eaaaaaaaaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaaaaaaaaaf",
  "eaaaaaaaaaaaaae"

  }

mapSource4 = {  
  "eaaaaaaaaaaaaae",
  "caaaaacbaaaaaaf",
  "ceaaaccaaaaaaae",
  "caaaaccaaaaaaaf",
  "eaaaaccbaaaaaae",
  "caaaaccbaaaaaaf",
  "ccaaaaaaaaaaaae",
  "cccaaaaaaaaaaaf",
  "ccaaaaabaaaaaae",
  "eaaabaaaaaaaaaf",
  "caaaaaaaaaaaaae",
  "ccaaaaaaaaaaaaf",
  "cccaaaabaaaaaae",
  "cccaaaabaaaaaaf",
  "cccaaaaaaaaaaae",
  "ccaaaaaaaaaaaaf",
  "caaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "caaaaacbaaaaaaf",
  "ceaaaccaaaaaaae",
  "caaaaccaaaaaaaf",
  "eaaaaccbaaaaaae",
  "caaaaccbaaaaaaf",
  "ccaaaaaaaaaaaae",
  "cccaaaaaaaaaaaf",
  "ccaaaaabaaaaaae",
  "eaaabaaaaaaaaaf",
  "caaaaaaaaaaaaae",
  "ccaaaaaaaaaaaaf",
  "cccaaaabaaaaaae",
  "cccaaaabaaaaaaf",
  "cccaaaaaaaaaaae",
  "ccaaaaaaaaaaaaf",
  "caaaaaaaaaaaaae"
    
}

mapSource5 ={
  "aaaaaaeeaaaaaaa",
  "aaaaafbcaaaaaac",
  "aaaaaaeceaaaacc",
  "aaaaaafcaaaacac",
  "eaaaaabeaaaaacc",
  "faaaaabcaaaaacc",
  "aaaaaaeccaaaaaa",
  "faaaaabcccaaaaa",
  "eaaaaabccaaaaaa",
  "aaaaaafeaaabaaa",
  "aaaaaaecaaaaaaa",
  "aaaaaafccaaaaaa",
  "eaaaaabcccaaaaa",
  "faaaaabcccaaaaa",
  "aaaaaaecccaaaaa",
  "aaaaaafccaaaaaa",
  "aaaaaaecaaaaaaa",
  "aaaaaaeeaaaaaaa",
  "aaaaafbcaaaaaac",
  "aaaaaaeceaaaacc",
  "aaaaaafcaaaacac",
  "eaaaaabeaaaaacc",
  "faaaaabcaaaaacc",
  "aaaaaaeccaaaaaa",
  "faaaaabcccaaaaa",
  "eaaaaabccaaaaaa",
  "aaaaaafeaaabaaa",
  "aaaaaaecaaaaaaa",
  "aaaaaafccaaaaaa",
  "eaaaaabcccaaaaa",
  "faaaaabcccaaaaa",
  "aaaaaaecccaaaaa",
  "aaaaaafccaaaaaa",
  "aaaaaaecaaaaaaa"
    
}
mapSource6 ={
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "caaaaaaaaaaaaac",
  "eaaaaaaaaaaaaae"
  
}
mapSource7 ={
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaac",
  "eaaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "caaaaaabaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaac",
  "eaaaaaaaaaaaaac",
  "eaaaaaabaaaaaae",
  "caaaaaabaaaaaac",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae",
  "eaaaaaaaaaaaaae"
    
}
mapSource8 ={
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaafaaaaaaf"
  
}
mapSource9 ={
  "caaaaaafaaaaaac",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaafaaaaaaf",
  "eaaaaaabaaaaaac",
  "caaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaafaaaaaaf",
  "caaaaaabaaaaaac",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaac",
  "faaaaaafaaaaaaf",
  "caaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaac",
  "faaaaaafaaaaaaf",
  "caaaaaafaaaaaae",
  "caaaaaafaaaaaac",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaafaaaaaaf",
  "eaaaaaabaaaaaac",
  "caaaaaabaaaaaaf",
  "eaaaaaaaaaaaaae",
  "faaaaaafaaaaaaf",
  "caaaaaabaaaaaac",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaac",
  "faaaaaafaaaaaaf",
  "caaaaaabaaaaaae",
  "faaaaaabaaaaaaf",
  "eaaaaaaaaaaaaac",
  "faaaaaafaaaaaaf",
  "caaaaaafaaaaaae"
  
}
mapSource10 ={
  "ddddddddddddddd",
  "ddddddddddddddd",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "ddddddddddddddd",
  "ddddddddddddddd",
  "ddddddddddddddd",
  "ddddddddddddddd",
  "ddddddddddddddd",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaabaaaaaaf",
  "faaaaaaaaaaaaaf",
  "faaaaaafaaaaaaf",
  "faaaaaafaaaaaaf"
    
}
  
--some more variables to be used during the start and the race
ay=0
ay2=0
movemap_x = 0
movemap_y = 25
shifted=-2
shifted2=-2
scroll = false
--starts the race and keeps players from moving until the light is green
starttimer=Timer.new()--real timer yay!
starttimer:reset()
secondtimer = false
--starts the race and keeps players from moving until the light is green
function start()
if starttimer:time() >= pole1 and secondtimer == false then
screen:blit(240,193,pole)
starttimer:start()
ay=0
ay2=0
shifted=-2
shifted2=-2
scroll = false
end
if starttimer:time() >= red1 and starttimer:time() < totaltime then
screen:blit(225,195,redlight)
screen:blit(250,195,redlight)
ay=0
ay2=0
shifted=-2
shifted2=-2
scroll = false
end
if starttimer:time() >= yellow1 and starttimer:time() < totaltime then
screen:blit(225,210,yellowlight)
screen:blit(250,210,yellowlight)
ay=0
ay2=0
shifted=-2
shifted2=-2
scroll = false
end
if starttimer:time() >= yellow2 and starttimer:time() < totaltime then
screen:blit(225,225,yellowlight)
screen:blit(250,225,yellowlight)
ay=0
ay2=0
shifted=-2
shifted2=-2
scroll = false
end

if starttimer:time() >= green1 and starttimer:time() < totaltime then
screen:blit(250,245,greenlight)
screen:blit(225,245,greenlight)
shifted=-1
shifted2=-1
ay=0
ay2=0
scroll = true
end
if starttimer:time() >= totaltime then
starttimer:stop()
starttimer:reset()
secondtimer = true
scroll = true
end
end
lastpic=lastpicu
lastpic2=lastpic2u
--player controls function
currentrpms=0
currentrpms2=0
notdonereading = true
function controls()
dofile("controlsettings.txt")
rpma1 = false
rpma2 = false
screen:blit(pxx,pxy,lastpic)
if p1c and oldp1c ~= p1c then 
pxy=pxy-ay
screen:blit(pxx,pxy,lastpicu)
lastpic= lastpicu
rpma1 = true
currentrpms=currentrpms+gasrpm
end
if p12c and oldp12c ~= p12c then
pxy=pxy-ay
screen:blit(pxx,pxy,lastpicu)
rpma1 = true
currentrpms=currentrpms+gasrpm
lastpic= lastpicu
end
screen:blit(pxx2,pxy2,lastpic2)
if p21c and oldp21c ~= p21c then 
pxy2=pxy2-ay2
screen:blit(pxx2,pxy2,lastpic2u)
rpma2 = true
lastpic2 = lastpic2u
currentrpms2=currentrpms2+gasrpm
end
if p22c and oldp22c ~= p22c then
pxy2=pxy2-ay2
screen:blit(pxx2,pxy2,lastpic2u)
rpma2 = true
currentrpms2=currentrpms2+gasrpm
lastpic2 = lastpic2u
end
end
--controls players` shifting and tells players when to shift 
function rpmshift()
if currentrpms == neutralrpm and g1ss and oldg1ss ~= g1ss then
shifted=-1
end
if currentrpms > neutralrpm and currentrpms < fgrpm and g1ss and oldg1ss ~= g1ss then
shifted=0
end
if currentrpms >= fgrpm and currentrpms < sgrpm and g1ss and oldg1ss ~= g1ss then
shifted=1
end
if currentrpms >= sgrpm and currentrpms < tgrpm and g1ss and oldg1ss ~= g1ss then
shifted=2
end
if currentrpms >= tgrpm and  currentrpms < fogrpm and g1ss and oldg1ss ~= g1ss then
shifted=3
end
if  currentrpms >= fogrpm and currentrpms < figrpm and g1ss and oldg1ss ~= g1ss then
shifted=4
end
if  currentrpms >= figrpm and currentrpms < sigrpm and g1ss and oldg1ss ~= g1ss then
shifted=5
end
if  currentrpms >= sigrpm  and currentrpms < redlinerpm and g1ss and oldg1ss ~= g1ss then
shifted=6
end
if currentrpms == neutralrpm then
screen:blit(450,30,upblue)
end
if currentrpms > neutralrpm and currentrpms < fgrpm and shifted == -1 then
screen:blit(450,30,upblue)
end
if currentrpms >= fgrpm and currentrpms < sgrpm and shifted == 0 then
screen:blit(450,30,upblue)
end
if currentrpms >= sgrpm and currentrpms < tgrpm and shifted == 1 then
screen:blit(450,30,upblue)
end
if currentrpms >= tgrpm and currentrpms < fogrpm and shifted == 2 then
screen:blit(450,30,upblue)
end
if currentrpms >= fogrpm and  currentrpms < figrpm and shifted == 3 then
screen:blit(450,30,upblue)
end
if currentrpms >= figrpm and currentrpms < sigrpm and shifted == 4 then
screen:blit(450,30,upblue)
end
if  currentrpms >= sigrpm and shifted == 5 then
screen:blit(450,30,upblue)
end
if currentrpms2 == neutralrpm and g2ss and oldg2ss ~= g2ss then
shifted2=-1
end
if currentrpms2 > neutralrpm and currentrpms2 < fgrpm and g2ss and oldg2ss ~= g2ss then
shifted2=0
end
if currentrpms2 >= fgrpm and currentrpms2 < sgrpm and g2ss and oldg2ss ~= g2ss then
shifted2=1
end
if currentrpms2 >= sgrpm and currentrpms2 < tgrpm and g2ss and oldg2ss ~= g2ss then
shifted2=2
end
if currentrpms2 >= tgrpm and currentrpms2 < fogrpm and g2ss and oldg2ss ~= g2ss then
shifted2=3
end
if currentrpms2 >= fogrpm and  currentrpms2 < figrpm and g2ss and oldg2ss ~= g2ss then
shifted2=4
end
if  currentrpms2 >= figrpm and currentrpms2 < sigrpm and g2ss and oldg2ss ~= g2ss then
shifted2=5
end
if  currentrpms2 >= sigrpm and currentrpms2 < redlinerpm and g2ss and oldg2ss ~= g2ss then
shifted2=6
end
if currentrpms2 == 0 then
screen:blit(0,30,upred)
end
if currentrpms2 > neutralrpm and currentrpms2 < fgrpm and shifted2 == -1 then
screen:blit(0,30,upred)
end
if currentrpms2 >= fgrpm and currentrpms2 < sgrpm and shifted2 == 0 then
screen:blit(0,30,upred)
end
if currentrpms2 >= sgrpm and currentrpms2 < tgrpm and shifted2 == 1 then
screen:blit(0,30,upred)
end
if currentrpms2 >= tgrpm and currentrpms2 < fogrpm and shifted2 == 2 then
screen:blit(0,30,upred)
end
if currentrpms2 >= fogrpm and  currentrpms2 < figrpm and shifted2 == 3 then
screen:blit(0,30,upred)
end
if currentrpms2 >= figrpm and currentrpms2 < sigrpm and shifted2 == 4 then
screen:blit(0,30,upred)
end
if  currentrpms2 >= sigrpm and shifted2 == 5 then
screen:blit(0,30,upred)
end
end
--accelerates the players and displays current gear
--rpm
function rpma()
if rpma1 == true and shifted == -1 then
ay=ay+0.5
end
if rpma1 == true and shifted == 0 then
ay=ay+fg
end
if rpma1 == true and shifted == 1 then
ay=ay+sg
end
if rpma1 == true and shifted == 2 then
ay=ay+tg
end
if rpma1 == true and shifted == 3 then
ay=ay+fog
end
if rpma1 == true and shifted == 4 then
ay=ay+fig
end
if rpma1 == true and shifted == 5 then
ay=ay+sig
end
if rpma1 == true and shifted == 6 then
ay=ay+seg
end
if currentrpms == neutralrpm and shifted == -1 then
screen:blit(445,10,gn)
end
if currentrpms > neutralrpm and currentrpms < fgrpm and shifted == 0 then
screen:blit(445,10,g1)
end
if currentrpms >= fgrpm and currentrpms < sgrpm and shifted == 1 then 
screen:blit(445,10,g2g)
end
if currentrpms >= sgrpm and currentrpms < tgrpm and shifted == 2  then
screen:blit(445,10,g3)
end
if currentrpms >= tgrpm and currentrpms < fogrpm and shifted == 3 then
screen:blit(445,10,g4)
end
if currentrpms >= fogrpm and  currentrpms < figrpm and shifted == 4 then
screen:blit(445,10,g5)
end
if currentrpms >= figrpm and currentrpms < sigrpm and shifted == 5 then
screen:blit(445,10,g6)
end
if currentrpms >= sigrpm and shifted == 6  then
screen:blit(445,10,g7)
end

if rpma2 == true and shifted2 == -1 then
ay2=ay2+0.5
end
if rpma2 == true and shifted2 == 0 then
ay2=ay2+fg
end
if rpma2 == true and shifted2 == 1 then
ay2=ay2+sg
end
if rpma2 == true and shifted2 == 2 then
ay2=ay2+tg
end
if rpma2 == true and shifted2 == 3 then
ay2=ay2+fog
end
if rpma2 == true and shifted2 == 4 then
ay2=ay2+fig
end
if rpma2 == true and shifted2 == 5 then
ay2=ay2+sig
end
if rpma2 == true and shifted2 == 6 then
ay2=ay2+seg
end
if currentrpms2 == neutralrpm and shifted2 == -1 then
screen:blit(0,10,gn)
end
if currentrpms2 > neutralrpm and currentrpms2 < fgrpm and shifted2 == 0 then
screen:blit(0,10,g1)
end
if currentrpms2 >= fgrpm and currentrpms2 < sgrpm and shifted2 == 1 then 
screen:blit(0,10,g2g)
end
if currentrpms2 >= sgrpm and currentrpms2 < tgrpm and shifted2 == 2  then
screen:blit(0,10,g3)
end
if currentrpms2 >= tgrpm and currentrpms2 < fogrpm and shifted2 == 3 then
screen:blit(0,10,g4)
end
if currentrpms2 >= fogrpm and  currentrpms2 < figrpm and shifted2 == 4 then
screen:blit(0,10,g5)
end
if currentrpms2 >= figrpm and currentrpms2 < sigrpm and shifted2 == 5 then
screen:blit(0,10,g6)
end
if currentrpms2 >= sigrpm and shifted2 == 6  then
screen:blit(0,10,g7)
end
end

--check map borders
function checkborders()
current_drawviewmin_y = 1 + movemap_y
if current_drawviewmin_y <= maplimitmin_y then
   current_drawviewmin_y = maplimitmin_y
end
  
current_drawviewmax_y = movemap_y + 9
if current_drawviewmax_y >= maplimitmax_y then
   current_drawviewmax_y = maplimitmax_y
   end
   
current_drawviewmin_x = 1 + movemap_x
if current_drawviewmin_x <= maplimitmin_x then
   current_drawviewmin_x = maplimitmin_x
   end

current_drawviewmax_x = movemap_x + 15
if current_drawviewmax_x >= maplimitmax_x then
   current_drawviewmax_x = maplimitmax_x
   end
end

-- draw one tile on the map
function drawTile(tile, x, y)
  local tileX = math.mod(tile, 2)
  local tileY = math.floor(tile / 2)
  screen:blit(33 * x, 33 * y, tiles, 34 * tileX + 1, 34 * tileY + 1, 33, 33, false)
end

--current (first) map
map=mapSource1
--draw the map
function drawtiles()
for y = current_drawviewmin_y, current_drawviewmax_y do
    line = map[y]
    for x = current_drawviewmin_x, current_drawviewmax_x do
        tile = string.byte(line, x) - string.byte("a")
        drawTile(tile, x - movemap_x - 1, y - movemap_y -1)
end
  end
  end
--chooses and draws the map based on scores
function choosemap()
if sc0re1 == 0 or sc0re2 == 0 then
drawtiles()
end
if sc0re1 == 1 or sc0re2 == 1 then
map=mapSource2
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re1== 2 or sc0re2 == 2 then
map=mapSource3
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2== 3 or sc0re1 == 3 then
map=mapSource4
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 4 or sc0re1 == 4 then
map=mapSource5
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2== 5 or sc0re1 == 5 then
map=mapSource5
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 6 or sc0re1 == 6 then
map=mapSource6
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 7 or sc0re1 == 7 then
map=mapSource7
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 8 or sc0re1 == 8 then
map=mapSource8
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 9 or sc0re1 == 9 then
map=mapSource9
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 10 or sc0re1 == 10 then
map=mapSource1
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 11 or sc0re1 == 11 then
map=mapSource3
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 12 or sc0re1 == 12 then
map=mapSource8
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 13 or sc0re1 == 13 then
map=mapSource4
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 14 or sc0re1 == 14 then
map=mapSource6
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 15 or sc0re1 == 15 then
map=mapSource10
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 16 or sc0re1 == 16 then
map=mapSource5
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 17 or sc0re1 == 17 then
map=mapSource2
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 18 or sc0re1 == 18 then
map=mapSource1
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
if sc0re2 == 19 or sc0re1 == 19 then
map=mapSource10
drawtiles()
if sc0re1 > sc0re2 or sc0re2 > sc0re1 then
Image:fillRect(243,0,7,272,orangeb)
if sc0re1 > sc0re2 then
screen:print(244,260,sc0re1-sc0re2,white)
Image:drawLine(243,250,249,250,white)
Image:drawLine(249,250,245,246,white)
Image:drawLine(249,250,245,254,white)
end
if sc0re2 > sc0re1 then
screen:print(244,260,sc0re2-sc0re1,white)
Image:drawLine(249,250,243,250,white)
Image:drawLine(243,250,247,246,white)
Image:drawLine(243,250,247,254,white)
end
end
end
end
--scrolls map
function scrollcontrols()
if p12c and oldp12c ~= p12c or p1c and oldp1c ~= p1c  then
if scroll == true and  shifted2 ~= -1 and  shifted2 ~= -2 and  shifted ~= -1 and  shifted ~= -2 then   
movemap_y = movemap_y - 1
end
 if movemap_y <= mapbordermin_y then
             movemap_y = mapbordermin_y
             end  
             if  pxy <= 0 then
             movemap_y = 25
             pxx=336
             pxy=238
sc0re1=sc0re1+1             
end
   end
if p22c and oldp22c ~= p22c or p21c and oldp21c ~= p21c  then
if scroll == true and  shifted ~= -1 and  shifted ~= -2 and  shifted2 ~= -1 and  shifted2 ~= -2 then      
movemap_y = movemap_y - 1
end
if movemap_y <= mapbordermin_y then
             movemap_y = mapbordermin_y
          end      
          if  pxy2 <= 0 then
             movemap_y = 25
             pxx2=122
             pxy2=238
sc0re2=sc0re2+1             
end
end
end
--tells who won
function whowon()
if sc0re1 - sc0re2 == winmargin then
ay=0
shifted=-2
ay2=0
shifted2=-2
sc0re1=0
sc0re2=0
blowitup=lastpic2u
dofile("win.lua")
end
if sc0re2 - sc0re1 == winmargin then
ay=0
shifted=-2
ay2=0
shifted2=-2
sc0re1=0
sc0re2=0
blowitup=lastpicu
dofile("win.lua")
end
if sc0re1 == tsc0re then
ay=0
shifted=-2
ay2=0
shifted2=-2
sc0re1=0
sc0re2=0
blowitup=lastpic2u
dofile("win.lua")
end
if sc0re2 == tsc0re then
ay=0
shifted=-2
ay2=0
shifted2=-2
sc0re1=0
sc0re2=0
blowitup=lastpicu
dofile("win.lua")
end
end
--main loop
while true do
--read controls
pad = Controls.read()
checkborders()
choosemap()
scrollcontrols()
start()
controls()
rpmshift()
rpma()
whowon()
--exit to Lowser/Lua Os
if pad:start() then 
break
end
--show scores
screen:print(393,0,"Player 1:"..sc0re1,blue)
screen:print(0,0,"Player 2:"..sc0re2,blue)
screen:flip()
screen:waitVblankStart()
oldpad=pad
end --end of code:1266