Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Blitting不起作用?_Python_Pygame_Blit - Fatal编程技术网

Python Blitting不起作用?

Python Blitting不起作用?,python,pygame,blit,Python,Pygame,Blit,我正在用python和pygame编写一个游戏,我试图创建一个主菜单,但我的标签不会显示???,我也遇到了这个错误:第22行,在x,y=pygame.mouse.get_pos()中 错误:视频系统未初始化????我不明白这里发生了什么,因为我对python非常陌生,下面是我的代码: bif="menubg.jpg" load_check_x = "null" load_check_y = "null" import pygame, sys from pygame.locals import *

我正在用python和pygame编写一个游戏,我试图创建一个主菜单,但我的标签不会显示???,我也遇到了这个错误:第22行,在x,y=pygame.mouse.get_pos()中 错误:视频系统未初始化????我不明白这里发生了什么,因为我对python非常陌生,下面是我的代码:

bif="menubg.jpg"
load_check_x = "null"
load_check_y = "null"
import pygame, sys
from pygame.locals import *
x = 0
y = 0


pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
background=pygame.image.load(bif).convert()
pygame.display.set_caption("Some random Title")
pygame.display.flip()
#screen.fill((0,0,0))

while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
x,y = pygame.mouse.get_pos()
#Setup mouse pos!!!
if x >= 340 and x <= 465:
    load_check_x = "True"
if x < 340 or x > 465:
    load_check_x = "False"

if y >= 425 and y <= 445:
    load_check_y = "True"
if y < 425 or y > 445:
    load_check_y = "False"

if load_check_x == "True" and load_check_y == "True":
   for event in evc:
       if event.type ==pygame.MOUSEBUTTONUP:
           clear()
labelfont = pygame.font.SysFont("Comic Sans MS", 12)
new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
screen.blit(new_text, (340, 425))
bif=“menubg.jpg”
加载\u检查\u x=“空”
加载\u检查\u y=“空”
导入pygame,sys
从pygame.locals导入*
x=0
y=0
pygame.init()
screen=pygame.display.set_模式((640360),0,32)
background=pygame.image.load(bif.convert())
pygame.display.set_标题(“一些随机标题”)
pygame.display.flip()
#屏幕填充((0,0,0))
尽管如此:
evc=pygame.event.get()
对于evc中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
x、 y=pygame.mouse.get_pos()
#设置鼠标位置!!!
如果x>=340和x 465:
加载\u检查\u x=“错误”
如果y>=425和y 445:
加载检查=“错误”
如果加载检查x==“True”和加载检查y==“True”:
对于evc中的事件:
如果event.type==pygame.MOUSEBUTTONUP:
清除()
labelfont=pygame.font.SysFont(“Comic Sans MS”,12)
new_text=labelfont.render('Play!!!!',1,(255,255,255))
屏幕。blit(新文本,(340425))

有人帮帮我

看来你的缩进是错的。python解释器如何知道if语句体何时结束?它查看选项卡或空格的数量,以查看后面的代码是否属于主体

如果您查看代码,您将看到您有一些执行初始化的代码,然后是一个
while
循环和另一个处理事件的循环。之后,你有一些代码,检查鼠标等

正如您所看到的,事件循环之后的所有代码都不属于while循环,应该属于while循环。除此之外,您还可以通过创建新的矩形并检查单击的点是否位于矩形内来替换所有if条件

my_rect = pygame.Rect((340,425),(125,20))
while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
    x,y = pygame.mouse.get_pos()
    #Setup mouse pos!!!
    if my_rect.collidepoint(x,y):
       for event in evc:
           if event.type ==pygame.MOUSEBUTTONUP:
               clear()
    labelfont = pygame.font.SysFont("Comic Sans MS", 12)
    new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
    screen.blit(new_text, (340, 425))

缩进很重要
pygame.mouse.get_pos()
在while循环完成后运行,调用了
pygame.quit()
。什么意思,你能告诉我吗?你的大部分代码(包括blit)都是在你用
pygame.quit
终止pygame后调用的。while循环的内部只有四行。我仍然得到视频系统未初始化错误?您正在调用
pygame.display.set_mode
?好的,我得到了要显示的矩形。我将我的_rect=pygame.rect((340425),(125,20))更改为我的_rect=pygame.draw.rect(屏幕,(140240130),rect((100100),(130170)))当没有调用pygame.display.set_模式或有
pygame.quit
后退出时,会出现未初始化的视频。确保在任何其他pygame操作之前未调用pygame.quit。