首页 > 解决方案 > 我想描述一条战舰用pygame发射子弹时的抛物线

问题描述

我是一个编程初学者。最近我一直在尝试使用 pygame 创建一个简单的游戏,我设法描述了一个子弹并移动它。但是,我无法按预期移动这颗子弹。虽然我想移动这个描述抛物线曲线的子弹,但我不能旋转子弹并像抛物线一样移动。所以,代码如下。

import pygame
import math

# each variables
pmsl_no = 0
pmsl_MAX = 10
pmsl_f = [False]*pmsl_MAX
pmsl_x = [0]*pmsl_MAX
pmsl_y = [0]*pmsl_MAX
pmsl_xx = [0]*pmsl_MAX
pmsl_yy = [0]*pmsl_MAX
pmsl_deg = [0]*pmsl_MAX

# set bullet
def set_fire(a):
    global pmsl_no, pmsl_deg
    while True:

        if pmsl_f[pmsl_no] == False:
            pmsl_f[pmsl_no] = True
        if player_f[pmsl_no] == True:
            pmsl_deg[pmsl_no] += math.pi / 50
            if pmsl_deg[pmsl_no] >= 2*math.pi:
                pmsl_deg[pmsl_no] -= math.pi / 50
            pmsl_x[pmsl_no] = player_x[pmsl_no] + 100
            pmsl_y[pmsl_no] = player_y[pmsl_no] + 115
            d = abs(distance_e() - player_x[pmsl_no])
            pmsl_xx[pmsl_no] = d * (pmsl_deg[pmsl_no] - math.sin(math.radians(pmsl_deg[pmsl_no]))) / (2 * math.pi) / 50
            pmsl_yy[pmsl_no] = a * (1 - math.cos(math.radians(pmsl_deg[pmsl_no]))) / 50
            break

        pmsl_no = (pmsl_no+1) % pmsl_MAX

#bring bullet
def bring_pmsl():
    if tmr % 1000 == 0:
        set_fire(10)

# shoot bullet
def move_pmsl():
    for i in range(pmsl_MAX):
        if pmsl_f[i] == True:
            d = abs(distance_e() - player_x[pmsl_no])
            h = d*math.sin(pmsl_deg[i])/(2*10*math.pi)*(1-math.cos(pmsl_deg[i]))
            pmsl_x[i] = pmsl_x[i] + pmsl_xx[i]
            pmsl_y[i] = pmsl_y[i] - pmsl_yy[i]
            image = pygame.transform.scale(img_weapon, (20, 10)).convert_alpha()
            image2 = pygame.transform.rotate(image, h)

            screen.blit(image2, [pmsl_x[i], pmsl_y[i]])

# main loop  
running = True
while running:
    tmr += 1

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                screen = pygame.display.set_mode((X, Y))

            elif event.key == pygame.K_RETURN:
                 screen = pygame.display.set_mode((X, Y))

    screen.fill((230, 230, 230))

    pygame.display.update()
    clock.tick(500)

※ 我省略了一些与这个问题无关的代码。

我没有错误按摩,但我无法按预期移动子弹。那么,我应该怎么做才能解决这个问题呢?如果你知道这件事,请告诉我。

提示:版本:python ver.3.6

标签: pythonpygame

解决方案


推荐阅读