Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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 我怎样才能让这个物体射出射弹?_Python_Pygame - Fatal编程技术网

Python 我怎样才能让这个物体射出射弹?

Python 我怎样才能让这个物体射出射弹?,python,pygame,Python,Pygame,我有一个冰,我的问题是我怎样才能让它射出射弹,或者让射弹一个接一个地从中出来 例如:我怎样才能让它从尖端发射射弹,而它却不断下落,直到屏幕的尽头 这是我的ice对象类 smallice = pygame.image.load("fals.png") class smallice: def __init__(self,x,y,height,width,color): self.x = x self.y = y self.height = height

我有一个冰,我的问题是我怎样才能让它射出射弹,或者让射弹一个接一个地从中出来 例如:我怎样才能让它从尖端发射射弹,而它却不断下落,直到屏幕的尽头

这是我的ice对象类

smallice = pygame.image.load("fals.png")
class smallice:
   def __init__(self,x,y,height,width,color):
       self.x = x
       self.y = y
       self.height = height
       self.width = width
       self.color = color
       self.smallice = pygame.image.load("fals.png")
       self.smallice = pygame.transform.scale(self.smallice,(self.smallice.get_width()-2,self.smallice.get_height()-2))
       self.rect = pygame.Rect(x,y,height,width)
       # the hitbox our projectiles will be colliding with
       self.hitbox = (self.x + 0, self.y + 1, 10, 72)
   def draw(self):
       self.rect.topleft = (self.x,self.y)
       player_rect = self.smallice.get_rect(center = self.rect.center) 
       player_rect.centerx += 0 # 10 is just an example
       player_rect.centery += 70 # 15 is just an example
       window.blit(self.smallice, player_rect)

# define the small ices
black = (0,0,0)
smallice1 = smallice(550,215,20,20,black)
small = [smallice1]


您可以从创建一个
Bullet
类开始,例如:

class Bullet():

    def __init__(self, x, y):

        self.x = x
        self.y = y
        self.v = 5

    def fire_bullet(self,window):

        pg.draw.circle(window, (255,0,0), (self.x,self.y), 5)                         
        ice.fire = False

    def collision_box(self):

        collide = pg.Rect(self.x-offset_x, self.y-offset_y, size_x, size_y)
        return collide
子弹将是简单的圆圈,
self.v
代表速度。和
碰撞\u框
将用于检测可能的碰撞

现在,在
main_循环中
您需要检测冰对象何时“想要开火”,简单示例:

if keys[pg.K_SPACE]:
    if len(bullets) < 2:
        ice.fire = True
现在出现了
len(bullets)
bullets
是一个列表,您可以在其中添加一个
bullet
对象,然后在检测到碰撞或子弹超出所选区域时将其移除。使用此方法,您可以控制屏幕上的项目符号数量,也可以在其中循环并调用
collision()
方法查看其中一个(或多个)项目符号是否发生碰撞,并跟踪项目符号是否仍在屏幕上

如果你想随机拍摄,这里有一个基本想法:

if round(pg.time.get_ticks()/1000) % 3 == 0:
    ice.fire = True
最后一部分,浏览列表和一些简单的逻辑:

if bullets != []:                                                                   #Firing bullets
        for bullet in bullets:
            bullet.fire_bullet(window)

            if bullet.y < screen_y:                                         #Bullet movement
                    bullet.x += bullet.v

            else:
                bullets.pop(bullets.index(bullet))                                      #Poping bullets from list

            if bullet.collision_box().colliderect(other_rect_object):        #Bullet collision with other object
                bullets.pop(bullets.index(bullet))
如果项目符号!=[]:#发射子弹
对于子弹中的子弹:
子弹,子弹(窗户)
如果bullet.y<屏幕_y:#子弹移动
bullet.x+=bullet.v
其他:
项目符号.pop(项目符号.index(项目符号))#从列表中弹出项目符号
如果bullet.collision_box().collisted rect(其他对象):#bullet与其他对象碰撞
子弹.弹孔(子弹.索引(子弹))
我假设(从屏幕截图)您只想向下(垂直)射击子弹,因此
bullet.y+=bullet.v
没有方向标志

这是一个简单的代码和想法,当然还有很大的改进空间,但这种方法是有效的。
我希望我的回答能对你有所帮助。

这是我在主循环中所做的,但我遇到了一个错误“pygame.Surface”对象没有属性“fire”我将bullet类更改为Enemybolds这部分假设自己开火我没有使用“单击a”按钮进行射击我使用了你提供的自动射击你能提供bullet类代码吗,或者整个代码。如果它很长,你可以使用pasteubuntu。从您的评论中,我怀疑您使用了相同的变量名两次,例如bullet被图像覆盖,但我不能确定。因此,很抱歉,这里的回复太晚了,请不要担心,您有时间时回复。您永远不会恢复
enemybullets
类的对象。据我所见,在这一行
new\u bullet=spolution(start\u x,start\u y,dir\u x/distance,dir\u y/distance,(0,0,0))
您应该使用前面提到的类而不是这个类。在类名中也使用第一个大写字母。这就是我为底部所做的,我希望它能自动射击子弹,但我仍然会得到相同的错误'if ice.fire:AttributeError:'pygame.Surface'对象没有属性'fire','我使用了与您给我的相同的类
if bullets != []:                                                                   #Firing bullets
        for bullet in bullets:
            bullet.fire_bullet(window)

            if bullet.y < screen_y:                                         #Bullet movement
                    bullet.x += bullet.v

            else:
                bullets.pop(bullets.index(bullet))                                      #Poping bullets from list

            if bullet.collision_box().colliderect(other_rect_object):        #Bullet collision with other object
                bullets.pop(bullets.index(bullet))