为什么这个pygame程序不能正常工作,所以当我将鼠标悬停在计算机屏幕上时会变成蓝色?

问题描述 投票:1回答:3

您好我对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()

没有错误消息它只是不显示蓝色矩形?请帮助我在学校项目中需要这个。

python pygame
3个回答
1
投票

要在PyGame中使用多线程,你需要考虑两件事:

  1. 在主线程中处理事件循环
  2. 在单个线程中完成所有绘图和窗口更新。如果存在“简介”线程和“游戏”线程,则“引入”线程必须首先终止,然后才能启动“游戏”线程。

在主线程(程序)中,您需要声明线程并初始化控制(状态)变量。立即启动“intro”线程并运行事件循环。 我建议至少实施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)

0
投票

尝试直接将intro传递给introLoop函数。使用args参数将参数传递给线程...

introThread = threading.Thread(target=introLoop, args=(intro))

您还必须重新定义该功能:

def introLoop(intro):

0
投票

尝试使用.convert()转换要加载的图像

pygame.image.load("image file path").convert()

或尝试使用.convert_alpha()将图像转换为图像

pygame.image.load("image file path").convert_alpha()

Pygame发现加载用.convert()转换的图像更容易,更快,而使用.convert_alpha(),它会转换图像的alpha图层(E.G. Transparency ...)希望这会有所帮助!

有关更多信息,请参阅以下谷歌搜索结果:

I recommend the first link, although this link is pretty pointless

© www.soinside.com 2019 - 2024. All rights reserved.