Platformer Python Code


import turtle, random,time
map = input('Select a map from 1-8 or press 9 for random map')
max = 8
map = int(map)
if map == max:
  map = random.randint(1,max-1)
if map == 1:
  mapName = "v"
if map == 2:
  mapName = "d"
if map == 3:
  mapName = "m"
if map == 4:
  mapName = "a"
if map == 5:
  mapName = "j"
if map == 6:
  mapName = "e (1)"
if map == 7:
  mapName = "t"
if map == 8:
  mapName = "l"

charlist = ['rainbowbob','Kataniguana','yurp','REEE','Zhane','america','lightning','Goku','Symmetra','Dva','Junkrat2','Mercy','soldier']
print(charlist)
char1 = int(input('Player 1 Choose a character from the list.'))
char2 = int(input('Player 2 Choose a character from the list.'))
char1 = charlist[char1-1]
char2 = charlist[char2-1]

you = turtle.Turtle()
you.penup()
them = turtle.Turtle()
them.penup()
ball = turtle.Turtle()
screen = turtle.Screen()
screen.setup(400, 400)
direction = "up"
screen.bgpic(mapName+".png")
screen.addshape(char1+".png")
you.shape(char1+".png")
screen.addshape(char2+".png")
them.shape(char2+".png")
move_speed = 10


##MOVEMENT CODE FOR SPRITE 1 (YOU)
def up():
  xold = you.xcor()
  yold = you.ycor()
  you.sety(you.ycor()+move_speed)
  if you.ycor()> 200:
    you.sety(-200)
  print(str(you.xcor())+','+str(you.ycor()))
  findbox(xold,yold)
def down():
  xold = you.xcor()
  yold = you.ycor()
  you.sety(you.ycor()-move_speed)
  if you.ycor()< -200:
    you.sety(200)
  print(str(you.xcor())+','+str(you.ycor()))
  findbox(xold,yold)
def left():
  xold = you.xcor()
  yold = you.ycor()
  you.setx(you.xcor()-move_speed)
  if you.xcor()< -200:
    you.setx(200)
  print(str(you.xcor())+','+str(you.ycor()))
  findbox(xold,yold)
def right():
  xold = you.xcor()
  yold = you.ycor()
  you.setx(you.xcor()+move_speed)
  if you.xcor()> 200:
    you.setx(-200)
  print(str(you.xcor())+','+str(you.ycor()))
  findbox(xold,yold)
def jump():
  up()
  up()
  up()
  time.sleep(0.1)
  down()
  down()
  down()
  

##MOVEMENT CODE FOR SPRITE 2 (THEM)
def up2():
  xold2 = them.xcor()
  yold2 = them.ycor()
  them.sety(them.ycor()+move_speed)
  if them.ycor()> 200:
    them.sety(-200)
  print(str(them.xcor())+','+str(them.ycor()))
  findbox(xold2,yold2)
def down2():
  xold2 = them.xcor()
  yold2 = them.ycor()
  them.sety(them.ycor()-move_speed)
  if them.ycor()< -200:
    them.sety(200)
  print(str(them.xcor())+','+str(them.ycor()))
  findbox(xold2,yold2)
def left2():
  xold2 = them.xcor()
  yold2 = them.ycor()
  them.setx(them.xcor()-move_speed)
  if them.xcor()< -200:
    them.setx(200)
  print(str(them.xcor())+','+str(them.ycor()))
  findbox(xold2,yold2)
def right2():
  xold2 = them.xcor()
  yold2 = them.ycor()
  them.setx(them.xcor()+move_speed)
  if them.xcor()> 200:
    them.setx(-200)
  print(str(them.xcor())+','+str(them.ycor()))
  findbox(xold2,yold2)
def jump2():
  up2()
  up2()
  up2()
  time.sleep(0.1)
  down2()
  down2()
  down2()
  
  
  
def findbox(xold, yold):
  if map == 1:
    coords = [
    [30,90,90,180],
    [10,120,30,70],
    [160,200,-10,70],
    [-160,-20,20,70],
    [-200,-180,20,80],
    [-80,-10,100,180],
    [-170,-120,100,200],
    [-190,-100,-80,-30],
    [-40,10,-90,-40]]
  if map == 7:
     coords = [
    [10,180,-110,-70],
    [10,180,-160,-120],
    [10,180,-180,-160],
    [-80,0,-180,-60],
    [-190,-90,-170,-60],
    [120,200,70,200],
    [20,70,80,190],
    [-160,-20,120,190],
    [-170,-120,70,100],
    [-120,-80,70,100],
    [-70,-30,70,100]]


  sprites = [you, them]
  for i in sprites:
    for j in coords:
      if i.xcor() > j[0] and i.xcor() < j[1]: 
        if i.ycor() > j[2] and i.ycor() < j[3]:
          print('you are in the box')
          i.setx(xold)
          i.sety(yold)
   
def fly(ball):
  global direction
  ball.hideturtle()
  ball.setx(you.xcor())
  ball.sety(you.ycor())
  ball.pendown()
  if direction == "up":
    ball.sety(200)
  if direction == "down":
    ball.sety(-200)
  if direction == "left":
    ball.setx(-200)
  if direction == "right":
    ball.setx(200)
  ball.penup()
  ball.showturtle()

you.penup()
you.speed(0)
you.home()
you.left(90)

them.penup()
them.speed(0)
them.home()
them.left(90)

screen.onkey(up, "Up")
screen.onkey(down, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.onkey(jump,"Space")

screen.onkey(up2, "w")
screen.onkey(down2, "s")
screen.onkey(left2, "a")
screen.onkey(right2, "d")
screen.onkey(jump2,"x")
screen.listen()

GPS Code for USB Receiver

import serial
gpsPort = "/dev/ttyACM0"
gpsSerial = serial.Serial(gpsPort, baudrate = 9600, timeout = 0.5)

def parseGPS(data):
    gps = data

    try:        
        if gps[2:8] == "$GNGGA":
            gps = gps.split(",")

            timeHour = (int(gps[1][0:2]) - 4) % 24
            timeMin = int(gps[1][2:4])
            timeSec = int(gps[1][4:6])
            print("Time: " + str(timeHour) + ":" + str(timeMin) + ":" + str(timeSec))

            latDeg = int(gps[2][0:2])
            latMin = int(gps[2][2:4])
            latSec = float(gps[2][5:9]) * (3/500)
            latNS = gps[3]
            print("Latitude:  " + str(latDeg) + "°" + str(latMin) + "'" + str(latSec) + '" ' + latNS)

            longDeg = int(gps[4][0:3])
            longMin = int(gps[4][3:5])
            longSec = float(gps[4][6:10]) * (3/500)
            longEW = gps[5]
            print("Longitude:  " + str(longDeg) + "°" + str(longMin) + "'" + str(longSec) + '" ' + longEW)

            alt = float(gps[9])
            print("Altitude: " + str(alt) + " m")

            sat = int(gps[7])
            print("Satellites: " + str(sat))
        if gps[2:8] == "$GNRMC":
            gps = gps.split(",")

            speed = float(gps[7]) * 1.852
            print("Speed: " + str(speed) + " km/h")
            
            head = float(gps[8])
            print("Heading: " + str(head))
        else:
            gps = ""
    except Exception as error:
        print(error)

    return gps
while True:
    print(parseGPS(gpsSerial.readline()))