gpt4 book ai didi

python - Pygame 显示不更新

转载 作者:行者123 更新时间:2023-12-01 06:27:48 25 4
gpt4 key购买 nike

我正在 pygame 中制作游戏,但显示不更新。我正在使用 update() 函数,并在循环开始时使用 fill() 清除屏幕。 Pygame 的加载速度也不是很快;一旦 pygame 应用程序显示在我的 Mac 上正在运行的应用程序中,我就必须在 pygame 应用程序上点击“退出”才能显示屏幕。

"Module Imports"
import pygame as pg
import shelve

"Import Classes"
from Settings import Settings
from Settings import Screen
from Settings import Save
from Random import Random
from Input import Input
from Input import Button
# Init Classes
Settings = Settings()
Screen = Screen()
Random = Random()
Save = Save()

on = True

pg.init()

event_const = pg.event.wait()

save_file = shelve.open("/Volumes/HISTORYGAME/Game_Saves/Save_File")

clock = pg.time.Clock()
MainDisplay = pg.display.set_mode((Screen.width, Screen.height))
pg.display.set_caption(Settings.title)
pg.display.init()


class Mouse:
def __init__(self):
self.left = False
self.right = False


Mouse = Mouse()


def begin_game():
players = []
num_players = input("How Many Players (enter int): ")

save_file["num_players_var"] = num_players

for pl in range(num_players):
players.append(raw_input("Enter Player Name: "))

save_file["players_var"] = players

print(players)


def intro():
intro = True

for event in pg.event.get():
if event.get == pg.QUIT:
intro = False
on = False
if event.get == pg.event.KEYDOWN:
pass


# begin_game()

test_button = Button(0, 0, 200, 200, "Enter Text Here", MainDisplay)
buttons = []
buttons.append(test_button)

loops = 0

test_x = 10
test_y = 10

while on:
MainDisplay.fill((0, 0, 0))
for event in pg.event.get():
if event.type == pg.QUIT:
On = False
pg.quit()
exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
On = False
pg.quit()
exit()
if event.key == pg.K_RIGHT:
test_x += 10
print("right")
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
Mouse.left = True
else:
Mouse.left = False
if event.type == pg.MOUSEBUTTONUP:
if event.button == 1:
Mouse.left = False

for button in buttons:
button.draw(pg.mouse.get_pos(), event_const)
if button.clicked is True:
print("True \n" * 100)

pg.draw.line(MainDisplay, (255, 0, 0), (100, 100), (600, 600))
pg.draw.rect(MainDisplay, (255, 0, 0), (test_x, test_y, 20, 20))

pg.display.flip()
pg.display.update()
clock.tick(Settings.FPS)
loops += 1
print(str(loops))

最佳答案

我怀疑等待事件或使用 input() 读取 stdin 会导致锁定问题。但在所提供的代码中,对此的调用已被注释掉。不可能真正知道。

这个版本的代码,带有重新创建的 Button 类,工作正常,但显然它没有任何引用的类。

"Module Imports"
import pygame
#import shelve

"Import Classes"
#from Settings import Settings
#from Settings import Screen
#from Settings import Save
#from Random import Random
#from Input import Input
#from Input import Button
# Init Classes
#Settings = Settings()
#Screen = Screen()
#Random = Random()
#Save = Save()

WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600

class Mouse:
def __init__(self):
self.left = False
self.right = False

class Button:
def __init__( self, x, y, width, height, label, window ):
self.rect = pygame.Rect( x, y, width, height )
self.clicked = False
self.window = window
self.font = pygame.font.Font( None, 20 )
self.label = self.font.render( label, True, ( 255, 255, 255 ) ) # large white text

def draw( self, mouse_pos ):
if ( self.rect.collidepoint( mouse_pos ) ):
colour = ( 0, 200, 0 ) # green if mouse over
else:
colour = (0, 0, 200) # blue otherwise
pygame.draw.rect( self.window, colour, self.rect, 0 ) # filled rectangle
# centred text
label_rect = self.label.get_rect()
x_offset = ( self.rect.width - label_rect.width ) // 2
y_offset = ( self.rect.height - label_rect.height ) // 2
self.window.blit( self.label, ( x_offset, y_offset ) )

def checkClick( self, mouse_pos ):
if ( self.rect.collidepoint( mouse_pos ) ):
self.clicked = True
else:
self.clicked = False


def begin_game():
players = [ "Mork", "Mindy", "E.T." ] # test data
#players = []
#num_players = input("How Many Players (enter int): ")
#save_file["num_players_var"] = num_players
#for pl in range(num_players):
# players.append(raw_input("Enter Player Name: "))
#save_file["players_var"] = players
print(players)




# Initialisation
pygame.init()
pygame.font.init()
#event_const = pygame.event.wait()
#save_file = shelve.open("/Volumes/HISTORYGAME/Game_Saves/Save_File")
clock = pygame.time.Clock()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption( "Settings.title" )
pygame.display.init()



Mouse = Mouse()
begin_game()
test_button = Button(0, 0, 200, 200, "Enter Text Here", window)
buttons = []
buttons.append(test_button)

loops = 0
test_x = 10
test_y = 10

on = True
while on:
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
on = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
on = False
elif event.key == pygame.K_RIGHT:
test_x += 10
print("right")
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
Mouse.left = True
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
Mouse.left = False
for button in buttons:
button.checkClick( event.pos ) # buttons actuate on mouse-up

for button in buttons:
button.draw( pygame.mouse.get_pos() ) #, event_const)
if button.clicked is True:
print("True \n" * 5) # 100

pygame.draw.line(window, (255, 0, 0), (100, 100), (600, 600))
pygame.draw.rect(window, (255, 0, 0), (test_x, test_y, 20, 20))

pygame.display.flip()
#pygame.display.update() # only need update() or flip()
clock.tick( 60 ) #Settings.FPS)
loops += 1
#print(str(loops)) # Too noisy

# exiting
pygame.quit()

关于python - Pygame 显示不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60049637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com