所以这是我的游戏代码的一部分,使用pygame。我正在尝试生成随机平台(来自3个不同的选项),将它们存储在列表中,然后将列表中的所有平台blit到屏幕。我能够正确生成平台形状,但除了第一个平台之外,我无法将它们定位在我想要的位置。
blue = (0, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
i = 0
c = 0
Done = True
globalplatpos = pygame.Surface([395, 30])
globalplat = globalplatpos.get_rect()
platform_dimensions = plattop, platleft, platw, plath = 0,0,0,0
def play():
#============GAME SETUP============
SIZE = WIDTH, HEIGHT = 800, 600
TITLE = "Duality"
SPEED = 10
JUMPHEIGHT = 300
JUMPCOUNT = 0
JUMPSPEED = 15
GRAVITY = 10
STANDING = 1
JUMPING = 0
globalplatpos.fill(red)
platform = globalplat
platform.bottom = HEIGHT
PLATFORM = []
PLATPOS = []
platstand = True
screen = pygame.display.set_mode(SIZE)
caption = pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
mainsprite = pygame.image.load("images\mainsprite.png")
mainchar = mainsprite.get_rect()
mainchar.left = 177.5
mainchar.bottom = 570
mirrsprite = pygame.image.load("images\mirrsprite.png")
mirrchar = mirrsprite.get_rect()
mirrchar.left = mainchar.left + 400
mirrchar.bottom = mainchar.bottom
#============GAME SETUP============
#============PLATFORM GENERATOR============
def platform_generator(platform):
global globalplat
global globalplatpos
global platform_dimensions
globalplat = platform.move(0,-60)
globalplatpos.fill(red)
lastplat = PLATFORM[len(PLATFORM) - 1]
platheight = lastplat.top
leftpos = pygame.Surface([131, 30])
leftplat = leftpos.get_rect()
centrepos = pygame.Surface([100, 30])
centreplat = centrepos.get_rect()
rightpos = pygame.Surface([131, 30])
rightplat = rightpos.get_rect()
plat_type = random.randrange(0,3)
if plat_type == 0:
globalplat = leftplat
globalplatpos = leftpos
platform_dimensions = int(globalplat.top + 290), 0, 131, 30
elif plat_type == 1:
globalplat = centreplat
globalplatpos = centrepos
platform_dimensions = int(globalplat.top + 290), 132, 100, 30
elif plat_type == 2:
globalplat = rightplat
globalplatpos = rightpos
platform_dimensions = int(globalplat.top + 290), 233, 131, 30
else:
pass
PLATFORM.append(globalplat)
PLATPOS.append(globalplatpos)
#============PLATFORM GENERATOR============
#============GAME LOOP============
Done = False
while not Done:
clock.tick(60)
fps = clock.get_fps()
print(fps)
platform = globalplat
platpos = globalplatpos
mirrchar.left = mainchar.left + 406
mirrchar.bottom = mainchar.bottom
def update():
screen = pygame.display.set_mode(SIZE)
screen.fill(blue)
screen.blit(mainsprite, mainchar)
screen.blit(mirrsprite, mirrchar)
listpos = 0
pos = PLATPOS[listpos]
platshape = pygame.Rect(platform_dimensions)
platform = pygame.draw.rect(screen, red, platshape, 0)
platpos = globalplatpos
PLATFORM.append(platform)
PLATPOS.append(platpos)
for form in PLATFORM:
pos = PLATPOS[listpos]
listpos += 1
screen.blit(pos, form)
divpos = pygame.Rect(395, 0, 10, HEIGHT)
divrect = pygame.draw.rect(screen, black, divpos, 0)
pygame.display.update()
global i
if i == 0:
globalplat.bottom = HEIGHT
i = 1
PLATFORM.append(globalplat)
PLATPOS.append(globalplatpos)
screen.blit(globalplatpos, globalplat)
elif i == 1 and len(PLATFORM) < 10:
platform_generator(platform)
plat1 = PLATFORM[0]
update()
elif plat1.top > HEIGHT:
plat1 = PLATFORM[0]
pos1 = PLATPOS[0]
del plat1
del pos1
else:
update()
if mainchar.left > 0: #MOVE LEFT
if pygame.key.get_pressed()[K_LEFT] or pygame.key.get_pressed()[K_a]:
mainchar.left -= SPEED
else:
mainchar.left = 0
if mainchar.right < 395: # MOVE RIGHT
if pygame.key.get_pressed()[K_RIGHT] or pygame.key.get_pressed()[K_d]:
mainchar.right += SPEED
else:
mainchar.right = 395
jump = pygame.key.get_pressed()[K_SPACE] or pygame.key.get_pressed()[K_UP]
platstand = mainchar.collidelist(PLATFORM)
for form in PLATFORM:
if mainchar.colliderect(form):
STANDING = 1
mainchar.bottom = form.top
if JUMPING == 0:
if mainchar.collidelist(PLATFORM) > -1:
STANDING = 1
if STANDING == 1:
if jump:
JUMPING = 1
if JUMPING == 1:
if JUMPCOUNT < JUMPHEIGHT/2:
mainchar.bottom -= JUMPSPEED
mirrchar.bottom -= JUMPSPEED
JUMPCOUNT += JUMPSPEED
elif JUMPCOUNT > JUMPHEIGHT/2 and JUMPCOUNT < JUMPHEIGHT * 0.75:
mainchar.bottom -= JUMPSPEED/2
mirrchar.bottom -= JUMPSPEED/2
JUMPCOUNT += JUMPSPEED
elif JUMPCOUNT > JUMPHEIGHT * 0.75 and JUMPCOUNT < JUMPHEIGHT:
mainchar.bottom -= JUMPSPEED/4
mirrchar.bottom -= JUMPSPEED/4
JUMPCOUNT += JUMPSPEED
else:
JUMPCOUNT = 0
JUMPING = 0
STANDING = 0
jump = False
if STANDING == 0:
mainchar.bottom += GRAVITY
mirrchar.bottom += GRAVITY
def gameover():
Done = True
if mainchar.top > HEIGHT:
gameover()
if pygame.key.get_pressed()[K_ESCAPE]:
escape()
for event in pygame.event.get():
if event.type == pygame.QUIT: Done = True
update()
#============GAME LOOP============
答案 0 :(得分:0)
我无法运行此示例,但我在blit()
您使用
screen.blit( position, surface )
但blit()
需要
screen.blit( surface, position )
阅读PyGame文档:pygame.Surface.blit()