如何旋转精灵并向鼠标位置射击子弹?

2025-02-25 09:08:00
admin
原创
30
摘要:问题描述:目标是让一些子弹从玩家当前位置射出,然后让它们沿直线移动,该直线由单击按钮时鼠标位置的点和玩家位置定义的矢量定义,但按下按钮时什么也没有发生。谁能告诉我这段代码有什么问题?谢谢import pygame import math pygame.init() win = pygame.display.s...

问题描述:

目标是让一些子弹从玩家当前位置射出,然后让它们沿直线移动,该直线由单击按钮时鼠标位置的点和玩家位置定义的矢量定义,但按下按钮时什么也没有发生。谁能告诉我这段代码有什么问题?谢谢

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()

解决方案 1:

有两个问题。子弹的当前位置乘以其标准化的方向向量。这没有意义:

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

将方向向量添加到子弹的位置:

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

如果你想增加子弹的速度,那么你必须按一定比例缩放矢量speed。例如:

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]) 

第二个问题是,子弹射出后,显示屏会被清除,因此您永远不会“看到”子弹。

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()

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


最小示例:

IT科技

import math
import pygame

def blit_point_to_mouse(target_surf, char_surf, x, y):
    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
    rotated_surface = pygame.transform.rotate(char_surf, round(angle))
    rotated_surface_location = rotated_surface.get_rect(center = (x, y))
    target_surf.blit(rotated_surface, rotated_surface_location)

def spawn_bullet(list_of_bullets, x, y):
    mouse_x, mouse_y = pygame.mouse.get_pos()
    vector_x, vector_y = mouse_x - x, mouse_y - y
    distance = math.hypot(vector_x, vector_y)
    if distance == 0:
        return
    speed = 5
    move_vec = (speed * vector_x / distance, speed * vector_y / distance)
    list_of_bullets.append([x, y, move_vec])

pygame.init()
window = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()

rocket = pygame.image.load('Rocket64.png')
rocket_rect = rocket.get_rect(center = window.get_rect().center)
velocity = 6
list_of_bullets = []
bullet = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(bullet, (64, 64, 64), (10, 10), 10)
pygame.draw.circle(bullet, (96, 96, 96), (10, 10), 9)
pygame.draw.circle(bullet, (128, 128, 128), (9, 9), 7)
pygame.draw.circle(bullet, (160, 160, 160), (8, 8), 5)
pygame.draw.circle(bullet, (192, 192, 192), (7, 7), 3)
pygame.draw.circle(bullet, (224, 224, 224), (6, 6), 1)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spawn_bullet(list_of_bullets, *rocket_rect.center)

    for bullet_pos in list_of_bullets:
        bullet_pos[0] += bullet_pos[2][0]
        bullet_pos[1] += bullet_pos[2][1]
        if not (0 <= bullet_pos[0] < window.get_width() and 0 < bullet_pos[1] < window.get_height()):
            del list_of_bullets[list_of_bullets.index(bullet_pos)]
            continue

    keys = pygame.key.get_pressed()
    rocket_rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * velocity
    rocket_rect.y += (keys[pygame.K_UP] - keys[pygame.K_DOWN]) * velocity
    rocket_rect.clamp_ip(window.get_rect())
    
    window.fill(0)
    for bullet_pos in list_of_bullets:
        window.blit(bullet, bullet.get_rect(center = (round(bullet_pos[0]),round(bullet_pos[1]))))
    blit_point_to_mouse(window, rocket, *rocket_rect.center)
    pygame.display.flip()

pygame.quit()
exit()
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1509  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1345  
  IPD(Integrated Product Development)流程是一套先进的产品开发管理体系,旨在通过整合跨部门资源,实现产品的高效开发与交付。在IPD流程中,确保项目按时交付是至关重要的,它直接关系到企业的市场竞争力和客户满意度。以下将从多个关键方面探讨如何在IPD流程阶段确保项目按时交付。精准的项目规划项...
IPD流程分为几个阶段   21  
  IPD(Integrated Product Development)流程是一套先进的产品开发管理体系,旨在缩短产品上市时间、提高产品质量、降低成本并增强企业的市场竞争力。深入理解IPD流程阶段的关键要素,对于企业成功实施IPD,实现产品开发的高效运作至关重要。IPD流程的概念与重要性IPD流程强调将产品开发视为一个整...
IPD测试流程   18  
  IPD(Integrated Product Development)产品开发流程是一套先进的、旨在提高产品开发效率与质量的管理体系。在这个体系中,评审环节起着至关重要的作用,它们如同关卡,确保产品在各个阶段都朝着正确的方向前进,符合市场需求和企业战略。其中有四个评审环节尤为关键,它们分别在不同阶段对产品进行全面审视,...
研发IPD流程   20  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用