问题描述
我目前只是在使用 Pygame 显示器测试水域,因为我对这个模块非常陌生。
代码如下:
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0,0)
black = (0,0)
white = (255,255,255)
green = (44,215,223)
red = (255,67,34)
blue = (77,77,77)
count = 0
running = True
Box1_image = True
Box3_image = True
background = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/background.jpg')
dirty_spot = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/dirty.png')
background_resized = pygame.transform.scale(dirty_spot,(display_width,display_height))
background_rect = background_resized.get_rect()
gamedisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot,(200,200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex,imagey):
gamedisplay.blit(dirty_spot_resized,(imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
#if Box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
pygame.display.update()
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gamedisplay.blit(dirty_rect,(0,0))
rects.append(pygame.Rect(0,200,200))
gamedisplay.fill(white)
clock.tick(30)
count += 1
pygame.quit()
quit()
Box1_image
和 Box1_image
是布尔变量,用于标记从另一个函数传递的某些条件。
但是我通过使用 print("Test 2")
进行了一个简单的测试,使上述变量得以通过。但是,当它尝试 blit dirty_rect
时。 pygame 显示没有任何变化。
资产(如果需要): background.jpg dirty.png
我可以检查缺少什么以正确“移除/删除”dirty_spot
blit?提前致谢。
解决方法
喜欢吗? (我把FPS改成5了所以需要一段时间才能改)
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0,0)
black = (0,0)
white = (255,255,255)
green = (44,215,223)
red = (255,67,34)
blue = (77,77,77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('background.jpg')
dirty_spot = pygame.image.load('dirty.png')
background_resized = pygame.transform.scale(background,(display_width,display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot,(200,200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex,imagey):
gameDisplay.blit(dirty_spot_resized,(imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
gameDisplay.fill(white)
#if box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gameDisplay.blit(dirty_rect,(0,0))
rects.append(pygame.Rect(0,200,200))
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()
我认为您误解了一些事情。您可以将一个图像或表面 blit 到另一个顶部,然后像这样构建内容,然后将它们 blit 到显示器上,最后更新显示器。
你有这条线:
background_resized = pygame.transform.scale(dirty_spot,display_height))
我认为应该是背景而不是dirty_spot。
我把对 display.update() 的调用移出了 if 循环,因为你最后调用了 display.update()。
,import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0,display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot,200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
show_dirt = False
def dirtyspot1(imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
if event.key == pygame.K_d:
show_dirt = not show_dirt
if event.key == pygame.K_UP:
rect_dirty_spot.y -= 5
if event.key == pygame.K_DOWN:
rect_dirty_spot.y += 5
if event.key == pygame.K_RIGHT:
rect_dirty_spot.x += 5
if event.key == pygame.K_LEFT:
rect_dirty_spot.x -= 5
gameDisplay.blit(background_resized,0))
if show_dirt:
gameDisplay.blit(dirty_spot_resized,rect_dirty_spot)
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()