[英]problem converting angle and speed to x, y change
Hi i am trying to make a game about shooting enemy's with a turret, but i ran into a problem i can't convert the angle and speed to a change in the x and y coordinates.嗨,我正在尝试制作一个关于用炮塔射击敌人的游戏,但我遇到了一个问题,我无法将角度和速度转换为 x 和 y 坐标的变化。 What i am doing now is trying to fix the problem in this part of code:我现在正在做的是试图解决这部分代码中的问题:
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)
Here is the rest of my code code:这是我的代码代码的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()
how do i solve this problem?我该如何解决这个问题?
You can convert your angle to radians using math.radians
您可以使用math.radians
将角度转换为弧度
angle = math.radians(angle)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.