#import modules import turtle import random import time #set variables score1 = 0 score2 = 0 delay = 0.1 #set conditions to change for main code flag1_attach = False flag2_attach = False #screen setup wn = turtle.Screen() wn.title('Capture The Flag') wn.bgcolor('black') wn.setup(1410,600) wn.tracer(0) #make a turtle to write for How to play, Controls and Creator multi_function_pen=turtle.Turtle() multi_function_pen.color('white') multi_function_pen.ht() multi_function_pen.pu() multi_function_pen.setpos(-555,-200) multi_function_pen.write('Created by: Karanvir Singh', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-555,170) multi_function_pen.write('How To Play:', align='center', font=('Courier', 31, 'normal')) multi_function_pen.goto(-560,120) multi_function_pen.write('Score the most points', align='center', font=('Courier', 17, 'normal')) multi_function_pen.goto(-560,100) multi_function_pen.write('to win!', align='center', font=('Courier', 17, 'normal')) multi_function_pen.goto(-560,60) multi_function_pen.color('green') multi_function_pen.write('You can win points by:', align='center', font=('Courier', 15, 'normal')) multi_function_pen.goto(-562,40) multi_function_pen.color('white') multi_function_pen.write('- Capturing the opponents', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-650,20) multi_function_pen.write('flag', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-557,0) multi_function_pen.write('- Tagging out the opponent', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-583,-20) multi_function_pen.write('in your territory', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-565,-70) multi_function_pen.color('red') multi_function_pen.write('Your opponent can win', align='center', font=('Courier', 15, 'normal')) multi_function_pen.goto(-630,-90) multi_function_pen.write('points if:', align='center', font=('Courier', 15, 'normal')) multi_function_pen.goto(-553,-110) multi_function_pen.color('white') multi_function_pen.write('- You touch the game border', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-563,-130) multi_function_pen.write('- You touch your own flag', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(-638,-150) multi_function_pen.write('border', align='center', font=('Courier', 13, 'normal')) multi_function_pen.goto(585,160) multi_function_pen.write('Controls:', align='center', font=('Courier', 35, 'normal')) multi_function_pen.goto(585,90) multi_function_pen.write('Player 1:', align='center', font=('Courier', 25, 'normal')) multi_function_pen.goto(580,50) multi_function_pen.write('W A S D', align='center', font=('Courier', 20, 'normal')) multi_function_pen.goto(585,-70) multi_function_pen.write('Player 2:', align='center', font=('Courier', 25, 'normal')) multi_function_pen.goto(585,-110) multi_function_pen.write('Arrow Keys', align='center', font=('Courier', 20, 'normal')) #make a turtle for the examples for Controls example1 = turtle.Turtle() example1.color('yellow') example1.shape('square') example1.penup() example1.goto(580,20) example2 = turtle.Turtle() example2.color('cyan') example2.shape('square') example2.penup() example2.goto(580,-150) #make a turtle for border border = turtle.Turtle() border.speed(0) border.color('white', '#25902B') border.penup() border.setposition(-390,-200) border.pendown() border.pensize(20) #make the border border.begin_fill() def move1(): border.forward(820) border.left(90) border.forward(400) border.left(90) move1() move1() border.end_fill() #make a turtle for the inside squares inside the border isis = turtle.Turtle() isis.ht() isis.speed(0) isis.color('#C1EBB7') isis.penup() isis.goto(-380,190) isis.pendown() #make the inside squares def move2(): isis.right(90) isis.forward(20) isis.right(90) def move3(): isis.left(90) isis.forward(20) isis.left(90) for i in range (9): isis.forward(800) move2() isis.forward(800) move3() isis.forward(800) move2() isis.forward(800) isis.setheading(90) isis.setpos(-380,-190) for i in range (20): isis.forward(380) move2() isis.forward(380) move3() #make an extra border to cover inside squares border.color('white') border.pensize(21) move1() move1() #draw the line half-way line border.pensize(6) border.penup() border.goto(20,200) border.right(90) border.pendown() border.forward(400) border.ht() #make a turtle for player1 player1 = turtle.Turtle() player1.speed(0) player1.color('yellow') player1.shape('square') player1.penup() player1.goto(-130,0) #make a turtle for player2 player2 = turtle.Turtle() player2.speed(0) player2.color('cyan') player2.shape('square') player2.penup() player2.goto(170,0) #make a turtle for flag1 flag1 = turtle.Turtle() flag1.speed(0) flag1.color('yellow') flag1.shape('triangle') flag1.penup() flag1.setpos(-290,0) flag1.setheading(90) #make a turtle for flag2 flag2 = turtle.Turtle() flag2.speed(0) flag2.color('cyan') flag2.shape('triangle') flag2.penup() flag2.setpos(330,0) flag2.setheading(90) #make a turtle to make the borders for the flags fb = turtle.Turtle() fb.speed(0) fb.color('white') fb.pensize(3) fb.penup() fb.setpos(360,-30) fb.pendown() for i in range(4): fb.left(90) fb.forward(60) fb.penup() fb.setpos(-320,-30) fb.pendown() for i in range(4): fb.forward(60) fb.left(90) fb.ht() #make a turtle to write the score score_pen=turtle.Turtle() score_pen.ht() score_pen.speed(-1) score_pen.pu() #writes the scores score_pen.goto(-195,235) score_pen.color('yellow') score_pen.write('Score:0', align='center', font=('Courier', 55, 'normal')) score_pen.goto(235,235) score_pen.color('cyan') score_pen.write('Score:0', align='center', font=('Courier', 55, 'normal')) #defined functions for movement of the player1 def up1(): player1.setheading(90) player1.forward(20) def down1(): player1.setheading(270) player1.forward(20) def left1(): player1.setheading(180) player1.forward(20) def right1(): player1.setheading(0) player1.forward(20) #defined functions for movement of the player2 def up2(): player2.setheading(90) player2.forward(20) def down2(): player2.setheading(270) player2.forward(20) def left2(): player2.setheading(180) player2.forward(20) def right2(): player2.setheading(0) player2.forward(20) #defined function for restarting the game everytime someone wins def restart_move(): #globalises variables to change in fucntion global flag1_attach global flag2_attach flag1_attach = False flag2_attach = False #resets everything back to original position player1.setpos(-130,0) player2.setpos(170,0) flag1.setpos(-290,0) flag2.setpos(330,0) #writes the new score score_pen.clear() score_pen.goto(-195,235) score_pen.color('yellow') score_pen.write('Score:{}'.format(score1), align='center', font=('Courier', 55, 'normal')) score_pen.goto(235,235) score_pen.color('cyan') score_pen.write('Score:{}'.format(score2), align='center', font=('Courier', 55, 'normal')) wn.update() #key bindings which connect certain keys to the function to be carried out wn.listen() wn.onkey(up1, 'w') wn.onkey(down1, 's') wn.onkey(left1, 'a') wn.onkey(right1, 'd') wn.update() wn.listen() wn.onkey(up2, 'Up') wn.onkey(down2, 'Down') wn.onkey(left2, 'Left') wn.onkey(right2, 'Right') wn.update() #main code loop while True: #updates the screen wn.update() #to detect large border collision if player1.xcor() < -380 or player1.xcor() > 420 or player1.ycor() > 190 or player1.ycor() < -190: score2+=1 restart_move() if player2.xcor() < -380 or player2.xcor() > 420 or player2.ycor() > 190 or player2.ycor() < -190: score1+=1 restart_move() #to detect flag border collision (has to be placed before player collision to ensure statements don't clash) if player1.xcor() < -260 and player1.xcor() > -320 and player1.ycor() > -30 and player1.ycor() < 30: score2+=1 restart_move() if player2.xcor() > 300 and player2.xcor() < 360 and player2.ycor() > -30 and player2.ycor() < 30: score1+=1 restart_move() #to detetct player collision if player1.xcor() > 0 and player1.distance(player2) < 15: score2+=1 restart_move() if player2.xcor() < 0 and player2.distance(player1) < 15: score1+=1 restart_move() #attaching flag to player if player has touched flag if flag2.distance(player1) < 20: flag2_attach=True if flag1.distance(player2) < 20: flag1_attach=True if flag2_attach: x=player1.xcor() y=player1.ycor() flag2.goto(x,y) if flag1_attach: x=player2.xcor() y=player2.ycor() flag1.goto(x,y) #to detect player taking flag to their base if flag1.xcor() > 0: score2+=1 restart_move() if flag2.xcor() < 0: score1+=1 restart_move() #delays the screen for smooth animation time.sleep(delay) #for looping the code wn.mainloop()