Reputation: 304
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. 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:
#!/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?
Upvotes: 1
Views: 69
Reputation: 6056
You can convert your angle to radians using math.radians
angle = math.radians(angle)
Upvotes: 1