2016-04-14 97 views
1

即時嘗試做一個簡單的遊戲,其中有士兵向你走來,當你點擊它們「殺死」他們,他們在屏幕後面,並開始來到你身邊,等..... 但是我有pygame鼠標點擊事件的麻煩,它只是不工作。 繼承人到目前爲止我的代碼:Python:pygame刪除鼠標點擊圖像並再次創建它

import pygame, math 
from random import randrange 

import sys, math, pygame 
from operator import itemgetter 

def getKey(customobj): 
    return customobj.getKey() 

class Point3D: 
    def __init__(self, imfiles, nfrm, x = 0, y = 0, z = 0): 
     self.x, self.y, self.z = float(x), float(y), float(z) 
     self.frms = [] 
     self.nfrm=nfrm 
     self.index=0 
     for k in range(0,nfrm): 
      im=pygame.image.load(imfiles+'_'+str(k+1)+'.png') 
      im.set_colorkey((0,0,0)) 
      self.frms.append(im)  

    def 

    project(self, win_width, win_height, fov, viewer_distance): 
    """ Transforms this 3D point to 2D using a perspective projection. """ 
    factor = fov/(viewer_distance + self.z) 
    x = self.x * factor + win_width/2 
    y = -self.y * factor + win_height/2 
    return Point3D(x, y, self.z) 

def draw3D(self, wsurface, fov, viewer_distance, max_depth): 
    win_width=wsurface.get_width() 
    win_height=wsurface.get_height() 
    factor = fov/(viewer_distance + self.z) 
    x = self.x * factor + win_width/2 
    y = -self.y * factor + win_height/2 
    size = int((1 - float(self.z)/max_depth) * 64) 
    im=pygame.transform.smoothscale(self.frms[self.index],(size,size)) 
    try: 
     wsurface.blit(im, (x, y)) 
    except: 
     print((x,y)) 
    self.index=self.index+1 
    if self.index >= self.nfrm: 
     self.index=0 

def getKey(self): 
    return -self.z 


class StartField: 
def __init__(self, num_stars, max_depth): 
    pygame.init() 

    myWin = pygame.display.set_mode((640, 450), 0, 32) 
    pygame.display.set_caption('Drawing') 

    self.screen = myWin.subsurface([0,0,640,400]); 
    self.txtwin = myWin.subsurface([0,400,640,50]); 
    pygame.display.set_caption("Task C") 

    self.clock = pygame.time.Clock() 
    self.num_stars = num_stars 
    self.max_depth = max_depth 

    self.init_stars() 

def init_stars(self): 
    """ Create the starfield """ 
    self.stars = [] 
    for i in range(self.num_stars): 
     # A star is represented as a list with this format: [X,Y,Z] 
     star = Point3D('im',8,randrange(-25,25), randrange(-25,25), randrange(1, self.max_depth)) 
     self.stars.append(star) 

def move_and_draw_stars(self): 
    """ Move and draw the stars """ 
    origin_x = self.screen.get_width()/2 
    origin_y = self.screen.get_height()/2 

    stars=sorted(self.stars,key = getKey) 

    for star in stars: 
     # The Z component is decreased on each frame. 
     star.z -= 0.05 

     # If the star has past the screen (I mean Z<=0) then we 
     # reposition it far away from the screen (Z=max_depth) 
     # with random X and Y coordinates. 
     if star.z <= 0: 
      star.x = randrange(-25,25) 
      star.y = randrange(-25,25) 
      star.z = self.max_depth 

     # Convert the 3D coordinates to 2D using perspective projection. 
     star.draw3D(self.screen, 128, 0, self.max_depth) 

def run(self): 
    """ Main Loop """   
    bgPicture = pygame.transform.smoothscale(pygame.image.load('Starfield.jpg'),(self.screen.get_width(),self.screen.get_height())) 
    font = pygame.font.Font(None, 36)  

    while 1: 
     # Lock the framerate at 50 FPS. 
     self.clock.tick(50) 

     # Handle events. 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       return 

     self.screen.blit(bgPicture, [0,0]) 
     self.move_and_draw_stars() 

     # Text window outputs 
     self.txtwin.fill([200,200,200]) 
     text = font.render("Total Score: ", 1, (10, 10, 10)) 
     self.txtwin.blit(text, [5, 5]) 

     pygame.display.update() 

    if __name__ == "__main__": 
    StartField(256, 24).run() 


     pygame.init() 
     pygame.mixer.init() 
     sounda= pygame.mixer.Sound("MuseUprising.mp3") 

     sounda.play() 
+1

這是什麼,「它只是沒有用」,是什麼意思?什麼_does_發生?你有沒有收到任何錯誤信息?他們是什麼?他們引用了哪些代碼行? – Chris

+0

這些對象意味着從遠處走向屏幕,直到它們到達您的位置,單擊它們不會執行我嘗試使用pygame.mouse.get_pos獲取鼠標位置的任何內容,然後將pygame.mouse.get_pressed()添加到看看它是否被按下,使用Sprite來檢查鼠標是否在圖像上clicked_sprites = [s for s in sprites if s.rect.collidepoint(pos)]然後使用Sprite.remove()移除圖像並添加它再次使用sprite.add() –

回答

0

要測試鼠標左鍵:

if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]: 

如果給戰士一個pygame.Rect你可以使用鼠標指針等以檢查碰撞這個:

mouse_pos = pygame.mouse.get_pos() 
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0] and self.rect.collidepoint(mouse_pos): 
+0

我如何給士兵一個pygame.Rect?你給我的其他代碼似乎與我心中的想法相似 –

+0

對不起,延誤了,一直在工作。我假設你正在爲士兵使用point3D類?在那裏添加一個矩形,設置爲None:self.rect = None。在繪圖函數中,使用x和y以及圖像大小來設置實際的矩形座標:'self.rect = pygame.Rect(x,y,im.get_width(),im.get_height())' – marienbad