2017-01-02 7 views
0

私はPygameで単純なplatformerゲームを作ろうとしていて、レベルをどのように見せたいかの基本的な概要を作成しました。しかし、文字を動かすためには、画面を色で塗りつぶす必要があります。この色はマップと重なります。私はどのように進めるべきですか?Pygameのblitに関する問題

マイコード:

import pygame 
from pygame import * 
import sys 

WIN_WIDTH = 680 
WIN_HEIGHT = 500 


DISPLAY = (WIN_WIDTH, WIN_HEIGHT) #variable for screen display 
DEPTH = 32 #standard 
FLAGS = 0 #standard 
RED = (0, 0, 255) 

class Hero(): 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 


    def appearance(self): 
     return pygame.image.load('C:\\Users\\admin\\Desktop\\Player1.png') 

    def move_right(self): 
     self.x += 10 
     return self.x 


    def move_left(self): 
     self.x -= 10 
     return self.x 





player = Hero(56, 420) 
player_img = player.appearance() 

x = 0 
y = 0 

platformx = 0 
platformy = 0 
WHITE = (255, 255, 255) 
pygame.init() 
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) 
screen.fill(WHITE) 
pygame.display.set_caption("Rum Islands") 
timer = pygame.time.Clock() 


level = [ 
     "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", #45 x 25 
     "P           P", 
     "P           P", 
     "P           P", 
     "P     PPPPPPPPPPP   P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "P PPPPPPPP        P", 
     "P           P", 
     "P       PPPPPPP   P", 
     "P     PPPPPP     P", 
     "P           P", 
     "P   PPPPPPP       P", 
     "P           P", 
     "P      PPPPPP    P", 
     "P           P", 
     "P PPPPPPPPPPP       P", 
     "P           P", 
     "P     PPPPPPPPPPP    P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",] 






pygame.key.set_repeat(10,10) 

while True: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

     elif event.type == pygame.KEYDOWN: 

      if event.key == pygame.K_LEFT: 
       x = player.move_left() 


      elif event.key == pygame.K_RIGHT: 
       x = player.move_right() 

    screen.fill(WHITE)    
    for row in level: 
     for col in row: 
      if col == "P": 
       pygame.draw.rect(screen, RED, (platformx, platformy, 40, 20)) 
      platformx += 15 
     platformy += 20 
     platformx = 0 



    screen.blit(player_img, (x, y)) 
    pygame.display.update() 
+0

あなたのマップが(pygame.draw.rect()を使って)すべてのウィンドウを埋めるなら、 'fill()'を使う必要はありません。だから、最初に 'fill(WHITE)'を使わないでコードを試してください) – furas

+0

、またはマップを描画する前に 'fill(WHITE)'を使うだけです。 - すべてのチュートリアルで見られるように。 – furas

+0

ところで 'while = while'ループの中に 'level = ... 'を割り当てる必要はありません。' while'の前に行うことができます。 – furas

答えて

2

あなたのコードを修正した後。

問題をplatformyに設定したことがあります。

import pygame 

# --- constants --- 

WIN_WIDTH = 680 
WIN_HEIGHT = 500 

DISPLAY = (WIN_WIDTH, WIN_HEIGHT) #variable for screen display 
DEPTH = 32 #standard 
FLAGS = 0 #standard 

RED = (0, 0, 255) 
WHITE = (255, 255, 255) 

# --- classes --- 

class Hero(): 

    def __init__(self, x, y): 
     self.image = pygame.image.load('C:\\Users\\admin\\Desktop\\Player1.png') 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

    def move_right(self): 
     self.rect.x += 10 

    def move_left(self): 
     self.rect.x -= 10 

    def draw(self, screen): 
     screen.blit(self.image, self.rect) 

# --- main --- 

level = [ 
     "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", #45 x 25 
     "P           P", 
     "P           P", 
     "P           P", 
     "P     PPPPPPPPPPP   P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "P PPPPPPPP        P", 
     "P           P", 
     "P       PPPPPPP   P", 
     "P     PPPPPP     P", 
     "P           P", 
     "P   PPPPPPP       P", 
     "P           P", 
     "P      PPPPPP    P", 
     "P           P", 
     "P PPPPPPPPPPP       P", 
     "P           P", 
     "P     PPPPPPPPPPP    P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "P           P", 
     "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", 
] 

# - init - 

pygame.init() 
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) 

pygame.display.set_caption("Rum Islands") 
pygame.key.set_repeat(10,10) 

# - objects - 

player = Hero(56, 420) 

# - mainloop - 

timer = pygame.time.Clock() 

while True: 

    # - events - 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

     elif event.type == pygame.KEYDOWN: 

      if event.key == pygame.K_LEFT: 
       player.move_left() 

      elif event.key == pygame.K_RIGHT: 
       player.move_right() 

    # - updates - 

    # - draws - 

    screen.fill(WHITE) 

    platform_x = 0 
    platform_y = 0 

    for row in level: 
     for col in row: 
      if col == "P": 
       pygame.draw.rect(screen, RED, (platform_x, platform_y, 40, 20)) 
      platform_x += 15 
     platform_y += 20 
     platform_x = 0 

    player.draw(screen) 
    pygame.display.update() 

    timer.tick(25)