2017-11-27 24 views
1

최근에 기본 rect 및 이미지를 Class 시스템으로 변경하여 작업중인 코드를 업그레이드하려고했습니다. 그러나 내 충돌 감지에 문제가있는 것 같습니다. floor (또는 입력 한 box)과 충돌 할 때 내 스프라이트 hg이 낙하하지 말라고 말하면 감지하지 못하지만 스크립트가 실행 중입니다. 구문이 잘못되었습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?파이 게임에서 스프라이트에 충돌 감지 기능이 작동하지 않습니다.

######## basic setup 
import pygame, sys, time, random, threading, tkinter, ctypes 
from threading import Timer 
from pygame.locals import * 
from tkinter import * 
pygame.init() 
WINDOWHEIGHT = 720 
WINDOWWIDTH = 1280 
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) 
pygame.display.set_caption('Hitman Grandma | vB1.0 (prealpha)') 
white = (255,255,255) 
red = (255,0,0) 
black = (0,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
cyan = (0,255,255) 
lightgrey = (198,198,198) 
windowSurface.fill(lightgrey) 
pygame.display.update() 
mainClock = pygame.time.Clock() 
########## variables 
level = 0 
touching = False 
global x_speed 
x_speed = 0 
y_speed = 0 
leftallowed = True 
rightallowed = True 
hgturnright = True 
hgjumpallowed = True 
########### the grandma d'awesome murder sorts 
hgimage = pygame.image.load('hgfinal.png') 
hgimage.convert_alpha() 
class HG(object): 
    def __init__(self,x,y,image): 
     self.image = image 
     self.rect = self.image.get_rect() 
     self.x = x 
     self.y = y  
    def draw(self): 
     windowSurface.blit(self.image,(self.x,self.y)) 
    def move(self): 
     self.x += x_speed 
     self.y += y_speed 
    def topcollide(self,box): 
     if not self.rect.colliderect(box.rect): 
      global y_speed 
      if y_speed < 20: 
       y_speed += 1 
      elif y_speed == 20: 
       y_speed = 20 
      print('shhoooo') 
     elif self.rect.colliderect(box.rect): 
      y_speed = 0 
      print('flop') 
hg = HG(0,0,hgimage) 
########### land and boundary 
lands = pygame.image.load('hgland1.png') 
floorland = pygame.transform.scale(lands,(1280,50)) 
sideedge = pygame.Rect(0,0,1,720),pygame.Rect(1279,0,1,720) 
topedge = pygame.Rect(0,0,1280,1) 
class Floor(object): 
    def __init__(self,x,y,image): 
     self.image = image 
     self.x = x 
     self.y = y 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 
    def draw(self): 
     windowSurface.blit(self.image,(self.x,self.y)) 
class Ground(object): 
    def __init__(self,x,y,image): 
     self.image = image 
     self.x = x 
     self.y = y 
     self.rect = self.image.get_rect() 
    def draw(self): 
     windowSurface.blit(self.image,(self.x,self.y)) 
floor = Floor(0,670,floorland) 
########### WHILE 
while True: 
########### background 
    windowSurface.fill(lightgrey) 
########### hg movement 
    for event in pygame.event.get(): 
     if event.type == KEYDOWN: 
       if event.key == K_LEFT and hg.x > 0 and leftallowed: 
        x_speed = -4 
        hgturnright = False 
       if event.key == K_RIGHT and (hg.x + 36) < WINDOWWIDTH and rightallowed: 
        x_speed = 4 
        hgturnright = True 
       if event.key == K_UP and hgjumpallowed: 
        y_speed = -17 
     if event.type == KEYUP: 
      if event.key == K_RIGHT: 
       x_speed = 0 
      if event.key == K_LEFT: 
       x_speed = 0 
     if event.type == KEYDOWN: 
########### ctrl+q 
      if event.key == K_q and pygame.key.get_mods() & pygame.KMOD_CTRL: 
       pygame.quit() 
       sys.exit() 
       exit 
########### [x] 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       sys.exit() 
       exit 
########### drawing and .move() 
    floor.draw() 
    hg.draw() 
    hg.move() 
    hg.topcollide(floor) 
########### technicals 
    pygame.display.update() 
    mainClock.tick(40) 

character picture floor picture

+2

:) (colliderect하지 않고'경우 : HG 예 (rect.topleft 또는 rect.center)가 충돌 검출에 사용 있기 때문에,의 RECT 엘 만약 colliderect() :'당신이 할 수있는'if colliderect() : else :'같은'colliderect()'를 두번 검사 할 필요가 없기 때문에. – furas

+1

'print()'를 사용하여 변수의 값과 코드의 일부분을 검사합니다. 문제를 찾는 데 도움이됩니다. – furas

+0

예, 충돌 감지에 문제가 발생하면 일반적으로 관련 객체의 'rect'를 인쇄하는 데 도움이됩니다. – skrx

답변

2

당신의 위치를 ​​업데이트 할 필요가 : 여기 난 당신이 (당신이 더 많은 정보가 필요하면 말해)가 필요합니다 충분히 당신을 도울 코드와이 사진을 믿는 것입니다 BTW

class HG(object): 
    def __init__(self,x,y,image): 
     self.image = image 
     self.rect = self.image.get_rect(topleft=(x, y)) 
     self.x = x 
     self.y = y 

    def move(self): 
     self.x += x_speed 
     self.y += y_speed 
     self.rect.topleft = (self.x, self.y) 
+0

도움을 주셔서 감사합니다! 당신은 내 질문에 전에 대답했고 당신은 항상 옳았습니다! 내 'rect'위치를 업데이트하지 않는다고 생각하지 않는다. – Maximosaur

+1

여러분을 환영합니다! – skrx