0

I am a programing beginner. Recently I've been trying to create a simple game by using pygame, I've managed to describe a bullet and move it. However, I can't move this bullet as I expected. Although I would like to move this bullet describing a parabola curve, I can't rotate a bullet and move like a parabola. So, the code is as below.

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)

※ I omit some codes which have nothing to do with this problem.

I have no error massage but I can't move bullet as I expected. So, what should I do to solve this problem? If you know about that, please let me know.

Tips: version: python ver.3.6

Yuji
  • 13
  • 2
  • 1
    I suggest you rethink the way you are going at it. don't try to produce a parabola directly, a parabola is the result a moving with constant horizontal velocity and a changing horizontal velocity (ie moving in a gravitational field) - maybe try updating the bullets position by tracking horizontal and vertical velocity. I see trigonometry in your code, that isn't needed for a parabola? – Fraser Langton Oct 12 '21 at 01:34
  • Because Pygame is a pixel-based graphics engine, it doesn't make it very easy to rotate things smoothly. Rotating your image every frame is going to be slow and awkward and the results may not look very good (depending on the resolution of your art). Have you considered circular bullets so the complexity of the rotation won't be needed? A common alternative used by many pixel games is to pre-compute art for only a small fraction of the possible angles (e.g. 8 or so, some of which may be reflections of others) and just pick the closest one. – Blckknght Oct 12 '21 at 02:20
  • Hello, Mr./Ms. Langton and Mr./Ms. Blckknght. Thanks for your suggestion and advise, I tried to draw parabola by using quadratic function. So, I set the argument of bullet as math.atan(pmsl_yy[i]/pmsl_xx[i]). But still I can't rotate the bullet. So I think it is not easy to rotate a object smoothly in pygame. So, I'll investigate pygame's document and compromise to reappear what I would like to make. – Yuji Oct 12 '21 at 03:32
  • Related [Pygame- rotate sprite and follow path simultaneously](https://stackoverflow.com/questions/56297756/pygame-rotate-sprite-and-follow-path-simultaneously/56298370#56298370) – Rabbid76 Oct 12 '21 at 04:58

0 Answers0