2016-04-14 57 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