如何在此代码中添加背景而不会出错?
how to add a background in this code without errors?
def game():
running = True
while running:
screen.fill((0, 0, 0))
mx, my = pygame.mouse.get_pos()
button_3 = pygame.Rect(300, 100, 1300, 700)
if button_3.collidepoint((mx, my)):
if click:
begin()
draw_text('game', font, (255, 255, 255), screen, 20, 20)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
# set the pygame window name
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
LeftButton = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if e.type == MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
screen.fill(0)
for i in range(len(images)):
screen.blit(images[i], img_rects[i])
pygame.display.flip()
pygame.time.delay(30)
screen.blit(BACKGROUND_FASE, (0, 0))
pygame.display.update()
mainClock.tick(60)
我想在这个屏幕上添加背景图片,但是每次尝试都出错或者没有出现,请问如何才能不出错地给那部分添加背景,另一部分代码是一个菜单,当我点击开始时会出现这个屏幕,整个代码在这里 https://pastebin.com/Rb7jh2GH
你必须在绘制场景之前 blit
背景,并且你必须在你的游戏循环中绘制它。当你绘制背景时,它会覆盖之前绘制的所有内容:
- 绘制背景
- 绘制场景(所有物体)
- 更新显示
注意绘制背景图时不需要清除背景。如果背景图覆盖了整个window,则无需清除背景。
screen.fill(0)
while running:
# [...]
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
# [...]
# draw background
# screen.fill(0) <--- superfluous
screen.blit(BACKGROUND_FASE, (0, 0))
# draw scene (objects)
for i in range(len(images)):
screen.blit(images[i], img_rects[i])
# update display
pygame.display.flip()
def game():
running = True
while running:
screen.fill((0, 0, 0))
mx, my = pygame.mouse.get_pos()
button_3 = pygame.Rect(300, 100, 1300, 700)
if button_3.collidepoint((mx, my)):
if click:
begin()
draw_text('game', font, (255, 255, 255), screen, 20, 20)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
# set the pygame window name
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
LeftButton = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if e.type == MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
screen.fill(0)
for i in range(len(images)):
screen.blit(images[i], img_rects[i])
pygame.display.flip()
pygame.time.delay(30)
screen.blit(BACKGROUND_FASE, (0, 0))
pygame.display.update()
mainClock.tick(60)
我想在这个屏幕上添加背景图片,但是每次尝试都出错或者没有出现,请问如何才能不出错地给那部分添加背景,另一部分代码是一个菜单,当我点击开始时会出现这个屏幕,整个代码在这里 https://pastebin.com/Rb7jh2GH
你必须在绘制场景之前 blit
背景,并且你必须在你的游戏循环中绘制它。当你绘制背景时,它会覆盖之前绘制的所有内容:
- 绘制背景
- 绘制场景(所有物体)
- 更新显示
注意绘制背景图时不需要清除背景。如果背景图覆盖了整个window,则无需清除背景。
screen.fill(0)
while running:
# [...]
while 1:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
exit(0)
# [...]
# draw background
# screen.fill(0) <--- superfluous
screen.blit(BACKGROUND_FASE, (0, 0))
# draw scene (objects)
for i in range(len(images)):
screen.blit(images[i], img_rects[i])
# update display
pygame.display.flip()