Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:pygame在鼠标单击时删除图像并再次创建它_Python_Image_Pygame_Mouse - Fatal编程技术网

Python:pygame在鼠标单击时删除图像并再次创建它

Python:pygame在鼠标单击时删除图像并再次创建它,python,image,pygame,mouse,Python,Image,Pygame,Mouse,我试着做一个简单的游戏,有士兵向你走来,当你点击他们“杀死”他们时,他们会出现在屏幕后面,开始向你走来,等等。。。。。 然而,我在pygame鼠标点击事件中遇到了问题,它就是不起作用。 以下是我目前的代码: import pygame, math from random import randrange import sys, math, pygame from operator import itemgetter def getKey(customobj): return cust

我试着做一个简单的游戏,有士兵向你走来,当你点击他们“杀死”他们时,他们会出现在屏幕后面,开始向你走来,等等。。。。。 然而,我在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()
导入pygame、数学
从随机输入范围
导入sys、math、pygame
从运算符导入itemgetter
def getKey(自定义对象):
返回customobj.getKey()
类Point3D:
定义初始化(self,imfiles,nfrm,x=0,y=0,z=0):
self.x,self.y,self.z=浮点(x),浮点(y),浮点(z)
self.frms=[]
self.nfrm=nfrm
self.index=0
对于范围内的k(0,nfrm):
im=pygame.image.load(imfiles++str(k+1)+.png)
im.set_颜色键((0,0,0))
self.frms.append(im)
def
项目(自身、视野宽度、视野高度、观察者距离):
“”“使用透视投影将此三维点转换为二维。”“”
系数=视野/(观察者距离+自身z)
x=自身。x*系数+赢得宽度/2
y=-self.y*因子+win_身高/2
返回点3d(x,y,self.z)
def draw3D(自身、表面、视野、观察者距离、最大深度):
win\u width=wsurface.get\u width()
win\u height=wsurface.get\u height()
系数=视野/(观察者距离+自身z)
x=自身。x*系数+赢得宽度/2
y=-self.y*因子+win_身高/2
大小=整数((1-浮点(self.z)/最大深度)*64)
im=pygame.transform.smoothscale(self.frms[self.index],(size,size))
尝试:
wsurface.blit(im,(x,y))
除:
打印((x,y))
self.index=self.index+1
如果self.index>=self.nfrm:
self.index=0
def getKey(自):
return-self.z
类StartField:
定义初始深度(自、数星、最大深度):
pygame.init()
myWin=pygame.display.set_模式((640450),0,32)
pygame.display.set_标题('绘图')
self.screen=myWin.subground([0,0640400]);
self.txtwin=myWin.subground([0400640,50]);
pygame.display.set_标题(“任务C”)
self.clock=pygame.time.clock()
self.num\u stars=num\u stars
self.max\u depth=最大深度
self.init_stars()
def初始恒星(自身):
“”“创建星域”“”
self.stars=[]
对于范围内的i(self.num_星):
#星形表示为以下格式的列表:[X,Y,Z]
星形=点3D('im',8,随机范围(-25,25),随机范围(-25,25),随机范围(1,自身最大深度))
self.stars.append(star)
def移动和绘制星星(自我):
“移动并绘制星星”
origin\u x=self.screen.get\u width()/2
origin\u y=self.screen.get\u height()/2
星号=排序(self.stars,key=getKey)
对于星星中的星星:
#每个帧上的Z分量减小。
星形z-=0.05

#如果星号已通过屏幕(我的意思是Z以测试鼠标左键:

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): 

要测试鼠标左键,请执行以下操作:

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): 

“它就是不起作用,”是什么意思?发生了什么?你收到任何错误消息吗?它们是什么?它们引用的代码行是什么?这些对象应该从很远的地方向屏幕移动,直到它们到达你,单击它们不会做任何事情。我尝试使用pygame.mouse.get_pos获取鼠标位置,然后添加pygame.mouse.get_pressed()为了查看它是否被按下,我使用Sprite检查鼠标是否在图像上单击了_sprites=[s代表s如果s.rect.collidepoint(pos)],然后使用Sprite.remove()删除图像,并使用Sprite.add()再次添加图像,“它只是不起作用,”是什么意思?发生了什么?你收到任何错误消息吗?它们是什么?它们引用的代码行是什么?这些对象应该从很远的地方向屏幕移动,直到它们到达你,单击它们不会做任何事情。我尝试使用pygame.mouse.get_pos获取鼠标位置,然后添加pygame.mouse.get_pressed()为了查看它是否被按下,我使用Sprite检查鼠标是否在图像上单击了_sprites=[s for s in sprites if s.rect.collidepoint(pos)],然后使用Sprite.remove()删除图像并使用Sprite.add()再次添加它我该如何给士兵一个pygame.Rect?你给我的另一个代码似乎与我想象中的类似。抱歉,耽搁了。我假设你正在为士兵使用point3D类?在那里添加一个Rect,设置为None:self.Rect=None。在draw函数中,使用x和y以及图像大小设置实际的Rect单词:
self.rect=pygame.rect(x,y,im.get\u width(),im.get\u height())
我该如何给士兵一个pygame.Rect?你给我的另一个代码似乎与我记忆中的代码相似抱歉,延迟已经开始工作。我假设你正在为士兵使用point3D类?在那里添加一个Rect,设置为None:self.Rect=None。在draw函数中,使用x和y以及图像si设置实际的Rect命令ze:
self.rect=pygame.rect(x,y,im.get\u width(),im.get\u height())