1

I've been trying and trying, but basically I wanna make a tank game which has a tank that can turn itself by the mouse to fire bullets; when you turn your mouse to a direction, the sprite will follow exactly. the problem is, I can't turn the tank with any code, no matter what.

import os
import pygame
import math

pygame.init()
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30)
icon = pygame.image.load('Sprite3.png')
pygame.display.set_icon((icon))
pygame.display.set_caption('DeMass.io')

class Tank(object):  # represents the bird, not the game
    def __init__(self):
        """ The constructor of the class """
        self.image = pygame.image.load('Sprite0.png')
        # the bird's position
        self.x = 0
        self.y = 0

    def handle_keys(self):
        """ Handles Keys """
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_DOWN] or key[pygame.K_s]:
            self.y += dist # move down

        elif key[pygame.K_UP] or key[pygame.K_w]:
            self.y -= dist # move up

        if key[pygame.K_RIGHT] or key[pygame.K_d]:
            self.x += dist # move right

        elif key[pygame.K_LEFT] or key[pygame.K_a]:
            self.x -= dist # move left


def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, (self.x, self.y))

w = 1900
h = 10000
screen = pygame.display.set_mode((w, h))

tank = Tank() # create an instance
clock = pygame.time.Clock()
connection_angle = 90

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() # quit the screen
            running = False
    
    
    screen.fill((255, 255, 255))  
    tank.draw(screen)  
    pygame.display.update()  
    tank.handle_keys() 
    clock.tick(100)
Red
  • 26,798
  • 7
  • 36
  • 58
BetterNot
  • 119
  • 7
  • 1
    There is nothing in your code about moving the tank with the mouse. It uses wasd and arrow keys – drum Nov 11 '20 at 23:07
  • Do you want to *move* the tank to the mouse-cursor, or have it *turn* towards the mouse-cursor? Or have the tank's Turret turn to face the cursor? Should the tank instantly turn to the mouse-cursor, or begin turning to that direction with some speed? Please be exact in your requirement. – Kingsley Nov 12 '20 at 00:24
  • @drum srry if i was a bit unclear. I want to add an extra function in this code so that the sprite turns with the mouse. Thx! – BetterNot Nov 12 '20 at 00:45

1 Answers1

1

You can use the atan2 function from the built-in math module to calcualte the angle between two coordinates, and then rotate your sprite accordingly:

import pygame
from math import atan2, degrees

wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40), pygame.SRCALPHA)
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect(topleft=(185, 180))

    def point_at(self, x, y):
        rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
        new_rect = rotated_image.get_rect(center=self.rect.center)
        wn.fill((0, 0, 0))
        wn.blit(rotated_image, new_rect.topleft)

player = Player()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            player.point_at(*pygame.mouse.get_pos())

    pygame.display.update()

Output:

enter image description here

Red
  • 26,798
  • 7
  • 36
  • 58
  • Is it possible to move the sprite as well? Thanks! – BetterNot Nov 12 '20 at 01:48
  • @BetterNot You mean make it follow the mouse? – Red Nov 12 '20 at 01:56
  • @BetterNot see https://stackoverflow.com/questions/64087982/how-to-make-smooth-movement-in-pygame/64088747#64088747 – Rabbid76 Nov 12 '20 at 05:36
  • @AnnZen Uh not quite, I hope to use "WASD" to move the sprite and the mouse to turn the sprite at the same time. I got the "WASD" part, but not the turning with mouse. Is it possible to put these two functions together? PS, is it possible to change the image into something else? Thanks! – BetterNot Nov 12 '20 at 12:56