Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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_Pygame_Projectile - Fatal编程技术网

Python 如何在pygame中移动多个对象而不影响其他对象的运动(无延迟)

Python 如何在pygame中移动多个对象而不影响其他对象的运动(无延迟),python,pygame,projectile,Python,Pygame,Projectile,在代码中,我只是尝试让两个对象移动,一个对象射击。但一旦一颗子弹射中,两颗就停止移动,直到子弹到达终点。是否有一种方法可以同时发射子弹和移动其他物体 当正方形移动时,当你进入空间时,它会射出子弹,但当射出时,正方形会停止移动,直到子弹到达循环中的代码末端。有什么方法可以同时操作子弹和方块 代码如下: import pygame Max_w=400 Max_h=400 window=pygame.display.set_mode((Max_w,Max_h)) pygame.display.set_

在代码中,我只是尝试让两个对象移动,一个对象射击。但一旦一颗子弹射中,两颗就停止移动,直到子弹到达终点。是否有一种方法可以同时发射子弹和移动其他物体

当正方形移动时,当你进入空间时,它会射出子弹,但当射出时,正方形会停止移动,直到子弹到达循环中的代码末端。有什么方法可以同时操作子弹和方块 代码如下:

import pygame
Max_w=400
Max_h=400
window=pygame.display.set_mode((Max_w,Max_h))
pygame.display.set_caption("Game")
pygame.QUIT
x_1=0
y_1=0
x=360
y=360
velocity=5
width=40
height=40
run=True
while run:
    def Bullet():
        pygame.time.delay(100)
        global x,y
        x1=x
        y1=y
        while y1>=0:
            pygame.time.delay(2)
            if x_1<x1+15<x_1+40 and y1<=y_1+40:
                break
            keys=pygame.key.get_pressed()
            
            window.fill((0,0,0))
            pygame.draw.rect(window,(0,0,255),(x,y,width,height))
            pygame.draw.rect(window,(0,255,0),(x_1,y_1,width,height))  
            
            pygame.draw.rect(window,(255,0,0),(x1+15,y1-10,10,10))
            
            y1-=1
            pygame.display.update()
        
    
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False
    window.fill((0,0,0))
    keys=pygame.key.get_pressed()

    if keys[ord("w")] and y_1<Max_h-height:
        y_1-=velocity
        
    if keys[ord("a")]  and x_1>0:
        x_1-=velocity
        
    if keys[ord("s")] and y_1<Max_h-height:
        y_1+=velocity
        
    if keys[ord("d")] and x_1<Max_w-width:
        x_1+=velocity
        
    
    if keys[pygame.K_LEFT] and x>0:
        x-=velocity

    if keys[pygame.K_RIGHT] and x<Max_w-width:
        x+=velocity

    if keys[pygame.K_UP] and y>0:
        y-=velocity

    if keys[pygame.K_DOWN] and y<Max_h-height:
        y+=velocity

    if keys[pygame.K_SPACE]:
        Bullet()
    pygame.draw.rect(window,(0,0,255),(x,y,width,height))
    pygame.draw.rect(window,(0,255,0),(x_1,y_1,width,height))  
        
    pygame.display.update()
pygame.quit()
导入pygame
最大w=400
最大高度=400
window=pygame.display.set_模式((最大宽度,最大高度))
pygame.display.set_标题(“游戏”)
pygame.退出
x_1=0
y_1=0
x=360
y=360
速度=5
宽度=40
高度=40
运行=真
运行时:
def Bullet():
pygame.时间延迟(100)
全局x,y
x1=x
y1=y
当y1>=0时:
pygame。时间。延迟(2)

如果x_1您的问题是由多个事件循环引起的-这几乎总是一个坏主意。一旦代码进入
Bullet
功能,在Bullet离开屏幕之前,控制权不会返回给玩家。显然,这就是报道的问题

最好将项目符号存储为“数据点”列表,并使用其他方法绘制它们。这种新方法可以集成到主循环中,与现有的播放器移动代码交错

在下面的示例代码中,我创建了一个
all_bullets
列表,其中包含定义项目符号的元组。但是你需要定义一个项目符号:x,y坐标,下一个移动时间,以及它移动的方向。所以从字面上看,子弹可能是类似于
(12500,50,100,-5)
。这表示:下一步是12500毫秒之后,当前位置是(50100),子弹每一步移动-5像素

这个解决方案的妙处在于启动时,只需在列表中添加一个新的元组即可。移动和绘图功能可以维护列表

因此,要移动一个项目符号,我们只需要将y-change值(元组中的最后一个值)添加到当前y。不过,“清理”屏幕外的子弹也很方便。因为我们已经在移动子弹了,这也是一个处理这件事的好地方

这就引出了函数
movebollets()

对于列表中的每个项目符号,我们获取其(x,y)位置,并使用该位置绘制矩形

因此,现在您的主循环只需将一个新的Bullet元组添加到
all_bullets
,即可“发射”一个子弹。每一帧它都会画出子弹,并做任何子弹运动

参考:测试/示例代码: 导入pygame

Max_w=400
Max_h=400
bullet_speed=100

def moveBullets( bullet_list ):
    """ Move the bullets along their trajectory.
        If they go off-screen, delete them  """

    time_now = pygame.time.get_ticks()
    for i in range( len( bullet_list )-1, -1, -1):   # iterate in reverse order so we can del() safely
        next_move_time, x, y, dy = bullet_list[i]

        # is it time to move this bullet again?
        if ( time_now > next_move_time ):
            y -= dy
            # Did the bullet go off-screen?
            if ( x < 0 or x > Max_w or y < 0 or y > Max_h ):
                # bullet has left screen, delete it it from the list
                print("DEBUG: erasing bullet at index " + str( i ) )
                del( bullet_list[i] )
            else:
                bullet_list[i] = ( time_now+bullet_speed, x, y, dy )  # save new time, and position


def drawBullets( window, bullet_list ):
    """ Draw all the bullets to the window """

    for bullet in bullet_list:
        next_move_time, x, y, dy = bullet
        # draw the bullet
        pygame.draw.rect( window, (255, 0, 0), ( x, y, 10, 10 ) )



window=pygame.display.set_mode((Max_w,Max_h))
pygame.display.set_caption("Game")
x_1=0
y_1=0
x=360
y=360
velocity=5
width=40
height=40

all_bullets = []   # list of all bullet tuples: (time, x, y)

run=True
while run:
    
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False
    keys=pygame.key.get_pressed()

    if keys[ord("w")] and y_1<Max_h-height:
        y_1-=velocity
        
    if keys[ord("a")]  and x_1>0:
        x_1-=velocity
        
    if keys[ord("s")] and y_1<Max_h-height:
        y_1+=velocity
        
    if keys[ord("d")] and x_1<Max_w-width:
        x_1+=velocity

    if keys[ord("f")]:
        # create a new bullet
        all_bullets.append( ( pygame.time.get_ticks(), x_1+15, y_1+40, -5 ) )
        
    
    if keys[pygame.K_LEFT] and x>0:
        x-=velocity

    if keys[pygame.K_RIGHT] and x<Max_w-width:
        x+=velocity

    if keys[pygame.K_UP] and y>0:
        y-=velocity

    if keys[pygame.K_DOWN] and y<Max_h-height:
        y+=velocity

    if keys[pygame.K_SPACE]:
        # create a new bullet
        all_bullets.append( ( pygame.time.get_ticks(), x+15, y-10, 5 ) )

    # move the bullets
    moveBullets( all_bullets )

    # draw the screen
    window.fill((0,0,0))
    pygame.draw.rect(window,(0,0,255),(x,y,width,height))
    pygame.draw.rect(window,(0,255,0),(x_1,y_1,width,height))  
    drawBullets( window, all_bullets )
        
    pygame.display.update()

pygame.quit()
Max_w=400
最大高度=400
子弹头速度=100
def移动项目符号(项目符号列表):
“”“沿弹道移动子弹。
如果它们离开屏幕,请删除它们“”
time\u now=pygame.time.get\u ticks()
对于范围内的i(len(bullet_list)-1,-1,-1):#以相反的顺序迭代,以便可以安全地del()
下一步移动时间,x,y,dy=bullet\u列表[i]
#是时候再次移动这颗子弹了吗?
如果(现在时间>下一步移动时间):
y-=dy
#子弹离开屏幕了吗?
如果(x<0或x>Max\u w或y<0或y>Max\u h):
#bullet已离开屏幕,请将其从列表中删除
打印(“调试:删除索引“+str(i))处的项目符号)
del(项目符号列表[i])
其他:
子弹头列表[i]=(现在的时间+子弹头速度,x,y,dy)#保存新的时间和位置
def drawBullets(窗口,bullet_列表):
“”“将所有项目符号拉到窗口”“”
对于项目符号列表中的项目符号:
下一步移动时间,x,y,dy=bullet
#拔出子弹
pygame.draw.rect(窗口,(255,0,0),(x,y,10,10))
window=pygame.display.set_模式((最大宽度,最大高度))
pygame.display.set_标题(“游戏”)
x_1=0
y_1=0
x=360
y=360
速度=5
宽度=40
高度=40
所有项目符号=[]所有项目符号元组的列表:(时间,x,y)
运行=真
运行时:
pygame。时间。延迟(50)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
keys=pygame.key.get_pressed()
如果键[ord(“w”)和y_10:
x_1-=速度

如果键[ord(“s”)和y_1尝试使用线程来运行while循环,这可能会影响程序的其他部分。

如果您能提供代码,那就太好了。我现在添加了它。希望能有帮助
def drawBullets( window, bullet_list ):
    """ Draw all the bullets to the window """

    for bullet in bullet_list:
        next_move_time, x, y, dy = bullet
        # draw the bullet
        pygame.draw.rect( window, (255, 0, 0), ( x, y, 10, 10 ) )
Max_w=400
Max_h=400
bullet_speed=100

def moveBullets( bullet_list ):
    """ Move the bullets along their trajectory.
        If they go off-screen, delete them  """

    time_now = pygame.time.get_ticks()
    for i in range( len( bullet_list )-1, -1, -1):   # iterate in reverse order so we can del() safely
        next_move_time, x, y, dy = bullet_list[i]

        # is it time to move this bullet again?
        if ( time_now > next_move_time ):
            y -= dy
            # Did the bullet go off-screen?
            if ( x < 0 or x > Max_w or y < 0 or y > Max_h ):
                # bullet has left screen, delete it it from the list
                print("DEBUG: erasing bullet at index " + str( i ) )
                del( bullet_list[i] )
            else:
                bullet_list[i] = ( time_now+bullet_speed, x, y, dy )  # save new time, and position


def drawBullets( window, bullet_list ):
    """ Draw all the bullets to the window """

    for bullet in bullet_list:
        next_move_time, x, y, dy = bullet
        # draw the bullet
        pygame.draw.rect( window, (255, 0, 0), ( x, y, 10, 10 ) )



window=pygame.display.set_mode((Max_w,Max_h))
pygame.display.set_caption("Game")
x_1=0
y_1=0
x=360
y=360
velocity=5
width=40
height=40

all_bullets = []   # list of all bullet tuples: (time, x, y)

run=True
while run:
    
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False
    keys=pygame.key.get_pressed()

    if keys[ord("w")] and y_1<Max_h-height:
        y_1-=velocity
        
    if keys[ord("a")]  and x_1>0:
        x_1-=velocity
        
    if keys[ord("s")] and y_1<Max_h-height:
        y_1+=velocity
        
    if keys[ord("d")] and x_1<Max_w-width:
        x_1+=velocity

    if keys[ord("f")]:
        # create a new bullet
        all_bullets.append( ( pygame.time.get_ticks(), x_1+15, y_1+40, -5 ) )
        
    
    if keys[pygame.K_LEFT] and x>0:
        x-=velocity

    if keys[pygame.K_RIGHT] and x<Max_w-width:
        x+=velocity

    if keys[pygame.K_UP] and y>0:
        y-=velocity

    if keys[pygame.K_DOWN] and y<Max_h-height:
        y+=velocity

    if keys[pygame.K_SPACE]:
        # create a new bullet
        all_bullets.append( ( pygame.time.get_ticks(), x+15, y-10, 5 ) )

    # move the bullets
    moveBullets( all_bullets )

    # draw the screen
    window.fill((0,0,0))
    pygame.draw.rect(window,(0,0,255),(x,y,width,height))
    pygame.draw.rect(window,(0,255,0),(x_1,y_1,width,height))  
    drawBullets( window, all_bullets )
        
    pygame.display.update()

pygame.quit()