0

我正在尝试用 Python 和 pygame 制作一个自上而下的坦克射击游戏,但我无法让坦克运动正确。我的目标是让它朝着它所指向的方向前进,这将是angle变量。我期待答案可以更改上下代码,这很好,无论如何它都是暂时的。这是代码:

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()

提前致谢!

4

0 回答 0