Why Are My Pygame Bullets Not Moving Towards the Mouse?
Bullet Trajectory in Pygame: A Troubleshooting Guide
This discussion aims to address a common issue encountered when firing bullets toward the mouse position in Pygame: the bullets either don't move or exhibit erratic behavior.
Understanding the Problem
At the time of firing, the bullet's position and the direction vector toward the mouse need to be determined. However, the code provided does not set the direction vector correctly, causing the bullets to move unpredictably or remain stationary.
Solving the Issue
To rectify this issue, the direction vector should be calculated and normalized (converted to a unit vector) as follows:
-
Obtain the starting position and mouse position:
<code class="py">self.pos = (x, y) mx, my = pygame.mouse.get_pos()</code>
Copy after login -
Calculate the direction vector:
<code class="py">self.dir = (mx - x, my - y)</code>
Copy after login -
Normalize the vector:
<code class="py">length = math.hypot(*self.dir) if length == 0.0: self.dir = (0, -1) else: self.dir = (self.dir[0]/length, self.dir[1]/length)</code>
Copy after login -
Compute the angle of the vector:
<code class="py">angle = math.degrees(math.atan2(-self.dir[1], self.dir[0]))</code>
Copy after login
Rotating and Updating the Bullet
Once the direction is determined, the bullet can be rotated and updated according to its velocity:
-
Rotate the bullet:
<code class="py">self.bullet = pygame.Surface((7, 2)).convert_alpha() self.bullet.fill((255, 255, 255)) self.bullet = pygame.transform.rotate(self.bullet, angle)</code>
Copy after login -
Update the bullet's position:
<code class="py">self.pos = (self.pos[0]+self.dir[0]*self.speed, self.pos[1]+self.dir[1]*self.speed)</code>
Copy after login
Drawing the Rotated Bullet
To correctly display the rotated bullet, its bounding rectangle is used to center it at its correct position:
-
Get the rotated bullet's bounding rectangle:
<code class="py">bullet_rect = self.bullet.get_rect(center = self.pos)</code>
Copy after login -
Draw the bullet:
<code class="py">surf.blit(self.bullet, bullet_rect)</code>
Copy after login
The above is the detailed content of Why Are My Pygame Bullets Not Moving Towards the Mouse?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
