2017-01-22 4 views
1

私のpygameでは、私はforループで描かれたカップルの四角形を持っています。私は円のオブジェクトによって表されるプレーヤーを持っています。それは次のようになります。どの矩形に円が含まれているかを確認するにはどうすればよいですか? (Pygameで)

enter image description here

ので、プレイヤーは、これが彼らのキーごとに一つの場所を移動し、矢印キーで移動することができます。最終的に、プレイヤーはこれらの長方形の外に移動することはできません。したがって、移動するたびに、私はチェックしたい: (任意の描画された矩形にプレーヤーが含まれている場合):Trueを返し、そうでなければFalseを返します。

これを行う簡単な方法はありますか?または私は私のアプローチを完全に変えるべきですか?

このような長方形の外側のスポットに移動することはできないはずです。 enter image description here

だけで、このような長方形で: enter image description here

これは私が今持っている。この例のコードであります:

import pygame 

pygame.init() 

#grid 
w = 25 
h = 25 
m = 2 

size = (550, 700) 
screen = pygame.display.set_mode(size) 
screen.fill((255, 255, 255)) 


class Player: 
    def __init__(self): 
     self.x = 149 
     self.y = 14 
     self.r = 10 

    def draw(self): 
     pygame.draw.circle(screen, (255, 0, 0), (self.x, self.y), self.r) 

    def update(self,event): 
      if event.key == pygame.K_LEFT: 
       screen.fill((250, 250, 250)) 
       self.x -= 27 
       self.draw() 
      elif event.key == pygame.K_RIGHT: 
       screen.fill((250, 250, 250)) 
       self.x += 27 
       self.draw() 
      elif event.key == pygame.K_UP: 
       screen.fill((250, 250, 250)) 
       self.y -= 27 
       self.draw() 
      elif event.key == pygame.K_DOWN: 
       screen.fill((250, 250, 250)) 
       self.y += 27 
       self.draw() 

player1 = Player() 
player1.draw() 

done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     elif event.type == pygame.KEYDOWN: 
      player1.update(event) 

     for row in range(5): 
      for col in range(5): 
       if row == 0 or row == 4 or col == 0 or col == 4: 
        pygame.draw.rect(screen, (0,0,0), ((w + m) * col + m, ((h + m) * row + m), w, h), 1) 

    pygame.display.flip() 

ありRect.contains(のRect)関数であるが、1つの長方形ならば、これはそれをチェックします別の矩形を含む基本的に次のような関数がありますか?既存のすべての矩形を選択し、それらが含まれているかどうかをチェックします(選択された矩形)。

すべてのヒントやヘルプは本当に感謝しています。また、これが完全に間違ったアプローチであるかどうかを教えてください。

ご挨拶。

+0

'あなたがrectangle'' 'としてcircle'を扱うことができるように' python.Rect'オブジェクトを返しますpygame.draw.circle'。あなたは 'x'の代わりに' row'、 'col'を使うことができますし、' y'は 'collision 'をチェックすることができます。 – furas

+1

しかし、すべてのコードに小さな間違いが1つあります。すべての四角形の位置をリストに保持しないので、サークル位置の位置を確認することはできません。 – furas

+0

ところで、コードを読みやすくするために、 'pygame.init()'の前にすべてのクラスと関数を保持することができます - [simple template](https://github.com/furas/python-examples/blob/master/pygame/)を参照してください。 __templates __/1__simple__.py) – furas

答えて

1

circlerectangleとして扱うことができます。すべてのオブジェクトを長方形として扱うと、衝突をチェックする計算(CPUパワー)が少なくて済みます。

あなたはこのリストでサークルの位置を確認することができるように、すべての長方形でリストを作成する必要があります - そして、あなたはところでcircle_rect.colliderect(some_rect)

import pygame 

# --- constants --- 

#grid 
W = 25 
H = 25 
M = 2 

SIZE = (550, 700) 

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

FPS = 25 
# --- classes --- 

class Player: 

    def __init__(self): 
     # to keep position and size 
     self.rect = pygame.Rect(0, 0, 20, 20) 

     # set start position 
     self.rect.center = 149, 14 

     self.r = 10 

    def draw(self): 
     pygame.draw.circle(screen, RED, self.rect.center, self.r) 

    def update(self, event): 
     # create copy of position 
     newrect = self.rect.copy() 

     # move "copy" to new position 
     if event.key == pygame.K_LEFT: 
      newrect.x -= 27 
     elif event.key == pygame.K_RIGHT: 
      newrect.x += 27 
     elif event.key == pygame.K_UP: 
      newrect.y -= 27 
     elif event.key == pygame.K_DOWN: 
      newrect.y += 27 

     # check if "copy" is still in rectangles 
     for rectangle in all_rectangles: 
      if newrect.colliderect(rectangle): 
       # now you can set new position 
       self.rect = newrect 
       # don't check other rectangles 
       break 

# --- main --- 

# - init - 

pygame.init() 

screen = pygame.display.set_mode(SIZE) 
screen_rect = screen.get_rect() 

# - objects - 

player1 = Player() 

# create list with rectangles (but not draw them) 

all_rectangles = [] 

for row in range(5): 
    for col in range(5): 
     if row == 0 or row == 4 or col == 0 or col == 4: 
      all_rectangles.append(pygame.Rect((W + M) * col + M, ((H + M) * row + M), W, H)) 

# - mainloop - 

clock = pygame.time.Clock()  
done = False 

while not done: 

    # - events (without draws) - 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     elif event.type == pygame.KEYDOWN: 
      player1.update(event) 


    # - draw everything in one place - 

    screen.fill(WHITE) 

    for rectangle in all_rectangles: 
     pygame.draw.rect(screen, BLACK, rectangle, 1) 

    player1.draw() 

    pygame.display.flip() 

    # - FPS - keep the same speed on all computers - 

    clock.tick(FPS) 

# - end - 
pygame.quit() 

を使用することができます。あなたがrowcolの代わりに使用することができますx,y矩形の位置と円の位置(および衝突のチェック)を保持し、描画するときにrow/colx/yに変換するだけです。

all_rectangles = [ 
    "######", 
    "# #", 
    "# #", 
    "# #", 
    "######", 
] 

のような長方形のリストを作成してマップを作成することもできます。


EDIT:

map = [ 
    "######## #######", 
    "#  ####  #", 
    "#  # #  #", 
    "# ######## #", 
    "######  # #", 
    " #  #####", 
    " #   # ", 
    " ############ ", 
] 

all_rectangles = [] 

for r, row in enumerate(map): 
    for c, item in enumerate(row): 
     if item == '#': 
      all_rectangles.append(pygame.Rect((W + M) * c + M, ((H + M) * r + M), W, H)) 

enter image description here

+0

徹底的に行った後、プレイヤーを移動する前に移動を比較したように、すべてのポジションのリストを作成しました。ありがとう、私は永遠に感謝しています! – Noob17

+1

@ Noob17 [その他の修正](https://github.com/furas/python-examples/tree/master/pygame/example-maze)を参照してください。 – furas

関連する問題