I am trying to make a top-down tank shooter game in Python and pygame, but I can't get the tank movement right. My objective is to make it move forward in the direction it is pointing in which would be the angle
variable. I am expecting the answer to change the up and down code and that is fine, it was temporary anyway. Here is the code:
import pygame
from pygame.locals import QUIT
import math
# Initalizing everything
pygame.init()
pygame.display.set_caption('Top Down Tank Wars || Cloud Multiplayer')
screen = pygame.display.set_mode((600, 600))
# Loading, converting and coloring the space around the tank_body and
# tank_turret images white.
tank_body = pygame.image.load(
'../Python Projects/Tank Game/blue tank body.png'
).convert()
tank_body.set_colorkey((0, 0, 0))
tank_turret = pygame.image.load(
'../Python Projects/Tank Game/blue tank turret.png'
).convert()
tank_turret.set_colorkey((0, 0, 0))
class Tank(pygame.sprite.Sprite):
def __init__(self, startingX, startingY, starting_angle, speed):
super(Tank, self).__init__()
self.x = startingX
self.y = startingX
self.angle = starting_angle
self.speed = speed
def up(self):
self.y = self.y - self.speed
return self.y
def down(self):
self.y = self.y + self.speed
return self.y
def left(self):
self.angle = self.angle + self.speed
return self.angle
def right(self):
self.angle = self.angle - self.speed
return self.angle
running = True
x = 300
y = 300
angle = 0
speed = 0.05
tank = Tank(x, y, angle, speed)
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
# Set 'key_pressed' to the key that is being currently pressed
keys_pressed = pygame.key.get_pressed()
# Forward and backward manuevers
if (keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]):
y = tank.up()
if (keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]):
y = tank.down()
# Turning tank body
if (keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]):
angle = tank.left()
if (keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]):
angle = tank.right()
angle = round(angle)
screen.fill((255, 255, 255))
# Rotating tank and rendering it
tank_body_copy = pygame.transform.rotate(tank_body, angle)
screen.blit(
tank_body_copy,
(300 - int(tank_body_copy.get_width() / 2),
y - int(tank_body_copy.get_height() / 2))
)
# Rotating turret and rendering it
tank_turret_copy = pygame.transform.rotate(tank_turret, angle)
screen.blit(
tank_turret_copy,
(300 - int(tank_turret_copy.get_width() / 2),
y - int(tank_turret_copy.get_height() / 2))
)
pygame.display.flip()
# Quit pygame when main loop has finished
pygame.quit()
Thanks in advance!!