0
votes

My code is printed below. It is just a simple program that has 2D motion.

bif="C:\\Users\\Andrew\\Pictures\\pygame pictures\\Background(black big).png"
mif="C:\\Users\\Andrew\\Pictures\\pygame pictures\\bullet.png"

import pygame, sys
from pygame.locals import *

pygame.init()

from timeit import default_timer


screen=pygame.display.set_mode((1000,1000),0,32)
background=pygame.image.load(bif).convert()
bullet=pygame.image.load(mif).convert_alpha()


##Lines##
color=(255,255,255)
screen.lock()
pygame.draw.line(background, color, (30,970), (585,970))
pygame.draw.line(background, color, (585,-190), (585,970))
pygame.draw.line(background, color, (30,-190), (30,970))
screen.unlock()
## Horizontal Movement##
x=0
speedx= 0
dmx=0
## Vertical motion##
y=-190
dmy=0
clock=pygame.time.Clock()
speedy= 0
acceleration= 0
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

My problem is right here:

    elif event.type == pygame.MOUSEBUTTONDOWN:
            ## Horizontal ##
            (mouseX, mouseY) = pygame.mouse.get_pos()    
            x=mouseX-87
            speedx= 0
            dmx=0
            X1 = mouseX-87
            ## Vertical ##
            y=mouseY-172
            dmy=0
            speedy= 0
            acceleration= 0
            Y1 = mouseY-172



        elif event.type == pygame.MOUSEBUTTONUP:
            (mouseX, mouseY) = pygame.mouse.get_pos()    

            ## Horizontal ##
            x=mouseX-87
            speedx= 100
            dmx=0
            X2 = mouseX-87
            ## Vertical ##
            y= mouseY-172
            dmy=0
            speedy= 1
            acceleration= .5
            y2 = mouseY - 87




    screen.blit(background, (0,0))
    screen.blit(bullet, (x, y))

I don't know how to make the bullet follow the mouse cursor.When the mouse button is pressed, the bullet appears and stays still, no matter how much the mouse cursor moves. When the mouse button is released, the bullet instantly appears at that point. How do I make the bullet follow the path of the mouse cursor?

Variables: dmx = distance moved on the x-axis dmy = distance moved on the y-axis The rest are self-explanatory.

2

2 Answers

1
votes

You need to add the statement pygame.display.update() after the statement screen.blit(bullet,(x,y))

0
votes

If you change

event.type == pygame.MOUSEBUTTONUP

to,

event.type == pygame.MOUSEMOTION, it will follow your mouse.