为什么这个 pygame 程序不起作用,所以当我将鼠标悬停在计算机屏幕上时它会变成蓝色? [英] Why is this pygame program not working so when I hover over the computer screen it turns blue?

查看:46
本文介绍了为什么这个 pygame 程序不起作用,所以当我将鼠标悬停在计算机屏幕上时它会变成蓝色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 pygame 的新手,我正在尝试为一个游戏做一个介绍,其中用户将鼠标悬停在计算机屏幕上,然后屏幕变成蓝色,表示当您按下它时游戏将开始.但是,蓝色矩形根本没有出现?顺便说一下,introScreen 就像一个 gif,但由许多不同的框架构成.

Hello I am quite new to pygame and I am trying to make an intro for a game where the user hovers over the computer screen and the screen then turns blue so indicate that when you press it the game will start. However, the blue rect simply isn't showing up? By the way, the introScreen is a like a gif but constructed from many different frames.

这是我的代码:

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

没有错误消息只是不显示蓝色矩形?请帮忙,因为我需要这个用于学校项目.

There is no error message it just doesn't display the blue rect? Please help as I need this for a school project.

推荐答案

要在 PyGame 中使用多线程,您必须考虑两件事:

To use multithreading in PyGame, you've to consider 2 things:

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

  1. Process an event loop in the main thread

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

Do all the drawing on and the update of the window in a single thread. If there is an "intro" thread and a "game" thread, then the "intro" thread has to terminate first, before the "game" thread can start.

在主线程(程序)中,您必须声明线程并初始化控制(状态)变量.立即启动介绍"线程并运行事件循环.
我建议至少实现 pygame.QUIT,它表示程序必须终止(run = False).
进一步可以连续检查鼠标是否在开始区域(inStartRect = startRectArea.collidepoint(*pygame.mouse.get_pos())):

In the main thread (program) you've to declare the threads and to initialize the control (state) variables. Start the "intro" thread immediately and run the event loop.
I recommend to implement at least the pygame.QUIT, which signals that the program has to be terminated (run = False).
Further it can be continuously checked if the mouse is in the start area (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

在介绍线程中,必须绘制整个介绍屏幕,包括背景和蓝色开始区域.如果介绍结束(游戏开始),则在介绍线程结束时,主游戏循环开始:

In the intro thread the entire intro screen has to be drawn, including the background and the blue start area. If the intro is finished (the game is started), then at the end of the intro thread the main game loop is started:

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

当介绍线程终止时,游戏线程开始.所以游戏线程可以做游戏场景的绘制:

The game thread starts, when the intro thread terminates. So the game thread can do the drawing of the game scene:

def gameLoop(): 

    while run:
        clock.tick(60)

        screen.fill(red)

        # [...]

        pygame.display.flip()
        clock.tick(60)

这篇关于为什么这个 pygame 程序不起作用,所以当我将鼠标悬停在计算机屏幕上时它会变成蓝色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆