2011-09-14 8 views
4

:) 이것은 내 첫 번째 질문이며 영어는 그렇게 좋지 않으므로 나와 함께하시기 바랍니다.파이 게임에서 사각형 격자 만들기

글쎄, 나는 파이 게임에서 8x8 격자를 클릭 할 필요가있다. 는 지금은 이런 일이 :

#!/usr/bin/python2 
#------------------------------------------------------------------------------- 
# Imports & Inits 
import pygame, sys 
from pygame.locals import * 
pygame.init() 
#------------------------------------------------------------------------------- 
# Settings 
WIDTH = 105 
HEIGHT = 105 
FPS = 60 
#------------------------------------------------------------------------------- 
# Screen Setup 
WINDOW = pygame.display.set_mode([WIDTH,HEIGHT]) 
CAPTION = pygame.display.set_caption('Test') 
SCREEN = pygame.display.get_surface() 
TRANSPARENT = pygame.Surface([WIDTH,HEIGHT]) 
TRANSPARENT.set_alpha(255) 
TRANSPARENT.fill((255,255,255)) 
#------------------------------------------------------------------------------- 
# Misc stuff 
rect1 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,0, 50, 50)) 
rect2 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,55, 50, 50)) 
rect3 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,0, 50, 50)) 
rect4 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,55, 50, 50)) 

... 

#------------------------------------------------------------------------------- 
# Refresh Display 
pygame.display.flip() 
#------------------------------------------------------------------------------- 
# Main Loop 
while True: 
    pos = pygame.mouse.get_pos() 
    mouse = pygame.draw.circle(TRANSPARENT, (0, 0, 0), pos , 0) 
    # Event Detection--------------- 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 
     elif event.type == MOUSEBUTTONDOWN: 
      if rect1.contains(mouse): 
       rect1 = pygame.draw.rect(SCREEN, (155, 155, 155), (0,0, 50, 50)) 
       pygame.display.flip() 

자, 내 원래의 코드에서, 내가 훨씬 더 사각형이와 나는 같은 것을 할 수있는 방법이 필요합니다, 물론 내 솔루션을

for i in rectangles: 
    if i hasbeenclickedon: 
      change color 

을 너무 정적이다. 그럼 어떻게 할 수 있습니까? 솔루션이 실제로 조금 복잡하지만

답변

4

, 내가 처음 의도 한대로

rectangles = (rect1, rect2, ...) 

다음 당신이 그들을 반복 할 수 있습니다 말하고 싶지만.

pos = pygame.mouse.get_pos() 
for rect in rectangles: 
    if rect.collidepoint(pos): 
     changecolor(rect) 

같은

해보십시오 STH 당신은 물론, changecolor 메소드를 구현해야 할 것이다.

일반적으로 changecolor 메서드를 정의하는 클릭 가능한 입력란의 클래스를 만드는 것이 좋습니다.

+0

하하, 이제 당신이 그것을 지적하는 것이, 너무 분명하다. 대단히 감사합니다. :) –

+0

반갑습니다. Btw. 이제 당신이 실제로 64 개의 직사각형을 그리는 것을 보았습니다. 루프를 사용하여 드로잉하고 동시에 '사각형'목록에 추가하는 것이 좋습니다. –

+0

도움이되고 더 이상 도움이되지 않을 것으로 예상되는 경우 대답을 수락 할 수 있습니다. ;) –

0

간단한 '인간'색상 :

Color("red") 
Color(255,255,255) 
Color("#fefefe") 

사용 :

import pygame 
# This makes event handling, rect, and colors simpler. 
# Now you can refer to `Sprite` or `Rect()` vs `pygame.sprite.Sprite` or `pygame.Rect()` 
from pygame.locals import * 
from pygame import Color, Rect, Surface 

pygame.draw.rect(screen, Color("blue"), Rect(10,10,200,200), width=0) 
pygame.draw.rect(screen, Color("darkred"), Rect(210,210,400,400), width=0) 
관련 문제