我有一个关于在游戏中放置长方形的小问题。当我运行代码时,我看不到矩形。有人知道怎么解决这个问题吗?
import pygame as pg
from pygame.locals import *
pg.init()
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
size = width, height = (800, 800)
screen = pg.display.set_mode(size)
pg.display.set_caption("Ball Game")
screen.fill((10, 255, 255))
pg.display.update()
running = True
clock = pg.time.Clock()
#board = pg.draw.rect(screen, BLACK, pg.Rect(30, 30, 60, 60))
#board_loc = pg.
#board_loc.center = width/2, height*0.8
background_image = pg.image.load("pngtree-blue-cartoon-minimalist-planet-surface-starry-sky-main-map-background-image_186868.jpg").convert()
while running:
for event in pg.event.get():
if event.type == QUIT:
running = False
screen.blit(background_image, [0, 0])
pg.display.flip()
#screen.blit(board, board_loc)
rect = pg.Rect(0, 0, 200, 100)
rect.center = (300, 300)
pg.draw.rect(screen, BLACK, rect)
clock.tick(30)
pg.quit()发布于 2022-05-13 14:06:41
绘制矩形后,必须更新显示:
rect = pg.Rect(0, 0, 200, 100)
rect.center = (300, 300)
while running:
for event in pg.event.get():
if event.type == QUIT:
running = False
# draw background
screen.blit(background_image, [0, 0])
# draw rectangle on top of the background
pg.draw.rect(screen, BLACK, rect)
# update display
pg.display.flip()您实际上是在Surface对象上绘图。如果在与PyGame显示相关联的曲面上绘图,这在显示中不会立即可见。当使用pygame.display.update()或pygame.display.flip()更新显示时,更改将变得可见。
https://stackoverflow.com/questions/72230875
复制相似问题