【问题标题】:Python - Pygame bullet movementsPython - Pygame 子弹动作
【发布时间】:2013-11-25 02:34:57
【问题描述】:

我正在尝试找到一种方法来获得船的旋转并将其应用到光束上,以便它沿被击中的方向行进。我真的不知道该怎么做,但这里是我的代码:请原谅它的混乱,我把 cmets 放进去让你知道是什么。

import sys, pygame, math, time;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
fire_beam = ('beams.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
f_beam = pygame.image.load(fire_beam).convert_alpha()
f_beam = pygame.transform.scale(f_beam, (50, 50))
f_beam_rect = f_beam.get_rect()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
space_ship_rect = space_ship.get_rect()
space_ship_rect.centerx = 375
space_ship_rect.centery = 300
speed = 3.5
pressed_down = 0
while True:
    clock.tick(60)
    screen.blit(bk, (0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            pressed_down = 1
        elif event.type == MOUSEBUTTONUP:
            pressed_down = 0
        if pressed_down == 1:
            x, y = pygame.mouse.get_pos()
            x1, y1 = x - space_ship_rect.x, y - space_ship_rect.y
            angle = math.atan2(y1, x1)
            dx = speed*math.cos(angle)
            dy = speed*math.sin(angle)
            movex = space_ship_rect.centerx = space_ship_rect.centerx + dx#ship x
            movey = space_ship_rect.centery = space_ship_rect.centery + dy#ship y
        if event.type == MOUSEMOTION:
            x1, y1 = pygame.mouse.get_pos()
            x2, y2 = space_ship_rect.x, space_ship_rect.y
            dx, dy = x2 - x1, y2 - y1
            rads = math.atan2(dx, dy)
            degs = math.degrees(rads)
            display_s = pygame.transform.rotate(space_ship, (degs))#rotation of ship
        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            #Is it possible for me to get the degree rotation of the space_ship and apply it to here so the beam will travel in the direction it was shot in?
    screen.blit(display_s, (space_ship_rect.centerx, space_ship_rect.centery))
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    pygame.display.update()

【问题讨论】:

  • 在发布代码之前请仔细检查缩进(Python spc。)我已经格式化了代码,但我希望你确保它。
  • 您的意思是laser beam 是从宇宙飞船发射的,并且您想向宇宙飞船的方向发射光束?
  • @adil 是的,缩进现在是正确的抱歉,是的,这就是我正在寻找的 adil :)
  • 你有宇宙飞船的角度(和 dx,dy)所以用它来做激光束。
  • 那你为什么不只计算你的十字准线和宇宙飞船之间的斜率,我也会建议你在你的宇宙飞船上安装一把枪(一个点)并用它来计算斜率

标签: python pygame


【解决方案1】:

我发现你的船舶旋转有问题。

如果你创建旋转的宇宙飞船display_s,你会得到与space_ship不同大小的图像,所以你必须得到display_s矩形并将宇宙飞船center分配给display_s中心。

 display_s_rect = display_s.get_rect( center=spaceship_rect.center)

现在你必须使用display_s_rect 来显示display_s

 screen.blit(display_s, display_s_rect)

顺便说一句:

blit() 期望在屏幕上放置 blited 图像左上角的位置。

screen.blit(display_s, (space_ship_rect.centerx, space_ship_rect.centery))

display_s 的左上角将放在(space_ship_rect.centerx, space_ship_rect.centery),但我想你想把display_s 中心放在(space_ship_rect.centerx, space_ship_rect.centery)

将中心值(如前所述)分配给(space_ship_rect.centerx, space_ship_rect.centery),但在blit() 中使用(space_ship_rect.x, space_ship_rect.y)

您可以使用space_ship_rect 代替blit() 中的(space_ship_rect.x, space_ship_rect.y),结果相同。


我认为您在blit() 中的mousec 位置也有同样的问题。

获取mousec矩形,将鼠标位置分配给矩形中心,然后使用矩形x,y进行blit。

 mousec_rect = mousec.get_rect( center = pygame.mouse.get_pos() )
 screen.blit(mousec, mousec_rect)

编辑:

我修改后你的主循环 - 现在船正在旋转

display_s = space_ship # default value at start when ship wasn't rotate

while True:
    clock.tick(60)
    screen.blit(bk, (0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            pressed_down = 1
        elif event.type == MOUSEBUTTONUP:
            pressed_down = 0
        if pressed_down == 1:
            x, y = pygame.mouse.get_pos()
            x1, y1 = x - space_ship_rect.x, y - space_ship_rect.y
            angle = math.atan2(y1, x1)
            dx = speed*math.cos(angle)
            dy = speed*math.sin(angle)
            movex = space_ship_rect.centerx = space_ship_rect.centerx + dx#ship x
            movey = space_ship_rect.centery = space_ship_rect.centery + dy#ship y
        if event.type == MOUSEMOTION:
            x1, y1 = pygame.mouse.get_pos()
            x2, y2 = space_ship_rect.centerx, space_ship_rect.centery
            dx, dy = x2 - x1, y2 - y1
            rads = math.atan2(dx, dy)
            degs = math.degrees(rads)

            display_s = pygame.transform.rotate(space_ship, (degs))#rotation of ship
            display_s_rect = display_s.get_rect(center = space_ship_rect.center)            

        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            #Is it possible for me to get the degree rotation of the space_ship and apply it to here so the beam will travel in the direction it was shot in?
            pass

    screen.blit(display_s, display_s_rect)
    #screen.blit(display_s, space_ship_rect)

    pos = pygame.mouse.get_pos()
    mousec_rect = mousec.get_rect(centerx=pos[0], centery=pos[1])

    screen.blit(mousec, mousec_rect )

    pygame.display.update()

【讨论】:

    【解决方案2】:

    在 While 循环之前:
    beam_speed=<somevalue>
    f_beam_rect=space_ship_rect #initialise beam position
    fired=False #check if beam is fired or not

    While 循环内部

    ##If beam is fired(right click) blit images through-out the path
    if fired:
        b_angle = math.atan2(by1, bx1)
        bdx = beam_speed*math.cos(b_angle)
        bdy = beam_speed*math.sin(b_angle)
        f_beam_rect.centerx = f_beam_rect.centerx + bdx
        f_beam_rect.centery = f_beam_rect.centery + bdy
        screen.blit(f_beam,f_beam_rect)
    
    for event in pygame.event.get():
    
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    
    ###If mouse is clicked then find the direction (mouse position and spaceship)
    
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            bx, by = pygame.mouse.get_pos()
            bx1, by1 = bx - space_ship_rect.x, by - space_ship_rect.y
            fired=True
    

    现在您可以创建一个函数来检查光束是否与敌方物体发生碰撞,如果为 True,则停止对光束和该物体进行 blitting。
    同时重置变量fired=False 并重置光束位置以再次从宇宙飞船开始f_beam_rect=space_ship_rect

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多