首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么它会给我"RuntimeError:线程只能启动一次“?

为什么它会给我"RuntimeError:线程只能启动一次“?
EN

Stack Overflow用户
提问于 2020-12-22 15:31:49
回答 3查看 256关注 0票数 1

如果我运行这段代码,它会给我错误信息,

RuntimeError:线程只能启动一次

一个窗口弹出与适当的图像,但它是冻结和屏幕立即崩溃。我认为线程无法在while循环中工作,但是如果是这样的话,我将如何修复代码。

代码语言:javascript
复制
import pygame
import random
import time
import threading
pygame.init()

#Creates a window for the game
screen = pygame.display.set_mode((1200,720))
#Names the window
pygame.display.set_caption("Pigeon Shootout")

#crosshair location
cx = 550
cy = 300
#pigeon1 location
p1x = 1000
p1y = 400
#pigeon2 location
p2x = 600
p2y = 400
#pigeon3 location
p3x = 200
p3y = 400

#Names images 
Background = pygame.image.load('Background.png')
Crosshair = pygame.image.load('Crosshair.png')
Pigeon = pygame.image.load('Pigeon.png')

def pigeon_spawn():
    #Generates random number than spawns pigeon acordingly
    num = random.randint(1,3)
    if num == 1:
        screen.blit(Pigeon, (p1x, p1y))
    elif num == 2:
        screen.blit(Pigeon, (p2x, p2y))
    else:
        screen.blit(Pigeon, (p3x, p3y))
    time.sleep(5)

#Puts pigeon_spawn run separately
pig = threading.Thread(target=pigeon_spawn)


run = True
while run == True:
    
    #imputs background and crosshair
    screen.blit(Background, (0,0))
    screen.blit(Crosshair, (cx, cy))
    
    #starts pigeon_spawn
    pig.start()
    
    #enables you to quit when you press the x at the top
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    #makes crosshair move with the arrow keys
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        cy -= 1
    if keys[pygame.K_DOWN]:
        cy += 1
    if keys[pygame.K_LEFT]:
        cx -= 1.5
    if keys[pygame.K_RIGHT]:
        cx += 1.5
    screen.blit(Crosshair, (cx, cy))
    pygame.display.update()    

pygame.quit() 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-22 15:52:53

你的方法行不通。不能在线程中使用曲面指示器来blit曲面。整个场景需要在应用程序循环中不断地重新绘制。添加一个变量pigeon_pos变量,该变量将存储全局名称空间中的Pigeon位置。在交易中更改循环中的变量。在应用程序循环中blit Pigeon。在循环之前启动线程一次:

代码语言:javascript
复制
pigeon_pos = (p1x, p1y)
run = True

def pigeon_spawn():
    global pigeon_pos
    while run:
        pigeon_pos = random.choice([(p1x, p1y), (p2x, p2y), (p3x, p3y)])
        print(pigeon_pos)
        time.sleep(5)

#Puts pigeon_spawn run seperatly
pig = threading.Thread(target=pigeon_spawn)
#starts pigeon_spawn
pig.start()

while run == True:
    #enables you to quit when you press the x at the top
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    #makes crosshair move with the arrow keys
    keys = pygame.key.get_pressed()
    cx += keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
    cy += keys[pygame.K_DOWN] - keys[pygame.K_UP]

    #imputs background and crosshair
    screen.blit(Background, (0,0))
    screen.blit(Crosshair, (cx, cy))
    screen.blit(Pigeon, pigeon_pos)
    pygame.display.update()
票数 0
EN

Stack Overflow用户

发布于 2020-12-22 15:44:33

如果您真的想在一个单独的线程中一次又一次地运行函数pigeon_spawn中的代码,那么为什么不将该代码放在一个循环中,使线程永远不会退出呢?

如果确实需要一次又一次地启动一个新线程,那么每次都要创建一个新的thread对象。只需将线程的创建移动到while循环中:

代码语言:javascript
复制
while run == True:
    ...
    threading.Thread(target=pigeon_spawn).start()
    ...
票数 0
EN

Stack Overflow用户

发布于 2020-12-22 15:57:21

RuntimeError:线程只能启动一次

简单地说,线程只能启动一次,而不是两次。

代码语言:javascript
复制
import threading
import time

def function():
    print("Hello!")
    time.sleep(3)
    print("Bye")

#Starts thread1
thread1 = threading.Thread(target=function)
thread1.start()

#Starts thread2
thread2 = threading.Thread(target=function)
thread2.start()

#Tries to start thread 2, but it is already started, so it will give a RunTimeError
thread2.start()

控制台:

代码语言:javascript
复制
Hello!
Hello!
Traceback (most recent call last):
  File "C:\Users\Lukas\Desktop\test322.py", line 16, in <module>
    thread2.start()
  File "C:\Users\Lukas\AppData\Local\Programs\Python\Python39\lib\threading.py", line 865, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
Bye
Bye
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65411445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档