Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/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 pygame.init() 红色=255,0,0 蓝色=0,0255 黑色=0,0,0 屏幕宽度=800 屏幕高度=600 gameDisplay=pygame.display.set_模式((屏幕宽度,屏幕高度))##屏幕宽度和高度 pygame.display.set_caption(“只是一些块”)##设置我的窗口标题 clock=pygame.time.clock() 类player():##具有我对player 1的所有属性 定义初始值(自、x、y、宽度、高度): self.x=x self.y=y 自我高度=高度 self.width=宽度 self.vel=5 self.left=False self.right=False self.up=False self.down=False 类射弹():##射弹属性 定义初始(自、x、y、半径、颜色、面): self.x=x self.y=y 自半径=半径 自我面对 颜色 self.vel=8*正面#子弹速度*方向(-1或1) def绘制(自绘制、游戏显示): pygame.draw.circle(游戏显示,self.color,(self.x,self.y),self.radius)##在其后面加上1,使其成为一个轮廓 def重画游戏窗口(): 对于项目符号中的项目符号:##绘制项目符号 子弹画(游戏显示) pygame.display.update() #主回路 player1=player(300410,50,70)#从类中移动内容(当变量为user use player1.var时) 项目符号=[] 运行=真 当run==True时: 时钟滴答作响(27) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 运行=错误 对于子弹中的子弹: 如果bullet.x0和bullet.y0:##确保bullet不会离开屏幕 bullet.x+=bullet.vel 其他: 子弹.弹孔(子弹.索引(子弹)) keys=pygame.key.get_pressed()###检查是否已按下某个键 ##红人运动 如果键[pygame.K_w]和player1.y>player1.vel:##检查该键是否已按下(这将检查w)并检查边界 player1.y-=player1.vel##沿一个方向移动形状 player1.up=True player1.down=False 如果键[pygame.K#a]和player1.x>player1.vel:###这是为 player1.x-=player1.vel player1.left=True player1.right=False 如果键[pygame.K#s]和player1.y_Python_Pygame_Pygame Tick - Fatal编程技术网

Python 如何阻止一次发射超过一颗子弹? 导入pygame pygame.init() 红色=255,0,0 蓝色=0,0255 黑色=0,0,0 屏幕宽度=800 屏幕高度=600 gameDisplay=pygame.display.set_模式((屏幕宽度,屏幕高度))##屏幕宽度和高度 pygame.display.set_caption(“只是一些块”)##设置我的窗口标题 clock=pygame.time.clock() 类player():##具有我对player 1的所有属性 定义初始值(自、x、y、宽度、高度): self.x=x self.y=y 自我高度=高度 self.width=宽度 self.vel=5 self.left=False self.right=False self.up=False self.down=False 类射弹():##射弹属性 定义初始(自、x、y、半径、颜色、面): self.x=x self.y=y 自半径=半径 自我面对 颜色 self.vel=8*正面#子弹速度*方向(-1或1) def绘制(自绘制、游戏显示): pygame.draw.circle(游戏显示,self.color,(self.x,self.y),self.radius)##在其后面加上1,使其成为一个轮廓 def重画游戏窗口(): 对于项目符号中的项目符号:##绘制项目符号 子弹画(游戏显示) pygame.display.update() #主回路 player1=player(300410,50,70)#从类中移动内容(当变量为user use player1.var时) 项目符号=[] 运行=真 当run==True时: 时钟滴答作响(27) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 运行=错误 对于子弹中的子弹: 如果bullet.x0和bullet.y0:##确保bullet不会离开屏幕 bullet.x+=bullet.vel 其他: 子弹.弹孔(子弹.索引(子弹)) keys=pygame.key.get_pressed()###检查是否已按下某个键 ##红人运动 如果键[pygame.K_w]和player1.y>player1.vel:##检查该键是否已按下(这将检查w)并检查边界 player1.y-=player1.vel##沿一个方向移动形状 player1.up=True player1.down=False 如果键[pygame.K#a]和player1.x>player1.vel:###这是为 player1.x-=player1.vel player1.left=True player1.right=False 如果键[pygame.K#s]和player1.y

Python 如何阻止一次发射超过一颗子弹? 导入pygame pygame.init() 红色=255,0,0 蓝色=0,0255 黑色=0,0,0 屏幕宽度=800 屏幕高度=600 gameDisplay=pygame.display.set_模式((屏幕宽度,屏幕高度))##屏幕宽度和高度 pygame.display.set_caption(“只是一些块”)##设置我的窗口标题 clock=pygame.time.clock() 类player():##具有我对player 1的所有属性 定义初始值(自、x、y、宽度、高度): self.x=x self.y=y 自我高度=高度 self.width=宽度 self.vel=5 self.left=False self.right=False self.up=False self.down=False 类射弹():##射弹属性 定义初始(自、x、y、半径、颜色、面): self.x=x self.y=y 自半径=半径 自我面对 颜色 self.vel=8*正面#子弹速度*方向(-1或1) def绘制(自绘制、游戏显示): pygame.draw.circle(游戏显示,self.color,(self.x,self.y),self.radius)##在其后面加上1,使其成为一个轮廓 def重画游戏窗口(): 对于项目符号中的项目符号:##绘制项目符号 子弹画(游戏显示) pygame.display.update() #主回路 player1=player(300410,50,70)#从类中移动内容(当变量为user use player1.var时) 项目符号=[] 运行=真 当run==True时: 时钟滴答作响(27) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 运行=错误 对于子弹中的子弹: 如果bullet.x0和bullet.y0:##确保bullet不会离开屏幕 bullet.x+=bullet.vel 其他: 子弹.弹孔(子弹.索引(子弹)) keys=pygame.key.get_pressed()###检查是否已按下某个键 ##红人运动 如果键[pygame.K_w]和player1.y>player1.vel:##检查该键是否已按下(这将检查w)并检查边界 player1.y-=player1.vel##沿一个方向移动形状 player1.up=True player1.down=False 如果键[pygame.K#a]和player1.x>player1.vel:###这是为 player1.x-=player1.vel player1.left=True player1.right=False 如果键[pygame.K#s]和player1.y,python,pygame,pygame-tick,Python,Pygame,Pygame Tick,当我射出超过1发子弹时,我一次只想射出1发子弹(但不只是屏幕上的1发子弹) 它们都在一个大团中开火,并且粘在一起,所以我希望它们在不同的时间开火 我试过使用延迟时钟。滴答声,但这会使游戏非常滞后 我是pygame的新手,还不完全了解它。如果有任何帮助,将不胜感激。谢谢 发射子弹的一般方法是将子弹的位置存储在列表中(bullet\u list)。发射子弹时,将子弹的起始位置([start\u x,start\u y])添加到列表中。起始位置是发射子弹的对象(玩家或敌人)的位置。使用for-循环遍历

当我射出超过1发子弹时,我一次只想射出1发子弹(但不只是屏幕上的1发子弹) 它们都在一个大团中开火,并且粘在一起,所以我希望它们在不同的时间开火 我试过使用延迟
时钟。滴答声
,但这会使游戏非常滞后


我是pygame的新手,还不完全了解它。如果有任何帮助,将不胜感激。谢谢

发射子弹的一般方法是将子弹的位置存储在列表中(
bullet\u list
)。发射子弹时,将子弹的起始位置(
[start\u x,start\u y]
)添加到列表中。起始位置是发射子弹的对象(玩家或敌人)的位置。使用
for
-循环遍历列表中的所有项目符号。移动循环中每个单独项目符号的位置。从离开屏幕的列表中删除项目符号(
bullet\u list.Remove(bullet\u pos)
)。因此,必须运行列表的副本(
bullet_list[:]
)(请参阅)。使用另一个
for
-循环
blit
屏幕上的剩余项目符号:

bullet_list=[]
当run==True时:
# [...]
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_空间:
bullet\u list.append([start\u x,start\u y])
对于项目符号列表[:]中的项目符号位置:
子弹头位置[0]+=移动子弹头位置
子弹头位置[1]+=移动子弹头位置
如果不在屏幕上,则使用screen.get_rect().colliderect(bullet_image.get_rect(center=bullet_pos))
项目符号列表。移除(项目符号位置)
# [...]
对于bullet_列表中的bullet_位置[:]
screen.blit(bullet\u image,bullet\u image.get\u rect(中心=bullet\u位置))
# [...]
另见


只要按下一个键,就会设置返回的状态。这对玩家的移动很有用。只要按下一个键,玩家就会继续移动。
但当你想开枪的时候,这与你的意图相矛盾。如果要在按键时发射子弹,则可以使用事件。按下某个键时,该事件仅发生一次:

while run==True:
时钟滴答作响(27)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.KEYDOWN:
import pygame
pygame.init()

red = 255,0,0
blue = 0,0,255
black = 0,0,0

screenWidth = 800
screenHeight = 600

gameDisplay = pygame.display.set_mode((screenWidth,screenHeight))        ## screen width and height
pygame.display.set_caption('JUST SOME BLOCKS')       ## set my title of the window

clock = pygame.time.Clock()

class player():       ## has all of my attributes for player 1
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.vel = 5
        self.left = False
        self.right = False
        self.up = False
        self.down = False

class projectile():     ## projectile attributes
    def __init__(self,x,y,radius,colour,facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.facing = facing
        self.colour = colour
        self.vel = 8 * facing       # speed of bullet * the direction (-1 or 1)

    def draw(self,gameDisplay):
        pygame.draw.circle(gameDisplay, self.colour , (self.x,self.y),self.radius)      ## put a 1 after that to make it so the circle is just an outline

def redrawGameWindow():
    for bullet in bullets:      ## draw bullets
        bullet.draw(gameDisplay)

    pygame.display.update()   

#mainloop

player1 = player(300,410,50,70)     # moves the stuff from the class (when variables are user use player1.var)
bullets = []

run = True
while run == True:
    clock.tick(27)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for bullet in bullets:
        if bullet.x < screenWidth and bullet.x > 0 and bullet.y < screenHeight and bullet.y > 0: ## makes sure bullet does not go off screen
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))


    keys = pygame.key.get_pressed()     ## check if a key has been pressed

    ## red player movement   
    if keys[pygame.K_w] and player1.y > player1.vel:    ## check if that key has been pressed down (this will check for w)     and checks for boundry
        player1.y -= player1.vel            ## move the shape in a direction
        player1.up = True
        player1.down = False

    if keys[pygame.K_a] and player1.x > player1.vel:      ### this is for a 
        player1.x -= player1.vel
        player1.left = True
        player1.right = False

    if keys[pygame.K_s] and player1.y < screenHeight - player1.height - player1.vel: ## this is for s
        player1.y += player1.vel
        player1.down = True
        player1.up = False

    if keys[pygame.K_d] and player1.x < screenWidth - player1.width - player1.vel:   ## this is for d                          
        player1.x += player1.vel
        player1.right = True
        player1.left = False

    if keys[pygame.K_SPACE]:     # shooting with the space bar
        if player1.left == True:   ## handles the direction of the bullet
            facing = -1
        else:
            facing = 1  


        if len(bullets) < 5:    ## max amounts of bullets on screen
            bullets.append(projectile(player1.x + player1.width //2 ,player1.y + player1.height//2,6,black,facing))   ##just like calling upon a function




    ## level


    gameDisplay.fill((0,255,0))        ### will stop the shape from spreading around and will have a background
    pygame.draw.rect(gameDisplay,(red),(player1.x,player1.y,player1.width,player1.height))  ## draw player
    pygame.display.update()
    redrawGameWindow()

pygame.quit()