繁体   English   中英

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

[英]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 中使用多线程,您必须考虑两件事:

  1. 在主线程中处理事件循环

  2. 在单个线程中完成所有绘图和窗口更新。 如果有一个“介绍”线程和一个“游戏”线程,那么“介绍”线程必须先终止,然后“游戏”线程才能开始。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM