簡體   English   中英

將角度和速度轉換為 x,y 變化的問題

[英]problem converting angle and speed to x, y change

嗨,我正在嘗試制作一個關於用炮塔射擊敵人的游戲,但我遇到了一個問題,我無法將角度和速度轉換為 x 和 y 坐標的變化。 我現在正在做的是試圖解決這部分代碼中的問題:

def nextpos(pos, angle, speed):
    x, y = pos
    angle /= 180*math.pi
    y += math.sin(angle)*speed
    x += math.cos(angle)*speed
    return (x, y)

這是我的代碼代碼的rest:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    import pygame
    import math
    pygame.init()
    # set up the pygame window
    width, height = 1200, 800
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption(f'shooter')
    # game loop
    clock = pygame.time.Clock()
    playericon = pygame.image.load('playercannon.png')
    projectileicon = pygame.image.load('ballwithtrail.png')
    def player(angle):
        pos_org = [width/2, height/2]
        image_rotated = pygame.transform.rotate(playericon, -angle)
        pos_new = (pos_org[0] - image_rotated.get_rect().width / 2, pos_org[1] -   image_rotated.get_rect().height / 2)
        screen.blit(image_rotated, pos_new)
    def getangle():
        x = width/2
        y = height/2
        mousex, mousey = pygame.mouse.get_pos()
        angle = math.atan2((y-mousey), (x-mousex))*180/math.pi
        return int(angle - 90)
        def projectile(pos, angle):
        x, y = pos
        pos_org = [x, y]
        image_rotated = pygame.transform.rotate(projectileicon, -angle)
        pos_new = (pos_org[0] - image_rotated.get_rect().width / 2, pos_org[1] - image_rotated.get_rect().height / 2)
        screen.blit(image_rotated, pos_new)
    def nextpos(pos, angle, speed):
        x, y = pos
        angle /= 180*math.pi
        y += math.sin(angle)*speed
        x += math.cos(angle)*speed
        return (x, y)
   running = True
   inmenu = True
   bullet = False
   while running:
        clock.tick(60)
        pygame.display.flip()
        events = pygame.event.get()
        if inmenu:
            screen.fill((120, 120, 220))
            for event in events:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        inmenu = False
        else:
            screen.fill((123, 132, 231))
            if bullet:
                projectile(bulletpos, bulletangle)
                bulletpos = nextpos(bulletpos, bulletangle, 1)
            player(getangle())
            for event in events:
                if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                inmenu = True
                bullet = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                bullet = True
                bulletangle = getangle()
                bulletpos = (width/2, height/2)
        for event in events:
            if event.type == pygame.QUIT:
            pygame.quit()

我該如何解決這個問題?

您可以使用math.radians將角度轉換為弧度

angle = math.radians(angle)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM