2016-12-27 135 views
0

目標:知道什麼時候一個精靈點擊/主動用鼠標如何檢測鼠標和精靈之間的碰撞/鼠標懸停?

使用:Python的3.2 64位,pygame的1.92 64位,64位Windows 7

我花了6個小時無果......我嘗試:

s.rect.collidepoint(pygame.mouse.get_pos()) 
s.collidepoint(pygame.mouse.get_pos()) 
s.sprite.spritecollide(pygame.mouse.get_pos()) 
s.spritecollide(pygame.mouse.get_pos()) 
s.sprite.collide_rect(pygame.mouse.get_pos()) 
s.collide_rect(pygame.mouse.get_pos()) 

我也試着轉動鼠標的位置,我真的不想做,就像是有人在這裏提到的其他在另一個崗位,爲雪碧和碰撞一樣,具有相同的結果; (

我能夠succ essfully鼠標與圖像相撞,但只要我把圖像變成一個精靈類,它就成了一場噩夢......精靈類有什麼問題?或者我是在浪費時間試圖使用精靈來製作漂亮的碰撞特徵,而只是使用rect碰撞的圖像呢?

保持於獲取AttributeError的: '謝爾頓' 對象沒有屬性 '矩形'(s.Rect.collidepoint) 或AttributeError的: '元組' 對象沒有屬性 'collidepoint'(s.collidepoint) 或AttributeError的: 'Rake'對象沒有屬性'sprite'(s.sprite.collidepoint)

由於我是python/pygame的新手,我應該將這個檢測放在Sprite類本身的Update/Render方法中,或者我使用錯誤的事件輪詢?

我沒有打擾試圖重新編碼鼠標按下/上/拖動,因爲我甚至無法獲得鼠標懸停到

希望這一次的工作後得到一個工作響應......別人沒「T;(

感謝您的幫助

代碼:

import pygame 
from pygame import * 
from pygame.locals import * 
from pygame.sprite import * 

class Sheldon(Sprite): 
    def __init__(self): 
     Sprite.__init__(self) 
     self.image = transform.scale(image.load('sheldon.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

class Rake(Sprite): 
    def __init__(self): 
     Sprite.__init__(self) 
     self.image = transform.scale(image.load('rake.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

class Sprite_Mouse_Location(Sprite): 
    def __init__(self,x,y): 
     Sprite.__init__(self) 
     self.rect = pygame.Rect(x,y,1,1) 
     print(self.rect) 

pygame.init() 
window = display.set_mode((800,600)) 
sheldon = Sheldon() 
sheldon.rect = (10,10) 
all_sprites = Group(sheldon) 
rake = Rake() 
rake.rect = (400,250) 
all_sprites.add(rake) 
x,y = pygame.mouse.get_pos() 
mouse_sprite = Sprite_Mouse_Location(x,y) 
running = True 

while running == True: 
    for event in pygame.event.get(): 
     if event.type == QUIT or event.type == KEYUP and event.key == K_ESCAPE : 
      pygame.quit() 

     elif event.type == MOUSEMOTION : 
      for s in all_sprites : 
       if pygame.sprite.collide_rect(s,mouse_sprite): 
        print("hit") 

    window.fill((0,0,0)) 
    all_sprites.update() 
    all_sprites.draw(window) 
    display.update() 
+0

你必須使用[pygame.Rect()](http://www.pygame.org/docs/ref/rect.html) – furas

+0

我沒有,但它不工作要麼,但也許我有使用元組而不是(x,y,w,h)?我再次嘗試self.rect.collidepoint(x,y),但仍然無法讓它工作...反正它現在是固定的;) –

回答

0

你不需要Sprite_Mouse_Location

BTW:設置位置,你需要

rake.rect.topleft = (400, 250) 

# or 

rake.rect.x = 400 
rake.rect.y = 250 

rake.rect = (400, 250) 

,因爲它與tuple


示例代碼替換pygame.Rect()

我使用Surface而不是image.load(),因此每個人都可以在沒有圖像的情況下運行它。

import pygame 

# --- constants --- (UPPER_CASE names) 

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

# --- classes --- (CamelCase names) 

class Sheldon(pygame.sprite.Sprite): 

    def __init__(self, x, y): 
     pygame.sprite.Sprite.__init__(self) 

     self.image = pygame.Surface((230, 310)) 
     self.image.fill(RED) 

     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

    def check_click(self, mouse): 
     if self.rect.collidepoint(mouse): 
      print("hit RED") 

class Rake(pygame.sprite.Sprite): 

    def __init__(self, x, y): 
     pygame.sprite.Sprite.__init__(self) 

     self.image = pygame.Surface((230, 310)) 
     self.image.fill(GREEN) 

     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

    def check_click(self, mouse): 
     if self.rect.collidepoint(mouse): 
      print("hit GREEN") 

# --- main --- (lower_case names) 

# - init - 

pygame.init() 
window = pygame.display.set_mode((800,600)) 

# - objects - 

sheldon = Sheldon(10, 10) 
#sheldon.rect.topleft = (10, 10) 

rake = Rake(400, 250) 
#rake.rect.topleft = (400, 250) 

all_sprites = pygame.sprite.Group() 
all_sprites.add(sheldon, rake) 

# - mainloop - 

running = True 

while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT or \ 
      (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): 
      running = False 

     elif event.type == pygame.MOUSEBUTTONDOWN: 
      for s in all_sprites: 
       s.check_click(event.pos) 

    window.fill(BLACK) 
    all_sprites.update() 
    all_sprites.draw(window) 
    pygame.display.update() 

# - end -  

pygame.quit() 
+0

謝謝......我將驗證它對我來說是否適用於後者...... –

+0

謝謝......對我更好......我希望我可以直接做一個跟進問題來優化這裏的另一個問題 –

0

有很多的事情,與你的代碼怎麼回事編碼可以在冷杉很難。噸,但要堅持!只要你不斷嘗試,不要放棄,你會更好!你的錯誤發生是因爲循環上面,你正在設置你的各種精靈的矩形變量爲元組而不是矩形。 Rects不是元組,它們有一些元組沒有的變量,而collide_rect需要一些變量。我在下面的代碼中提出了一些關於其他問題的評論。

import pygame 
from pygame import * 
from pygame.locals import * 
from pygame.sprite import * 

class Sheldon(Sprite): 
    def __init__(self): 
     Sprite.__init__(self) 
     self.image = transform.scale(image.load('sheldon.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

class Rake(Sprite): 
    def __init__(self): 
     Sprite.__init__(self) 
     self.image = transform.scale(image.load('rake.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

class Sprite_Mouse_Location(Sprite): 
    def __init__(self,x,y): 
     Sprite.__init__(self) 
     self.rect = pygame.Rect(x,y,1,1) 
     print(self.rect) 

pygame.init() 
window = display.set_mode((800,600)) 
sheldon = Sheldon() 
sheldon.rect = (10,10) # (10,10) is not a rect! 
         # pygame.Rect(0,0,10,10) is a rect! 
all_sprites = Group(sheldon) 
rake = Rake() 
rake.rect = (400,250) # Same as above comment. 
all_sprites.add(rake) 
x,y = pygame.mouse.get_pos()    # If you are going to make the mouse 
mouse_sprite = Sprite_Mouse_Location(x,y) # a sprite, then update your mouse position in the loop. 
running = True 

while running == True: 
    for event in pygame.event.get(): 
     if event.type == QUIT or event.type == KEYUP and event.key == K_ESCAPE : 
      pygame.quit() 

     elif event.type == MOUSEMOTION : 
      # You could have this instead. 
      # mouse_sprite.rect.x, mouse_sprite.rect.y = pygame.mouse.get_pos() 
      # Then move the for loop below to be out of the event loop. 

      for s in all_sprites : 
       if pygame.sprite.collide_rect(s,mouse_sprite): 
        print("hit") 

    window.fill((0,0,0)) 
    all_sprites.update() 
    all_sprites.draw(window) 
    display.update() 

我也建議通過pygame tutorial會以及看其他人的pygame的代碼,並試圖逐行理解。這些提示都幫助我。祝你好運。

+0

dooo ...好的謝謝...我錯過了rects的東西.. 。從來沒有得到元組術語的權利......讓我看看這是否能解決它...我不能相信我昨天晚上有sprite圖層去工作;)感謝鼓勵...我會檢查出那個tut列表看看我是否錯過了任何... 25在我的桌面上的python/pygame圖書,並沒有一點幫助,當涉及到自己編程... –

+0

感謝alanxoc3和新的一天,我得到它的工作...我在這裏插入代碼非常困難,必須多次執行。 –

+0

好吧我正在嘗試發佈解決方案: –

0

感謝alanxoc3,新的一天,代碼在這裏工作 發帖,因爲我敢肯定,另一個新手會想要做這種類型的點擊鼠標

# to create a group of sprites and use the mouse over them individually 
import pygame 
from pygame import * 
from pygame.locals import * 
from pygame.sprite import * 

class Sheldon(Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) #have to have groups here, not documented well 
     self.image = transform.scale(image.load('sheldon.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

    def clickCheck(self,smouse): 
     if pygame.sprite.collide_rect(smouse, self): #could not use sritecollison because it would flag whole group 
      print('hit sheldon') # this if the check mouse that its working 
#------------------------------------------ 
class Rake(Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) 
     self.image = transform.scale(image.load('rake.jpg').convert(),(230,310)) 
     self.rect = self.image.get_rect() 

    def clickCheck(self,smouse): 
     if pygame.sprite.collide_rect(smouse, self): 
      print('hit rake') 
#-------------------------------------------   
class Sprite_Mouse_Location(Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.rect = pygame.Rect(0 , 0 , 1 , 1) # no updating needed here 
#--------------------------------------------   
pygame.init() 
window = display.set_mode((800,600)) 
mouse_sprite = Sprite_Mouse_Location() # had to create a mouse as sprite to use the sprite collision feature 
all_sprites = pygame.sprite.Group() # have to declare first, no well documented 
Sheldon.groups = all_sprites  # better than all_sprites = Group(sheldon), and can be assigned before instantiation of sheldon!!! 
sheldon = Sheldon() 
sheldon.rect = (10,10,230,310) 
Rake.groups = all_sprites 
rake = Rake() 
rake.rect = (400,250,230,310) 
running = True 

while running == True: 

    for event in pygame.event.get(): 

     if event.type == QUIT or event.type == KEYUP and event.key == K_ESCAPE : 
      pygame.quit() 

     elif event.type == MOUSEBUTTONDOWN: 
      if event.button == 1: 
       mouse_sprite.rect.x , mouse_sprite.rect.y = pygame.mouse.get_pos() # have to have this to update mouse here or get wrong location 
       for s in all_sprites: #can't have outside the event or it will continuously check 
        s.clickCheck(mouse_sprite) 

    window.fill((0,0,0)) 
    all_sprites.update() # have to have this for group sprites 
    all_sprites.draw(window) # have to have this for sprites 
    display.flip()