Enemy Projectiles Attack Way To Fast Problem

2024/11/18 13:40:06

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class

    # enemys bulletsksud = pygame.image.load("heart.png")class Boolss(object):def __init__(self, x, y,color, xspeed, yspeed):self.x = xself.y = yself.xspeed = xspeedself.yspeed = yspeedself.ksud = pygame.image.load("heart.png")self.hitbox  = self.ksud.get_rect()self.rect  = self.ksud.get_rect()self.rect.topleft = (self.x,self.y)self.color = colorself.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEWdef draw(self, window):self.rect.topleft = (self.x,self.y)player_rect = self.ksud.get_rect(center = self.rect.center) player_rect.centerx += 0 # 10 is just an exampleplayer_rect.centery += 0 # 15 is just an examplewindow.blit(self.ksud, player_rect)self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEWwindow.blit(self.ksud,self.rect)

this is where it appends bullet like attack the player

          for shootss in shootsright:shootss.x += shootss.xspeedshootss.y += shootss.yspeedif shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:shootsright.pop(shootsright.index(shootss))if len(shootsright) < 1:start_x = round(enemyshoots1.x+enemyshoots1.width-107)start_y = round(enemyshoots1.y + enemyshoots1.height-50)target_x = playerman.x+playerman.width//2target_y = playerman.y+playerman.width//2dir_x, dir_y = target_x - start_x, target_y - start_ydistance = math.sqrt(dir_x**2 + dir_y**2)if distance > 0:shootsright.append(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))#------------------------------------------------------------------------------
Answer

The problem is here:

start_x = round(enemyshoots1.x+enemyshoots1.width-107)
start_y = round(enemyshoots1.y + enemyshoots1.height-50)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
dir_x, dir_y = target_x - start_x, target_y - start_y

If you print the dir_x and dir_y you'll see that the values are very high. You probably add this values each frame to the position of the bullet. That's why they leave the screen before you can really see them.

You could try this:

BULLET_SPEED = 5delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance

Consider that this solution doesn't rounds the values and pygame might throw an exception because of coordinates with float values. You should round the coordinates before using them for drawing.

https://en.xdnf.cn/q/118673.html

Related Q&A

How to find the maximum digit of an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.This p…

How to use python to generate a magazine cover? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why do I get this error? NoneType object has no attribute shape in opencv

Im working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last …

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…

How to assert that a function call does not return an error with unittest?

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.example:assert function(a,b) is not errororif not assertRaises fun…

How to calculate Python float-number-th root of float number

I found the following answer here on Stackoverflow:https://stackoverflow.com/a/356187/1829329But it only works for integers as n in nth root:import gmpy2 as gmpyresult = gmpy.root((1/0.213), 31.5).real…

Need help making a Hilbert Curve using numbers in Python

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To…

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why python doesnt see the members of quantumCircuit class qiskit

I`m trying to learn the programming on quantum computers. I have installed qiskit in VS Code (all qiskit extentions available in VS Code market) , python compilator (from Vs Code market "Python&qu…

Compare multiple lines in a text file using python

Im writing a program that is time tracking report that reports the time that was spent in each virtual machine, I`m having a problem comparing the txt file cause the only thing that changes are the num…