0

私が作成しているゲーム (Realm of the Mad God に基づく) に、クリックした場所を撃つことができる機能を実装しようとしています。これを行うために、私はピタゴラスと三角形の上昇率または勾配規則を使用して、安定したペースを維持し、クリックするたびにそのペースを維持するために、x と y が毎回移動する必要がある距離を計算しようとしました。私が見たところはどこでも、人々は角度と python 2 を使用していましたが、私は現在 python 3 で作業しているため、コードを互換性のあるものにすることは、単に尋ねるよりもトリッキーでした。私のコードでは、クラスを使用して障害物とテクスチャを作成しています。配列を使用してゲームループ内に弾丸を保存および作成し、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 です

4

1 に答える 1