简体   繁体   English

Pygame窗口显示黑屏

[英]Pygame window displays blank black screen

Here is my code, please keep in mind that I picked up python a couple of days ago so my code may be incorrectly made, ect. 这是我的代码,请记住,几天前我选择了python,因此我的代码可能制作不正确,等等。 I am trying to make a window that will display some text (beta) and will display two small rectangles that I want to make into buttons. 我正在尝试制作一个窗口,该窗口将显示一些文本(测试版)并显示两个我想放入按钮的小矩形。

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)

screen = pygame.display.set_mode(size)
screen.fill((blue))

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()

def game_intro():

  intro = True

  while intro:
     for event in pygame.event.get():
         print(event)
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()

    gameDisplay.fill(blue)
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects("BETA", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.draw.rect(gameDisplay, green,(150,450,100,50))
    pygame.draw.rect(gameDisplay, red,(550,450,100,50))

    pygame.display.flip(screen)
    pygame.display.update(screen)
    clock.tick(15)

Problems: 问题:

  1. Indentation error 压痕错误
  2. Your game_intro() function is defined, but never called 您的game_intro()函数已定义,但从未调用
  3. You wrote gameDisplay instead of screen 4 times, also display_width, display_height instead of width, height 您编写了gameDisplay而不是screen 4次,还编写了display_width, display_height而不是width, height
  4. In game_intro() , code after your infinite loop is never called game_intro() ,永远不会调用无限循环之后的代码
  5. pygame.display.update(screen) does not work, it should be pygame.display.update() to update the whole screen. pygame.display.update(screen)不起作用,它应该是pygame.display.update()以更新整个屏幕。 flip(screen) also needs to be changed to flip() flip(screen)也需要更改为flip()
  6. The function text_objects() is not defined but I found the definition in the example you probably copied the code from :) 没有定义函数text_objects()但是我在示例中找到了定义,您可能从:)复制了代码

Corrected code: 更正的代码:

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_intro():

  screen.fill(blue)

  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects("BETA", largeText)
  TextRect.center = ((width/2),(height/2))
  screen.blit(TextSurf, TextRect)

  pygame.draw.rect(screen, green,(150,450,100,50))
  pygame.draw.rect(screen, red,(550,450,100,50))

  pygame.display.flip()
  pygame.display.update()
  clock.tick(15)

  intro = True

  while intro:
     for event in pygame.event.get():
         print(event)
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()


game_intro()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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