2014-07-18 3 views
-1

그래서 파이 게임을 사용하는 게임 코드의 일부입니다. 나는 임의의 플랫폼을 (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

mainloop에서'update()'를 만드는 이유 - 한 번만 (while 'done'이전에) 생성하십시오. 'update()'에서'screen '을 만드는 이유는 - 한 번만 ('done while'이전에) 생성하십시오. – furas

+0

'play()'안에'platform_generator()'를 왜 만드나요? – furas

+0

플랫폼과 위치를 튜플'(플랫폼, 위치)'로 유지하거나'[플랫폼, 위치]'를'PLATFORM '한 목록에 나열 할 수 있습니다. – furas

답변

0

나는이 예제를 실행할 수 없습니다하지만 당신은

screen.blit(position, surface) 

사용 blit()

한 실수를 볼 수 있지만

screen.blit(surface, position) 

읽기 파이 게임 문서는 blit() 기대 : pygame.Surface.blit()

관련 문제