-1
I’m making a game in python using pygame. The game consists of a player who shoots small bullets depending on the position of the mouse and itself. Everything runs smoothly, the player moves as I want, the program records the position of the mouse, when I click on SPACE the program reads it and "shoots" a bullet, makes all the correct bullet calculations... The only problem is the bullet doesn’t show up on the screen, I’ve already tested the function where the bullet is "drawn" and the function that determines the position of the bullet and is being read correctly. Here is the code that I consider important:
Bullet code
class Bullet:
doDraw = False
def __init__(self, startx, starty, endx, endy):
self.startx = startx
self.starty = starty
self.endx = endx
self.endy = endy
self.framenum = 0
self.shootBullet()
def shootBullet(self):
self.declive = (self.endy-self.starty) / (self.endx-self.startx)
self.bulletx = self.startx
self.bullety = self.starty
self.doDraw = True
def draw(self):
self.GetPosition()
pygame.draw.rect(screen, (200, 200, 25), pygame.Rect(self.bulletx, self.bullety, 10, 10))
def GetPosition(self):
self.framenum += 1
if self.endx > self.startx:
self.bulletx += 1*self.framenum
elif self.endx < self.startx:
self.bulletx -= 1*self.framenum
self.bullety += self.declive*self.framenum
if self.bulletx < 0 or self.bulletx > 800 or self.bullety < 0 or self.bullety > 600:
self.stop()
def stop(self):
self.doDraw = False
Code in the Main Loop
if keyPressed[pygame.K_SPACE]:
if 'bullet' in globals():
if bullet.doDraw == False:
bullet = Bullet(p.x, p.y, mousePoint[0], mousePoint[1])
bullet.doDraw = True
else:
bullet = Bullet(p.x, p.y, mousePoint[0], mousePoint[1])
bullet.doDraw = True
if 'bullet' in globals():
if bullet.doDraw == True:
bullet.draw()
Who gave the downvote on the question, please, the idea of downvotes is not "I do not know how to answer so I will give a negative vote". The code snippet is concise - there is something missing to be executable separately (the screen startup, and the mainloop code), but the code snippets are very clear, there is no more than needed, and allows to say a lot about what happens.
– jsbueno