Pygame射击到位置

时间:2019-02-07 00:31:59

标签: python pygame pythagorean

我一直在尝试将我正在制作的游戏(基于《狂神王国》改版为游戏)的功能实现,然后点击即可拍摄。为此,我一直在尝试使用毕达哥拉斯(Pythagoras)和三角形的越过或渐变规则来计算x和y每次必须走多远才能保持稳定的步伐,并在每次单击时保持该步伐。我到过的所有地方,人们一直在使用angles和python 2,而我目前在python 3中工作,因此使代码兼容比问问题更棘手。我的代码使用类来创建障碍和纹理。我正在使用数组在gameloop中存储和创建项目符号,并使用pygames矩形功能将其绘制到屏幕上。请参见下面的代码(并非包括所有代码)。

import pygame, random, time
pygame.init()
shoot= []

class Projectiles(object):
    def __init__(self, pos):
        self.rect = pygame.Rect(pos[0], pos[1], 20, 20)
        shoot.append(self)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                quit()

            if event.key == pygame.K_a:
                movement_status = pcharl
                x_change = 5
            if event.key == pygame.K_w:
                movement_status = pcharb
                y_change = 5
            if event.key == pygame.K_s:
                movement_status = pchar
                y_change = -5
            if event.key == pygame.K_d:
                movement_status = pcharr
                x_change = -5
            if event.key == pygame.K_e:
                pygame.display.toggle_fullscreen()

            if event.key == pygame.K_SPACE:
                shooting = True
                if movement_status == pcharl:
                    Projectiles((x, y))
                    direction = "left"
                    bx_change = -8
                    shooting = True
                if movement_status == pcharr:
                    Projectiles((x+30, y))
                    direction = "right"
                    bx_change = 8
                    shooting = True
                if movement_status == pcharb:
                    Projectiles((x + 30, y))
                    direction = "up"
                    by_change = -8
                    shooting = True
                if movement_status == pchar:
                    Projectiles((x + 30, y))
                    direction = "down"
                    by_change = 8
                    shooting = True
        if event.type == pygame.KEYUP:
            x_change = 0
            y_change = 0
        if event.type == pygame.MOUSEBUTTONDOWN:
            xm,ym = pygame.mouse.get_pos()
            shooting_arr = True
            if movement_status == pchar:
                Projectiles((x + 60, y))
                bullx = 560
            if movement_status == pcharb:
                Projectiles((x + 60, y))
                bullx = 560
            if movement_status == pcharr:
                Projectiles((x + 60, y))
                bullx = 560
            if movement_status == pcharl:
                Projectiles((x, y))
                bullx = 500
            for bullet in shoot:
                pygame.draw.rect(screen, (255, 100, 0), bullet.rect)
                xm = xm - bullx
                ym = ym - 500
                if xm % 2 == 0 and ym % 2 == 0:
                    xm = xm / 2
                    ym = ym / 2
                    xm = xm / ym
                    ym = ym / xm
                bx_change = xm
                by_change = ym
                print(str(xm) + " " + str(ym))
                xm = 0
                ym = 0
                shooting = True
    screen.fill((225, 50, 25))

    if shooting:
        for bullet in shoot:
            pygame.draw.rect(screen, (255, 0, 255), bullet.rect)
            bullet.rect.x += bx_change
            bullet.rect.y += by_change
            if bullet.rect.x <= 0:
                shoot.remove(bullet)
            if bullet.rect.x >= 1000:
                shoot.remove(bullet)
            if bullet.rect.y <= 0:
                shoot.remove(bullet)
            if bullet.rect.y >= 1000:
                shoot.remove(bullet)

x和y等于500,屏幕尺寸为1000x1000

1 个答案:

答案 0 :(得分:0)

通过从鼠标光标位置减去玩家位置来创建矢量。然后使用该矢量将其添加到弹丸位置来移动弹丸。

这是一个简单的例子:

import pygame
import math

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((32, 32))
        self.image.fill((0, 0, 0))
        self.image.set_colorkey((0, 0, 0))
        pygame.draw.polygon(self.image, pygame.Color('dodgerblue'), ((0, 0), (16, 32), (32, 0)))
        self.org_image = self.image.copy()
        self.rect = self.image.get_rect(center=(200, 200))
        self.direction = pygame.Vector2(0, 1)

    def update(self, events, dt):
        for e in events:
            if e.type == pygame.MOUSEBUTTONDOWN:
                self.groups()[0].add(Projectile(self.rect.center, self.direction))


        self.direction = pygame.Vector2(pygame.mouse.get_pos()) - self.rect.center
        self.image = pygame.transform.rotate(self.org_image, math.degrees(math.atan2(*self.direction)))
        self.rect = self.image.get_rect(center=self.rect.center)

class Projectile(pygame.sprite.Sprite):
    def __init__(self, pos, direction):
        super().__init__()
        self.image = pygame.Surface((8, 8))
        self.image.fill((0, 0, 0))
        self.image.set_colorkey((0, 0, 0))
        pygame.draw.circle(self.image, pygame.Color('orange'), (4, 4), 4)
        self.rect = self.image.get_rect(center=pos)
        self.direction = direction.normalize()
        self.pos = pygame.Vector2(self.rect.center)

    def update(self, events, dt):
        self.pos += self.direction * dt
        self.rect.center = self.pos
        if not pygame.display.get_surface().get_rect().contains(self.rect):
            self.kill()

def main():
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    sprites = pygame.sprite.Group(Player())
    clock = pygame.time.Clock()
    dt = 0

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
        sprites.update(events, dt)
        screen.fill((30, 30, 30))
        sprites.draw(screen)
        pygame.display.update()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()

enter image description here