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

Python 如何在pygame中画一条向上移动的线

Python 如何在pygame中画一条向上移动的线,python,pygame,Python,Pygame,我制作了一个python游戏,当你按下空格键时,它会射出一颗子弹(一条线)。不过它只能拍一次。我在想我怎样才能拍好几次 shotStartX = x + 30 shotStartY = y + 20 shotEndX = x + 30 shotEndY = y shoot = False while True: if event.type == pygame.KEYDOWN: elif event.key == pygame.K_SPACE:

我制作了一个python游戏,当你按下空格键时,它会射出一颗子弹(一条线)。不过它只能拍一次。我在想我怎样才能拍好几次

shotStartX = x + 30
shotStartY = y + 20

shotEndX = x + 30
shotEndY = y

shoot = False

while True:
        if event.type == pygame.KEYDOWN:
            elif event.key == pygame.K_SPACE:
                shoot = True

    gameDisplay.fill(blue)

    if shoot:
        pygame.draw.line(gameDisplay,red,(shotStartX,shotStartY),(shotEndX,shotEndY),5)
        shotStartY -= 10
        shotEndY -= 10

    gameDisplay.blit(rocketImg,(x,y))

    pygame.display.update()

    clock.tick(30)

我将创建两个类,bullet和bullets,并将射击速率限制在某个时间。试试这个:

import pygame
import time

# define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

W = 480
H = 720
FPS = 60

BULLET_SPEED = 5
BULLET_FIRING_RATE = 0.5 # seconds

class bullet:
    def __init__(s, x, y): # x, y = starting point
        s.x = x
        s.y = y
        s.dead = False

    def draw(s):
        global DS
        global WHITE

        pygame.draw.circle(DS, WHITE, (s.x, s.y), 10)

    def move(s):
        global BULLET_SPEED

        s.y -= BULLET_SPEED
        if s.y <= 0:
            s.dead = True

class bullets:
    def __init__(s):
        s.container = []
        s.lastBulletTime = time.time() -BULLET_FIRING_RATE

    def add(s, x, y):
        global BULLET_FIRING_RATE

        now = time.time()
        if now - s.lastBulletTime >= BULLET_FIRING_RATE:
            s.container.append(bullet(x, y))
            s.lastBulletTime = now

    def do(s):
        deadBullets = []
        for b in s.container:
            b.draw()
            b.move()
            if b.dead: deadBullets.append(b)
        for db in deadBullets:
            s.container.remove(db)

pygame.init()
DS = pygame.display.set_mode((W, H))
CLOCK = pygame.time.Clock()

BULLETS = bullets()

# press escape to exit
while True:
    e = pygame.event.get()
    if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
    mx, my = pygame.mouse.get_pos()
    mb = pygame.mouse.get_pressed()

    if mb[0]:
        BULLETS.add(mx, my)

    DS.fill(BLACK)
    BULLETS.do()
    pygame.draw.circle(DS, WHITE, (mx, my), 40)

    pygame.display.update()
    CLOCK.tick(FPS)
pygame.quit()
导入pygame
导入时间
#定义一些颜色
黑色=(0,0,0)
白色=(255,255,255)
W=480
H=720
FPS=60
子弹头速度=5
子弹射速=0.5秒
类别项目符号:
def uu init uu(s,x,y):#x,y=起点
s、 x=x
s、 y=y
s、 死=假
def提取:
全球DS
全球白色
pygame.draw.circle(DS,白色,(s.x,s.y),10)
def移动:
全球子弹头速度
s、 y-=子弹头速度
如果s.y=子弹射击率:
s、 容器。附加(项目符号(x,y))
s、 lastBulletTime=现在
def do(s):
死亡子弹=[]
对于s.容器中的b:
b、 画()
b、 移动()
如果b.dead:deadbollets.append(b)
对于deadBullets中的db:
s、 容器。移除(db)
pygame.init()
DS=pygame.display.set_模式((W,H))
CLOCK=pygame.time.CLOCK()
子弹=子弹()
#按escape退出
尽管如此:
e=pygame.event.get()
如果pygame.key.get_按下()[pygame.K_ESCAPE]:中断
mx,my=pygame.mouse.get_pos()
mb=pygame.mouse.get_pressed()
如果mb[0]:
项目符号。添加(mx,我的)
DS.填充(黑色)
子弹
pygame.draw.circle(DS,白色,(mx,my),40)
pygame.display.update()
时钟滴答声(FPS)
pygame.quit()

我认为问题出在“elif event.key…”中。它应该是“if event.key…”。除此之外,在拍摄后设置“shot=False”(我的意思是,在“if shot”一段中)。那只会使线条瞬间消失是的,我明白了。如果没有完整的代码,我无法理解您的实际意图。例如,如果shotStartX在您的程序中的某个地方(我的意思是在while循环中)从未更改,那么您将在同一位置上闪烁该行,因此您将看不到它,尽管您一次闪烁多行。它应该在何时多次闪烁?如果反复按下或按住该键,或?