[英]When i make a pygame game and hover over it, the cursor turns into the loading thing
[英]Why is this pygame program not working so when I hover over the computer screen it turns blue?
您好,我是 pygame 的新手,我正在嘗試為游戲做一個介紹,其中用戶將鼠標懸停在計算機屏幕上,然后屏幕變為藍色,以便表示當您按下它時游戲將開始。 但是,藍色矩形根本沒有出現? 順便說一下,introScreen 就像一個 gif,但由許多不同的框架構成。
這是我的代碼:
import pygame
import pygame.gfxdraw
import threading
pygame.init()
width = 800
height = 600
fps = 30
clock = pygame.time.Clock()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
screen = pygame.display.set_mode((width, height))
def introLoop():
while intro:
for i in range(0, 26):
clock.tick(8)
i = str(i)
introScreen = pygame.image.load("introScreen/" + i + ".gif")
introScreen = pygame.transform.scale(introScreen, (width, height))
screen.blit(introScreen, (30, 30))
pygame.display.flip()
def gameLoop():
while intro:
mouseX, mouseY = pygame.mouse.get_pos()
startRectArea = pygame.Rect(279, 276, 220, 128)
if startRectArea.collidepoint(mouseX, mouseY):
StartRect = pygame.draw.rect(screen, blue, (279, 276, 220, 128), 0)
pygame.display.update()
for event in pygame.event.get():
holder = 0
introThread = threading.Thread(target = introLoop)
gameThread = threading.Thread(target = gameLoop)
intro = True
introThread.start()
gameThread.start()
沒有錯誤消息它只是不顯示藍色矩形? 請幫忙,因為我需要這個用於學校項目。
要在 PyGame 中使用多線程,您必須考慮兩件事:
在主線程中處理事件循環
在單個線程中完成所有繪圖和窗口更新。 如果有一個“介紹”線程和一個“游戲”線程,那么“介紹”線程必須先終止,然后“游戲”線程才能開始。
在主線程(程序)中,您必須聲明線程並初始化控制(狀態)變量。 立即啟動“介紹”線程並運行事件循環。
我建議至少實現pygame.QUIT
,它表示程序必須終止( run = False
)。
此外,它可以連續檢查鼠標是否在開始區域( inStartRect = startRectArea.collidepoint(*pygame.mouse.get_pos())
):
introThread = threading.Thread(target = introLoop)
gameThread = threading.Thread(target = gameLoop)
run = True
intro = True
inStartRect = False
startRectArea = pygame.Rect(279, 276, 220, 128)
introThread.start()
while run:
if intro:
inStartRect = startRectArea.collidepoint(*pygame.mouse.get_pos())
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN and inStartRect:
intro = False
在介紹線程中,必須繪制整個介紹屏幕,包括背景和藍色開始區域。 如果介紹結束(游戲開始),則在介紹線程結束時,主游戲循環開始:
def introLoop():
i = 0
while run and intro:
clock.tick(8)
filename = "introScreen/" + str(i) + ".gif"
i = i+1 if i < 26 else 0
introScreen = pygame.image.load(filename)
introScreen = pygame.transform.scale(introScreen, (width, height))
screen.blit(introScreen, (30, 30))
if inStartRect:
pygame.draw.rect(screen, blue, (279, 276, 220, 128), 0)
pygame.display.flip()
gameThread.start()
當介紹線程終止時,游戲線程開始。 所以游戲線程可以做游戲場景的繪制:
def gameLoop():
while run:
clock.tick(60)
screen.fill(red)
# [...]
pygame.display.flip()
clock.tick(60)
嘗試將 intro 直接傳遞到introLoop
函數中。 使用args
參數將args
傳遞到線程中...
introThread = threading.Thread(target=introLoop, args=(intro))
您還必須重新定義函數:
def introLoop(intro):
嘗試使用.convert()
轉換要加載的圖像
pygame.image.load("image file path").convert()
或者嘗試使用.convert_alpha()
將圖像轉換為圖像
pygame.image.load("image file path").convert_alpha()
Pygame的發現它更容易和更快地被轉換與加載圖像.convert()
並與.convert_alpha()
把它轉換他們的阿爾法層(例如透明...)希望這有助於圖像!
有關更多信息,請參閱這些谷歌搜索結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.