简体   繁体   中英

Python/Pygame; MOUSEBUTTONDOWN event

Okay I'm pretty new to using Pygame, I'm really just playing around with some of the methods and events. So far i pretty much have an image that moves around the pygame frame and bounces off any of the edges of the frame when it hits it. if the image touches the top of the frame it will increase a count variable by 1 which will be displayed on the screen. I then wanted to add a feature whereby if I clicked the image which was moving it would also add one onto the count variable. When i added this code in however (I think because the function operates on a loop), depending how long you hold the mouse down, count increases by a multiple of 8. I want to make it so that no matter how long i hold the mouse down for, the event stored inside the MOUSEBUTTONDOWN handler will only fire once. what am I doing wrong?

import pygame, sys
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
screen =pygame.display.set_mode((600,400))
ball = pygame.image.load("homers.png")
ball = pygame.transform.scale(ball,(225,200))
x=200
y=100
left = True
up = True
color = 67,143,218
def text_objects(text,font):
    text_surface = font.render(text,True, color)
    return text_surface,text_surface.get_rect()

def message_display(text,x,y,z):
    largeText = pygame.font.Font('freesansbold.ttf',z)
    TextSurf,TextRect = text_objects(text,largeText)
    TextRect.center = (x,y)
    screen.blit(TextSurf,TextRect)

def hi(x,y,p,z):

    message_display(x,y,p,z)
count = 0
message_count = str(count)
while True: # main game loop
        screen.fill((180,0,0))
        screen.blit(ball,(x,y))



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

        hi(message_count,x,y,140)
        hi("How Many Times Has Homer Hit His Head?",300,200,20)

        if event.type == pygame.MOUSEBUTTONDOWN:
            # Set the x, y postions of the mouse click

            if ball.get_rect().collidepoint(x, y):
                count = count+1

        if event.type == pygame.MOUSEBUTTONUP:
            0

        if left == True:

            x=x-10
        if x == -100:
            left =False
        if left == False:

            x=x+10
        if x == 450:
            left = True
        if up == True:

            y=y-10
        if y == -20:
            up =False
            count = count+1
            message_count = str(count)
            hi(message_count,x,y,140)
        if up == False:

            y=y+10
        if y== 230:
            up =True
        pygame.display.update()

You have to fix the indentation of your code:

while True: # main game loop
    screen.fill((180,0,0))
    screen.blit(ball,(x,y))

    hi(message_count,x,y,140)
    hi("How Many Times Has Homer Hit His Head?",300,200,20)

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

         # this has to be part of the for loop
         if event.type == pygame.MOUSEBUTTONDOWN:
             if ball.get_rect().collidepoint(x, y):
                 count = count+1

    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM