Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - 为什么屏幕上没有出现任何项目符号? -pygame(Why aren't any bullets appearing on screen? - pygame)

The objective was to make some bullets come out of the current position of the player and then have them move in a straight line, defined by a vector that is defined by the points of the mouse position at the time of clicking the button and the position of the player, but when the button is pressed nothing happens.

(目的是使子弹从播放器的当前位置出来,然后使其直线移动,该直线由矢量定义,该矢量由单击按钮时鼠标位置和位置的点定义播放器的功能,但是当按下按钮时什么也没有发生。)

Can anyone tell me whats wrong with this code?

(谁能告诉我这段代码有什么问题吗?)

Thank you

(谢谢)

import pygame
import math
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Space Shooter")
char = pygame.image.load("spaceship_sprite.png")

x = 500/2
y = 500/2
width = 50
height = 50
velocity = 6
list_of_bullets = []

def point_to_mouse(x,y,char):
    mouse_x, mouse_y = pygame.mouse.get_pos()
    vector_x, vector_y = mouse_x - x, mouse_y - y
    angle = (180 / math.pi) * -math.atan2(vector_y, vector_x) - 90
    updated_image = pygame.transform.rotate(char,int(angle))
    image_location = updated_image.get_rect(center= (x,y))
    win.blit(updated_image,image_location)

def update_game(x,y,width,height,char):
    win.fill((0,0,0))
    point_to_mouse(x,y,char)
    pygame.display.update()

def spawn_bullet(x,y):
    global list_of_bullets
    initial_x = x
    initial_y = y
    mouse_x, mouse_y = pygame.mouse.get_pos()
    vector_x, vector_y = mouse_x - x, mouse_y - y
    #normalize the vector
    distance = math.sqrt(vector_x ** 2 + vector_y **2)
    normalized_vec = (vector_x/ distance, vector_y/distance)
    list_of_bullets.append([initial_x,initial_y,normalized_vec])


run = True
while run:
    #check for an event
    pygame.time.delay(75)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spawn_bullet(x,y)

    for bullet in list_of_bullets:
        bullet[0] = bullet[0] * bullet[2][0]
        bullet[1] = bullet[1] * bullet[2][1]

        pygame.draw.rect(win,(0,0,255),(int(bullet[0]),int(bullet[1]),20,20))

        if bullet[0] > 500 or bullet[0] < 0 or bullet[1] < 0 or bullet[1] > 500:
            del list_of_bullets[list_of_bullets.index(bullet)]
            continue



    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and x > 0 + width:
        x = x - velocity
    elif keys[pygame.K_RIGHT] and x < 500 - width:
        x = x + velocity
    elif keys[pygame.K_UP] and y > 0 + width:
        y = y - velocity
    elif keys[pygame.K_DOWN] and y < 500 - width:
        y = y + velocity


    update_game(x,y,width,height,char)

pygame.quit()
  ask by Poodlers Freitas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are 2 issues.

(有2个问题。)

The current position of the bullets is multiplied by its the normalized direction vector.

(子弹的当前位置乘以其归一化方向矢量。)

That doesn't make sense:

(那没有道理:)

 for bullet in list_of_bullets: bullet[0] = bullet[0] * bullet[2][0] bullet[1] = bullet[1] * bullet[2][1] 

Add the direction vector to the position of the bullet:

(将方向向量添加到项目符号的位置:)

for bullet in list_of_bullets:
    bullet[0] += bullet[2][0]
    bullet[1] += bullet[2][1]

If you want to increase the speed of the bullets, then you've to scale the vector by a speed .

(如果要提高子弹的速度,则必须按speed缩放矢量。)

eg:

(例如:)

def spawn_bullet(x,y):
    global list_of_bullets
    initial_x = x
    initial_y = y
    mouse_x, mouse_y = pygame.mouse.get_pos()
    vector_x, vector_y = mouse_x - x, mouse_y - y

    distance = math.sqrt(vector_x ** 2 + vector_y **2)
    speed = 5
    move_vec = (speed*vector_x/distance, speed*vector_y/distance)

    list_of_bullets.append([initial_x, initial_y, move_vec]) 

the 2nd issue is, that the display is cleared, after the bullets are draw, so you'll never "see2 the bullets.

(第二个问题是,在抽出子弹后清除了显示内容,因此您将永远不会“看到”子弹。)

Draw the bullets in update_game after the display is cleared:

(清除显示后,在update_game绘制项目符号:)

def update_game(x,y,width,height,char):
    win.fill((0,0,0))
    for bullet in list_of_bullets:
        pygame.draw.rect(win,(0,0,255),(int(bullet[0]),int(bullet[1]),20,20))
    point_to_mouse(x,y,char)
    pygame.display.update()

(Delete the drawing for the main application loop)

((删除主应用程序循环的图形))



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...