一段时间后如何使子弹消失?

时间:2019-05-11 18:11:42

标签: python python-3.x

我正在尝试制作一款可以按空间发射子弹的游戏。有很多滞后。我试图弄清楚如何使子弹退出屏幕后消失。还有很多滞后。

我很确定滞后是由于子弹到达终点后没有重置屏幕。

我导入了time,但似乎无法弄清楚如何使用它。

这是我尝试过的:

# Importing GUI library
import turtle 
import time

bullets = []
bullets2 = []
# Set speed, GUI, creates new turtle
t_speed = 5
bullet_speed = 50000
screen = turtle.Screen()
t = turtle.Turtle()
t2=turtle.Turtle()
screen.setup(700, 400)

# Arrow Keys as turtle movement
def forward():
  t.forward(t_speed)
def left():
  t.left(t_speed)
def right():
  t.right(t_speed)
def back():
  t.back(t_speed)
def forward2():
  t2.forward(t_speed)
def left2():
  t2.left(t_speed)
def right2():
  t2.right(t_speed)
def back2():
  t2.back(t_speed)

# Shoot a turtle from a turtle
def shoot():
  # sets position of bullet at x and y of t
  # spawn turtle at x and y
  bullet = turtle.Turtle()
  bullet.ht()
  bullet.penup()
  # Launch him
  screen.addshape("uparrow.png")
  bullet.shape("uparrow.png")
  bullet.setheading(t.heading())
  bullet.setposition(t.xcor(), t.ycor())
  bullet.st()
  bullet.forward(700)
  bullet.speed(bullet_speed)
  #TODO: Make less laggy by deleting bullet object after x seconds
def shoot2():
  # set a timer
  current = time.localtime().tm_sec
  # sets position of bullet at x and y of t
  # spawn turtle at x and y
  bullet = turtle.Turtle()
  bullets2.append(bullet)
  bullet.ht()
  bullet.penup()
  # Launch him
  screen.addshape("uparrow.png")
  bullet.shape("uparrow.png")
  bullet.setheading(t2.heading())
  bullet.setposition(t2.xcor(), t2.ycor())
  bullet.st()
  bullet.forward(700)
  bullet.speed(bullet_speed)
  #TODO: Make less laggy by deleting bullet object after x seconds
  #new = time.localtime().tm_sec
  #if new > current + 3:
    #bullets2.

def playGame():
  # TODO: Health Bar

  # TODO: Characters

  t.penup()
  t.setheading(5)
  t2.penup()
  # TODO
  still_alive = True
  # Movement
  screen.onkey(back, "Down")
  screen.onkey(left, "Left")
  screen.onkey(right, "Right")
  screen.onkey(shoot, "space")
  screen.onkey(forward, "Up")
  screen.onkey(back2, "S")
  screen.onkey(left2, "A")
  screen.onkey(right2, "D")
  screen.onkey(shoot2, "Z")
  screen.onkey(forward2, "W")
  # Game Engine
  screen.listen()
  t.mainloop()

def gameStart():
  # Title
  print("Welcome to my game!")
  # Menu; press Q to quit, Press p to play
  startGame = True
  while startGame == True:
    inp = raw_input("Press p to play or Press q to quit")
    if inp == "q":
      exit()
    elif inp == "p":
      playGame()
      startGame = False
    else:
      print("Incorrect prompt")
  # Instructions
  print("Use Arrow Keys to move. Press space to shoot")

def main():
  gameStart()

if __name__ == "__main__":
  main()

0 个答案:

没有答案