Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import os
- import math
- import random
- list = []
- c_id = 0
- mlst = []
- def adduser():
- global c_id
- print("\n----- Add User -----")
- idn = len(list) + 1
- firstn = input("First name: ")
- lastn = input("Last name: ")
- ema = input("Email: ")
- search = bsearch(mlst, ema)
- if search == 1:
- print("\n!!! Email has already been used. Try again!!!\n")
- main()
- else:
- c_id+=1
- passw = input("Password: ")
- print(">>> Student added with ID: {}".format(c_id))
- user = {
- "ID" : idn,
- "Firstname" : firstn,
- "Lastname" : lastn,
- "Mail" : ema,
- "Password" : passw}
- mlst.append(ema)
- mailsort(mlst, "Mail")
- list.append(user)
- wait()
- main()
- def dispuser():
- global list
- dictsort(list, "ID")
- print("\n----- Display Users -----")
- print("ID\t\t\t", "Name\t\t\t\t", "Email")
- for i in range(len(list)):
- name = str(list[i]["Firstname"] + " " + list[i]["Lastname"])
- ID = str(list[i]["ID"])
- mail = str(list[i]["Mail"])
- print(ID,"\t\t\t",name,"\t\t\t",mail)
- def bsearch(mlst, maill):
- first = 0
- last = len(mlst)-1
- found = 0
- while first <= last and not found:
- midpoint = (first + last)//2
- if mlst[midpoint] == maill:
- found = 1
- else:
- if maill < mlst[midpoint]:
- last = midpoint - 1
- else:
- first = midpoint + 1
- return found
- def mailsort(mlst, maill):
- for fillslot in range(len(mlst) -1, 0 , -1):
- positionOfMax = 0
- for location in range(1, fillslot + 1):
- if mlst[location] > mlst[positionOfMax]:
- positionOfMax = location
- temp = mlst[fillslot]
- mlst[fillslot] = mlst[positionOfMax]
- mlst[positionOfMax] = temp
- def dictsort(list, idd):
- for fillslot in range(len(list) -1, 0 , -1):
- positionOfMax = 0
- for location in range(1, fillslot + 1):
- if list[location][idd] > list[positionOfMax][idd]:
- positionOfMax = location
- temp = list[fillslot]
- list[fillslot] = list[positionOfMax]
- list[positionOfMax] = temp
- def wait():
- wait = input("\nPress <ENTER> to continue...")
- def main():
- print("\n----- Menu -----")
- print("""A - Add user
- D - Display Users
- P - Play game
- E - Exit""")
- ch = input("\n>> Enter your choice: ")
- while (1):
- if ch.lower()=='a':
- adduser()
- elif ch.lower()=='d':
- dispuser()
- wait()
- main()
- return False
- elif ch.lower()=='p':
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("Space Guardian")
- #wn.bgpic("space_invaders_background.gif")
- score = 0
- #draw score
- score_pen = turtle.Turtle()
- score_pen.speed(0)
- score_pen.color("white")
- score_pen.penup()
- score_pen.setposition(-290, 280)
- scorestring = "Score: %s" %score
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- score_pen.hideturtle()
- #shapes
- turtle.register_shape("invader.gif")
- turtle.register_shape("player.gif")
- #border
- border_pen = turtle.Turtle()
- border_pen.speed(0)
- border_pen.color("white")
- border_pen.penup()
- border_pen.setposition(-300,-300)
- border_pen.pendown()
- border_pen.pensize(3)
- for side in range(4):
- border_pen.fd(600)
- border_pen.lt(90)
- border_pen.hideturtle()
- #player
- player = turtle.Turtle()
- player.color("blue")
- player.shape("triangle")
- player.penup()
- player.speed(0)
- player.setposition(0,-250)
- player.setheading(90)
- playerspeed = 15
- #number of enemies
- number_of_enemies = 2
- #list of enemies
- enemies = []
- #add enemies to the list
- for i in range(number_of_enemies):
- enemies.append(turtle.Turtle())
- for enemy in enemies:
- enemy.color("red")
- enemy.shape("circle")
- enemy.penup()
- enemy.speed(0)
- x = random.randint(-200,200)
- y = random.randint(100,250)
- enemy.setposition(x,y)
- enemyspeed= 3
- #player weapon
- bullet = turtle.Turtle()
- bullet.color("yellow")
- bullet.shape("triangle")
- bullet.penup()
- bullet.speed(0)
- bullet.setheading(90)
- bullet.shapesize(0.5,0.5)
- bullet.hideturtle()
- bulletspeed = 25
- #weapon state
- # ready to fire and fire
- bulletstate = "ready"
- #move function
- def move_left():
- x = player.xcor()
- x -= playerspeed
- #player border boundary
- if x <- 280:
- x= -280
- player.setx(x)
- def move_right():
- x = player.xcor()
- x += playerspeed
- #player border boundary
- if x > 280:
- x= 280
- player.setx(x)
- #bullet fn.
- def fire_bullet():
- global bulletstate
- if bulletstate == "ready":
- bulletstate = "fire"
- if not bullet.isvisible():
- x = player.xcor()
- y = player.ycor() + 10
- bullet.setposition(x,y)
- bullet.showturtle()
- #bullet above player
- x = player.xcor()
- y = player.ycor()
- bullet.setposition(x, y+10)
- bullet.showturtle()
- def isCollision(t1, t2):
- distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
- if distance < 15:
- return True
- else:
- return False
- wn.listen()
- wn.onkeypress(move_left, "Left")
- wn.onkeypress(move_right, "Right")
- wn.onkeypress(fire_bullet,"space")
- #main game loop
- while True:
- for enemy in enemies:
- #move enemy
- x = enemy.xcor()
- x += enemyspeed
- enemy.setx(x)
- #enemy boundary check
- if enemy.xcor() > 280:
- #move all enemies down
- for e in enemies:
- y = e.ycor()
- y -= 40
- e.sety(y)
- #change direction
- enemyspeed *= -1
- if enemy.xcor() < - 280:
- #move all enemies down
- for e in enemies:
- y = e.ycor()
- y -= 40
- e.sety(y)
- #change direction
- enemyspeed *= -1
- #collision w bullet and enemy
- if isCollision(bullet,enemy):
- bullet.hideturtle()
- bulletstate = "ready"
- bullet.setposition(0, -400)
- x = random.randint(-200,200)
- y = random.randint(100,250)
- enemy.setposition(x,y)
- #update score
- score += 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
- if isCollision(player,enemy):
- player.hideturtle()
- enemy.hideturtle()
- print("Game over")
- break
- #Boundary check
- if enemy.xcor() > 300 or enemy.xcor() < -300:
- print("GAME OVER")
- t1 = turtle.Turtle()
- t1.pencolor("#8e072b")
- t1.hideturtle()
- t1.penup()
- t1.write("GAME OVER", align = "center", font=("Silkscreen",20))
- t1.setposition(0,-350)
- t1.penup()
- if enemy.ycor() > 300 or enemy.ycor() < -300:
- print("Game OVER")
- t1 = turtle.Turtle()
- t1.pencolor("#8e072b")
- t1.hideturtle()
- t1.penup()
- t1.write("GAME OVER", align = "center", font=("Silkscreen",20))
- t1.setposition(0,-350)
- t1.penup()
- quit()
- #move bullet
- if bulletstate == "fire":
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- if bullet.ycor() > 275:
- bullet.hideturtle()
- bulletstate = "ready"
- if bullet.isvisible():
- y = bullet.ycor() + bulletspeed
- bullet.sety(y)
- wn.mainloop()
- delay = input("enter to finish")
- elif ch.lower()=='e':
- print("\nThank you for coming!")
- raise SystemExit()
- else:
- print("\n!!! Invalid input. Try again !!!\n")
- main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement