1

I have fixed the problem pointed out in this post, but now I am confused on how to point the gray rectangle (barrel) towards my mouse. Could somebody please help? Thanks!

Here is my code:

import pygame
import math

pygame.init()

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

pygame.display.set_caption("diep.io")

screen.fill((255,255,255))

auto_shoot = False

class Bullet:
  def __init__(self, x_move, y_move, x_loc, y_loc):
    self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
    self.x_move = x_move
    self.y_move = y_move
    self.x_loc = x_loc
    self.y_loc = y_loc
    self.bullet_rect = self.image.get_rect()
  
  def update(self):
    self.x_loc += self.x_move
    self.y_loc += self.y_move   
    self.bullet_rect.center = round(self.x_loc), round(self.y_loc)

    rect = screen.blit(self.image, self.bullet_rect)
    if not screen.get_rect().contains(rect):
      bullets.remove(self)

    if self.x_loc > 400 or self.y_loc > 400:
      bullets.remove(self)

bullet = None
bullets = []

while True:
  screen.fill((255, 255, 255))
  pygame.draw.rect(screen, (100, 100, 100), (205, 193, 25, 15)) # This is the rectangle I want to point at my mouse
  pygame.draw.circle(screen, (82, 219, 255), (200, 200), 15)
  for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONUP:
      x = pygame.mouse.get_pos()[0] - 200
      y = pygame.mouse.get_pos()[1] - 200
      pythag = float(math.sqrt(x**2 + y**2))
      bullets.append(Bullet(x/pythag, y/pythag, 200, 200))
  for bullet in bullets:
    bullet.update()
  pygame.display.update()
  pygame.time.delay(10)

Note: This is different from other problems because I am trying to point a rectangle at my mouse, and other posts are pointing an image at the mouse. That is not what I want to do. Secondly, pygame rectangles are defined by their bottom left corner, so this further complicates things.

  • 1
    To get your question answered, please make a new, self-sufficient post with your latest code, a clear description of what you're trying to accomplish, and what you've tried. That way, someone who has not seen the previous thread can know what you are talking about. Linking to the previous issue is ok, but it should not be the entirety of your post. – stelioslogothetis Dec 09 '21 at 00:44
  • I have fixed my code and clarified what I am asking. Now is my post good, and can it be re-opened? – ᴇɴᴅᴇʀᴍᴀɴ Dec 11 '21 at 00:02

1 Answers1

2

For the rotation of the bullets see How to move a sprite according to an angle in Pygame and calculating direction of the player to shoot PyGmae.
Compute the angle of the direction vector grees(math.atan2(-y_move, x_move)) and rotate the bullet image:

class Bullet:
    def __init__(self, x_move, y_move, x_loc, y_loc):
        self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
        self.image = pygame.transform.rotate(self.image, math.degrees(math.atan2(-y_move, x_move)))

Rotating the rectangle is a little trickier. Draw the cannon on a pygame.Surface:

cannon = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.rect(cannon, (100, 100, 100), (30, 17, 25, 15))
pygame.draw.circle(cannon, (82, 219, 255), (25, 25), 15)

See How do I rotate an image around its center using PyGame?. Write a function to rotate and blit a Surface:

def blitRotateCenter(surf, image, center, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
    surf.blit(rotated_image, new_rect)

Use the function in the application loop to draw the cannon:

while True:
    # [...]
    
    x = pygame.mouse.get_pos()[0] - 200
    y = pygame.mouse.get_pos()[1] - 200
    angle = math.degrees(math.atan2(-y, x)) 
    blitRotateCenter(screen, cannon, (200, 200), angle)
    
    # [...]

See also How to rotate an image(player) to the mouse direction? and How can you rotate the sprite and shoot the bullets towards the mouse position?.


Complete example:

import pygame
import math

pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("diep.io")
screen.fill((255,255,255))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x_move, y_move, x_loc, y_loc):
        #self.image = pygame.image.load("C:\\Users\\ender\\OneDrive\\Documents\\repos\\Bullet.png")
        self.image = pygame.Surface((20, 5), pygame.SRCALPHA)
        self.image.fill((255, 0, 0))
        self.image = pygame.transform.rotate(self.image, math.degrees(math.atan2(-y_move, x_move)))
        self.x_move = x_move
        self.y_move = y_move
        self.x_loc = x_loc
        self.y_loc = y_loc
        self.bullet_rect = self.image.get_rect()
    
    def update(self):
        self.x_loc += self.x_move
        self.y_loc += self.y_move   
        self.bullet_rect.center = round(self.x_loc), round(self.y_loc)
        rect = screen.blit(self.image, self.bullet_rect)
        if not screen.get_rect().contains(rect):
            bullets.remove(self)
        if self.x_loc > 400 or self.y_loc > 400:
            bullets.remove(self)

cannon = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.rect(cannon, (100, 100, 100), (30, 17, 25, 15))
pygame.draw.circle(cannon, (82, 219, 255), (25, 25), 15)

def blitRotateCenter(surf, image, center, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = center).center)
    surf.blit(rotated_image, new_rect)

bullet = None
bullets = []
auto_shoot = False
run = True
while run:
    x = pygame.mouse.get_pos()[0] - 200
    y = pygame.mouse.get_pos()[1] - 200
    angle = math.degrees(math.atan2(-y, x))  
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONUP:
            pythag = float(math.sqrt(x**2 + y**2))
            bullets.append(Bullet(x/pythag, y/pythag, 200, 200))
    
    screen.fill((255, 255, 255))
    for bullet in bullets:
        bullet.update()
    blitRotateCenter(screen, cannon, (200, 200), angle)
    pygame.display.update()
    clock.tick(100)

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174