Skip to content

Instantly share code, notes, and snippets.

@CoolGuyBraydan
Last active April 9, 2024 20:58
Show Gist options
  • Save CoolGuyBraydan/a2ebfc8b14a976cb3c902ea2bafe8714 to your computer and use it in GitHub Desktop.
Save CoolGuyBraydan/a2ebfc8b14a976cb3c902ea2bafe8714 to your computer and use it in GitHub Desktop.
this game i made is like a cross between slither.io and snake, and its 2 players, hope you enjoy.
import turtle
import time
import random
blue_segments=[]
red_segments=[]
blgame="stop"
rdgame="stop"
blue_wait="nothing"
red_wait="nothing"
yvalues=[0,-20,-40,-60,-80,-100,-120,-140,-160,-180,-200,-220,-240,-260,-280,20,40,60,80,100,120,140,160,180,200,220,240,260,280]
xvalues=[-20,-40,-60,-80,-100,-120,-140,-160,-180,-200,-220,-240,-260,-280,-300,-320,-340,-360,-380,-400,-420,-440,-460,-480,-500,-520,-540,-560,-580,0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,520,540,560,580]
delay=0.05
redscore=0
bluescore=0
print("Left side: Blue, wasd keys. Right side: Red, arrow keys.")
bluename=input("Player 1 (left side) username: ")
redname=input("Player 2 (right side) username: ")
wn=turtle.Screen()
wn.title("Snake Battle")
wn.setup(width=1200,height=600)
wn.tracer(0)
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.setposition(-600, -300)
pen.pendown()
pen.pensize(3)
for side in range(2):
pen.fd(1200)
pen.lt(90)
pen.fd(600)
pen.lt(90)
pen.penup()
pen.ht()
scorepen=turtle.Turtle()
scorepen.ht()
scorepen.penup()
scorepen.goto(0,250)
headpen=turtle.Turtle()
headpen.ht()
headpen.penup()
apple=turtle.Turtle()
apple.speed(0)
apple.shape("circle")
apple.color("red")
apple.penup()
x=random.choice(xvalues)
y=random.choice(yvalues)
apple.goto(x,y)
bl_h=turtle.Turtle() #bl_h=blue head
bl_h.shape("circle")
bl_h.color("blue")
bl_h.penup()
bl_h.goto(-660,0)
bl_h.direction="right"
rd_h=bl_h.clone()
rd_h.color("red")
rd_h.goto(660,0)
rd_h.direction="left"
def bl_up():
global xvalues
global blue_wait
if bl_h.direction != "down" and blgame == "start":
if(bl_h.xcor() in xvalues):
bl_h.direction = "up"
elif(bl_h.xcor() not in xvalues):
blue_wait="up"
def bl_down():
global xvalues
global blue_wait
if bl_h.direction != "up" and blgame == "start":
if(bl_h.xcor() in xvalues):
bl_h.direction = "down"
elif(bl_h.xcor() not in xvalues):
blue_wait="down"
def bl_left():
global yvalues
global blue_wait
if bl_h.direction != "right" and blgame == "start":
if(bl_h.ycor() in yvalues):
bl_h.direction = "left"
elif(bl_h.ycor() not in yvalues):
blue_wait="left"
def bl_right():
global yvalues
global blue_wait
if bl_h.direction != "left" and blgame == "start":
if(bl_h.ycor() in yvalues):
bl_h.direction = "right"
elif(bl_h.ycor() not in yvalues):
blue_wait="right"
def rd_up():
global xvalues
global red_wait
if rd_h.direction != "down" and rdgame == "start":
if(rd_h.xcor() in xvalues):
rd_h.direction = "up"
elif(rd_h.xcor() not in xvalues):
red_wait="up"
def rd_down():
global xvalues
global red_wait
if rd_h.direction != "up" and rdgame == "start":
if(rd_h.xcor() in xvalues):
rd_h.direction = "down"
elif(rd_h.xcor() not in xvalues):
red_wait="down"
def rd_left():
global yvalues
global red_wait
if rd_h.direction != "right" and rdgame == "start":
if(rd_h.ycor() in yvalues):
rd_h.direction = "left"
elif(rd_h.ycor() not in yvalues):
red_wait="left"
def rd_right():
global yvalues
global red_wait
if rd_h.direction != "left" and rdgame == "start":
if(rd_h.ycor() in yvalues):
rd_h.direction = "right"
elif(rd_h.ycor() not in yvalues):
red_wait="right"
def move():
global blue_wait
global red_wait
global xvalues
global yvalues
if(bl_h.xcor() in xvalues):
if blue_wait=="up":
bl_h.direction="up"
blue_wait="nothing"
if blue_wait=="down":
bl_h.direction="down"
blue_wait="nothing"
if(bl_h.ycor() in yvalues):
if blue_wait=="left":
bl_h.direction="left"
blue_wait="nothing"
if blue_wait=="right":
bl_h.direction="right"
blue_wait="nothing"
if(rd_h.xcor() in xvalues):
if red_wait=="up":
rd_h.direction="up"
red_wait="nothing"
if red_wait=="down":
rd_h.direction="down"
red_wait="nothing"
if(rd_h.ycor() in yvalues):
if red_wait=="left":
rd_h.direction="left"
red_wait="nothing"
if red_wait=="right":
rd_h.direction="right"
red_wait="nothing"
if bl_h.direction == "up":
y = bl_h.ycor()
bl_h.sety(y+10)
if bl_h.direction == "down":
y = bl_h.ycor()
bl_h.sety(y-10)
if bl_h.direction == "left":
x = bl_h.xcor()
bl_h.setx(x-10)
if bl_h.direction == "right":
x = bl_h.xcor()
bl_h.setx(x+10)
if rd_h.direction == "up":
y = rd_h.ycor()
rd_h.sety(y+10)
if rd_h.direction == "down":
y = rd_h.ycor()
rd_h.sety(y-10)
if rd_h.direction == "left":
x = rd_h.xcor()
rd_h.setx(x-10)
if rd_h.direction == "right":
x = rd_h.xcor()
rd_h.setx(x+10)
wn.listen()
wn.onkeypress(bl_up, "w")
wn.onkeypress(bl_down, "s")
wn.onkeypress(bl_left, "a")
wn.onkeypress(bl_right, "d")
wn.onkeypress(rd_up, "Up")
wn.onkeypress(rd_down, "Down")
wn.onkeypress(rd_left, "Left")
wn.onkeypress(rd_right, "Right")
while True:
wn.update()
headpen.clear()
for index in range(len(blue_segments)-1, 0, -1):
x = blue_segments[index-1].xcor()
y = blue_segments[index-1].ycor()
blue_segments[index].goto(x,y)
if len(blue_segments) > 0:
x = bl_h.xcor()
y = bl_h.ycor()
blue_segments[0].goto(x,y)
if bl_h.xcor()==-650:
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
if bl_h.xcor()==-640:
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
if bl_h.xcor()==-630:
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
if bl_h.xcor()==-620:
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
if bl_h.distance(apple)<19:
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
if bl_h.distance(apple)<9:
x=random.choice(xvalues)
y=random.choice(yvalues)
apple.goto(x,y)
bl_s=turtle.Turtle()
bl_s.shape("circle")
bl_s.color("blue")
bl_s.speed(0)
bl_s.penup()
blue_segments.append(bl_s)
for index in range(len(red_segments)-1, 0, -1):
x = red_segments[index-1].xcor()
y = red_segments[index-1].ycor()
red_segments[index].goto(x,y)
if len(red_segments) > 0:
x = rd_h.xcor()
y = rd_h.ycor()
red_segments[0].goto(x,y)
if rd_h.xcor()==650:
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
if rd_h.xcor()==640:
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
if rd_h.xcor()==630:
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
if rd_h.xcor()==620:
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
if rd_h.distance(apple)<19:
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
if rd_h.distance(apple)<9:
x=random.choice(xvalues)
y=random.choice(yvalues)
apple.goto(x,y)
rd_s=turtle.Turtle()
rd_s.shape("circle")
rd_s.color("red")
rd_s.speed(0)
rd_s.penup()
red_segments.append(rd_s)
bx=bl_h.xcor()
by=bl_h.ycor()
rx=rd_h.xcor()
ry=rd_h.ycor()
if bx<-590:
blgame="stop"
bl_h.direction="right"
if bx>590:
blgame="stop"
bl_h.direction="left"
if by<-290:
blgame="stop"
bl_h.direction="up"
if by>290:
blgame="stop"
bl_h.direction="down"
if rx<-590:
rdgame="stop"
rd_h.direction="right"
if rx>590:
rdgame="stop"
rd_h.direction="left"
if ry<-290:
rdgame="stop"
rd_h.direction="up"
if ry>290:
rdgame="stop"
rd_h.direction="down"
if bx>-591 and bx<591 and by>-291 and by<291 and rx>-591 and rx<591 and ry>-291 and ry<291:
blgame="start"
rdgame="start"
for bl_s in blue_segments:
if (bl_s.distance(rd_h)<10) and (rd_h.xcor()!=0 or rd_h.ycor()!=0) and ((rd_h.distance(bl_h)>19) or (bl_h.distance(rd_h)>19)):
scorepen.pendown()
scorepen.write("{} Wins!".format(bluename),align="center",font=("candara",24,"bold"))
wn.update()
time.sleep(1)
scorepen.clear()
bl_h.setposition(-720,0)
rd_h.setposition(720,0)
bl_h.direction="right"
rd_h.direction="left"
blgame="stop"
rdgame="stop"
for bl_s in blue_segments:
bl_s.goto(1000,1000)
for rd_s in red_segments:
rd_s.goto(1000,1000)
blue_segments.clear()
red_segments.clear()
if bl_s.distance(bl_h)<11:
bl_s.ht()
if bl_s.distance(bl_h)>10:
bl_s.st()
for rd_s in red_segments:
if (rd_s.distance(bl_h)<10) and (bl_h.xcor()!=0 or bl_h.ycor()!=0) and ((rd_h.distance(bl_h)>19) or (bl_h.distance(rd_h)>19)):
scorepen.pendown()
scorepen.write("{} Wins!".format(redname),align="center",font=("candara",24,"bold"))
wn.update()
time.sleep(1)
scorepen.clear()
bl_h.setposition(-720,0)
rd_h.setposition(720,0)
bl_h.direction="right"
rd_h.direction="left"
blgame="stop"
rdgame="stop"
for bl_s in blue_segments:
bl_s.goto(1000,1000)
for rd_s in red_segments:
rd_s.goto(1000,1000)
blue_segments.clear()
red_segments.clear()
if rd_s.distance(rd_h)<11:
rd_s.ht()
if rd_s.distance(rd_h)>10:
rd_s.st()
if (rd_h.distance(bl_h)<20) or (bl_h.distance(rd_h)<20):
scorepen.pendown()
scorepen.write("Draw",align="center",font=("candara",24,"bold"))
wn.update()
time.sleep(1)
scorepen.clear()
bl_h.setposition(-720,0)
rd_h.setposition(720,0)
bl_h.direction="right"
rd_h.direction="left"
blgame="stop"
rdgame="stop"
for bl_s in blue_segments:
for bl_s in blue_segments:
bl_s.goto(1000,1000)
for rd_s in red_segments:
rd_s.goto(1000,1000)
blue_segments.clear()
red_segments.clear()
if bl_s.distance(bl_h)<11:
bl_s.ht()
if bl_s.distance(bl_h)>10:
bl_s.st()
for rd_s in red_segments:
for bl_s in blue_segments:
bl_s.goto(1000,1000)
for rd_s in red_segments:
rd_s.goto(1000,1000)
blue_segments.clear()
red_segments.clear()
if rd_s.distance(rd_h)<11:
rd_s.ht()
if rd_s.distance(rd_h)>10:
rd_s.st()
headpen.goto(bl_h.xcor(),bl_h.ycor()+8)
headpen.pendown()
headpen.write(bluename,align="center",font=("candara",10,"bold"))
headpen.penup()
headpen.goto(rd_h.xcor(),rd_h.ycor()+8)
headpen.pendown()
headpen.write(redname,align="center",font=("candara",10,"bold"))
headpen.penup()
time.sleep(delay)
move()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment