How do I make an object disappear after some time?

微笑、不失礼 提交于 2019-12-19 11:18:01

问题


I am trying to make a game where I can press space to shoot my bullet. There is a lot of lag. I am trying to figure out how to make the bullet disappear after it exits the screen. There is also a lot of lag. I am pretty sure the lag is from the bullet not resetting the screen after reaching the end. I imported time but I just can't seem to figure out how to use time.

This is what I tried:

# 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()

The object should disappear after some time.


回答1:


Your progam appears to be a collection of misconceptions glued together with bad code management. Your bullet not only has lag, but it locks out all other action while it's in motion! Let's toss the time module idea and instead take advantage of turtle's own timer events to really let the bullets fly:

from turtle import Screen, Turtle

# Arrow Keys as turtle movement
def forward_1():
    player_1.forward(player_speed)

def back_1():
    player_1.back(player_speed)

def left_1():
    player_1.left(player_speed)

def right_1():
    player_1.right(player_speed)

def shoot_1():
    screen.onkey(None, "space")  # disable handler inside handler!
    shoot(player_1)
    screen.onkey(shoot_1, "space")

def forward_2():
    player_2.forward(player_speed)

def left_2():
    player_2.left(player_speed)

def right_2():
    player_2.right(player_speed)

def back_2():
    player_2.back(player_speed)

def shoot_2():
    screen.onkey(None, "z")
    shoot(player_2)
    screen.onkey(shoot_2, "z")

def travel(bullet, milliseconds):
    bullet.forward(bullet_speed)

    if milliseconds:
        screen.ontimer(lambda b=bullet, s=milliseconds - 100: travel(b, s), 100)
    else:
        bullet.hideturtle()
        bullets.append(bullet)

# Shoot a bullet from a player
def shoot(player):
    # sets position of bullet at x and y of player
    # spawn turtle at x and y
    if bullets:
        bullet = bullets.pop(0)
    else:
        bullet = Turtle(visible=False)
        # bullet.shape("uparrow.png")
        bullet.shape('arrow')
        bullet.speed('fastest')
        bullet.penup()

    # Launch him
    bullet.color(player.fillcolor())
    bullet.setheading(player.heading())
    bullet.setposition(player.position())
    bullet.showturtle()

    travel(bullet, 2000)

bullets = []

# Set speed, GUI, creates new turtle
player_speed = 10
bullet_speed = 15

screen = Screen()
screen.setup(700, 400)
# screen.addshape("uparrow.png")

player_1 = Turtle('triangle')
player_1.speed('fastest')
player_1.color('red', 'pink')
player_1.penup()
player_1.setheading(90)

player_2 = Turtle('triangle')
player_2.speed('fastest')
player_2.color('blue', 'cyan')
player_2.penup()
player_2.setheading(270)

# Movement
screen.onkey(forward_1, "Up")
screen.onkey(back_1, "Down")
screen.onkey(left_1, "Left")
screen.onkey(right_1, "Right")
screen.onkey(shoot_1, "space")

screen.onkey(forward_2, "w")
screen.onkey(back_2, "s")
screen.onkey(left_2, "a")
screen.onkey(right_2, "d")
screen.onkey(shoot_2, "z")

# Game Engine
screen.listen()
screen.mainloop()



来源:https://stackoverflow.com/questions/56095507/how-do-i-make-an-object-disappear-after-some-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!