-3

I'm making a simple game app in pygame and cannot figure out how to make the user repeatedly click on the enemy for interaction (like shooting) rather than just holding down the mouse. Please Help! Here is my code.

    import pygame, sys
    from pygame.locals import *
    from time import *

    bif="map.jpg"
    enemy="squarecharacter.png"

    pygame.init()
    screen=pygame.display.set_mode((1000,550), 0, 32)
    background=pygame.image.load(bif).convert()
    square1=pygame.image.load(enemy).convert_alpha()
    square1_x,square1_y=(300,300)
    clock=pygame.time.Clock()
    speed=50
    square_health1=100

    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        screen.blit(background, (0,0))
        screen.blit(square1, (square1_x,square1_y))

        mousepos_x,mousepos_y=pygame.mouse.get_pos()
        button1, button2, button3=pygame.mouse.get_pressed()
        if mousepos_x in range((int(square1_x)),(int(square1_x+50))) and mousepos_y in range((int(square1_y)),(int(square1_y+50))):              
            if button1==1:
                square_health1-=10
                button1-=1
                print str(square_health1)
                continue
5
  • 1
    Hi Zack, welcome to StackOverflow! Rather than simply posting all of your code, it's more useful to state explicitly what (and where) your problem is, what you have tried to do to fix it, and what the results of that were (error messages, runtime failures, incorrect output, etc.). Then, we will be able to provide more useful feedback.
    – nbrooks
    Dec 12, 2012 at 20:26
  • How do you expect to "make the user do something" through code? Do you mean "allow"?
    – madth3
    Dec 12, 2012 at 20:27
  • Yes sorry i am new to coding. But the problem is in the last part of my code. from the "if button==1:" line down. And yes I do mean allow.
    – Zack Tarr
    Dec 12, 2012 at 20:30
  • @ZackTarr Is that intentionally non-indented? Python is whitespace-sensitive, you know. Dec 12, 2012 at 21:30
  • no that is a mistake on putting the code on here, it is indented on my code.
    – Zack Tarr
    Dec 12, 2012 at 22:16

1 Answer 1

1

Alright, for starters i would recommend to look into classes, to make your code much more readable. Now about the mouse: Apart from the event type quit, there are other events as well. The one you are looking for is MOUSEBUTTONUP and MOUSEBUTTONDOWN. A simple check for coordinates would be:

for event in pygame.event.get():
    if event.type==QUIT:
        pygame.quit()
    if event.type == MOUSEBUTTONUP:
        print('Mouse clicked at coordinates'+ str(event.pos))

I would recommend to take a look at different data structures available in python. A usage of list to store many enemies would be advisable. Then you could simply look through each enemy and check if the mouse coordinates are inside a rectangle which represents the enemy.

The python tutorial is a great start, as well as the pygame documentation. It is really short and you will find many useful functions, like collision detection, sprite groups etc.

1
  • I have taken a class, I'm still learning this and just experimenting. But I did use a code just like this. Thank you.
    – Zack Tarr
    Dec 21, 2012 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.