954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pygame - Shoot bullet towards mouse click!

Hi everyone. Well I've been trying to figure this out for a quite a long time now by looking at some examples and searching the web, reading some tutorials and so on, but I don't even know where to start yet. Can you explain to me about the sprites too and how I can use them or at least link me to a tutorial? But my main concern is how am I suppose to get the mouse click position and make the bullet travel in its direction until it disappears from the screen or it hits an object (Which I don't have yet as well).

Thanks, Dan08.

Dan08
Junior Poster
133 posts since Jul 2009
Reputation Points: 18
Solved Threads: 9
 

For a look at pygame sprites see:
http://www.cs.iupui.edu/~aharris/pygame/ch06/basicSprite.py

Here is an example for finding the mouse position ...

import pygame as pg

# pygame uses (r, g, b) tuples for color
red = (255, 0, 0)
white = (255, 255, 255)

screen = pg.display.set_mode((500, 500))
pg.display.set_caption("click the mouse on the window ...")

# create a little red square
red_square = pg.Surface((30, 30))
red_square.fill(red)

# starting position
xy_position = (100, 100)

# set up the event loop
running = True
while running:
    event = pg.event.poll()
    keyinput = pg.key.get_pressed()
    # exit on corner 'x' click or escape key press
    if keyinput[pg.K_ESCAPE]:
        raise SystemExit
    elif event.type == pg.QUIT:
        running = False
    elif event.type == pg.MOUSEBUTTONDOWN:
        #print(event.pos)  # test
        xy_position = event.pos

    # this erases the old sreen
    screen.fill(white)
    # put the image on the screen at given position
    screen.blit(red_square, xy_position)
    # update screen
    pg.display.flip()
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Well, I know how to detect the mouse position and make the object rotate towards it, I just don't know how I'm supposed to make the bullet move forward until it disappears or hits an object (which doesn't exist yet). I've uploaded an example with the images and everything.

Thanks, Dan08.

Attachments shotgame.zip (3.64KB)
Dan08
Junior Poster
133 posts since Jul 2009
Reputation Points: 18
Solved Threads: 9
 

Find out formula for line going through mouse to where it points to and how to count distance between bullet and object (hint: math.hypot of delta)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: