简体   繁体   中英

Pygame - event.pos not working in def main()

I've read some similar questions but couldn't understand so just gonna ask here...

import pygame as pg
from pygame import *
import sys, os

pg.init()
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, "images")
screen = pg.display.set_mode((1920, 1080))

class token(pg.sprite.Sprite):
    def __init__(self, path, x, y):
        super().__init__()
        self.image = pg.image.load(path)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.click = False
        token_group.add(self)

    def offset(self):
        self.offset_x = event.pos[0] - self.rect.x
        self.offset_y = event.pos[1] - self.rect.y

    def drag(self):
        self.rect.x = event.pos[0] - self.offset_x
        self.rect.y = event.pos[1] - self.offset_y

token_group = pg.sprite.Group()
token1 = token("images/token1.png", 1920/2, 1080/2)


def main():
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT:
                pg.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                  running = False
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    for x in token_group:
                        if x.rect.collidepoint(event.pos):
                            x.click = True
                            x.offset()
            elif event.type == MOUSEMOTION:
                for x in token_group:
                    if x.click:
                        x.drag()
            elif event.type == MOUSEBUTTONUP:
                for x in token_group:
                    x.click = False
                

        screen.fill((255, 255, 255))

        token_group.draw(screen)

        pg.display.update()

main()

If I don't put it in the main(), it works fine but this one has the error below.

File "C:\Users\zsa77\OneDrive\desktop\For_Python\project\ggggg.py", line 63, in <module>
    main()
  File "C:\Users\zsa77\OneDrive\desktop\For_Python\project\ggggg.py", line 47, in main
    x.offset()
  File "C:\Users\zsa77\OneDrive\desktop\For_Python\project\ggggg.py", line 21, in offset
    self.offset_x = event.pos[0] - self.rect.x
AttributeError: module 'pygame.event' has no attribute 'pos'

I'm doing this to make main() start when clicking the start button in the main menu. But if I make two def(main menu(), main()) and put main() in main menu(), the error occurs. The main menu() which is not written here didn't have a problem of event.pos, but main() did. The error says the problem is event.pos of class token, but I can't find a way to solve this. How should I do about this?

You need to pass the event object to the methods. Add an event parameter to the methods:

class token(pg.sprite.Sprite):
    def __init__(self, path, x, y):
        # [...]

    def offset(self, event):
        self.offset_x = event.pos[0] - self.rect.x
        self.offset_y = event.pos[1] - self.rect.y

    def drag(self, event):
        self.rect.x = event.pos[0] - self.offset_x
        self.rect.y = event.pos[1] - self.offset_y

Add the event argument when calling the methods:

def main():
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT:
                # [...]
            elif event.type == KEYDOWN:
                # [...]
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    for x in token_group:
                        if x.rect.collidepoint(event.pos):
                            x.click = True
                            x.offset(event)     # <---
            elif event.type == MOUSEMOTION:
                for x in token_group:
                    if x.click:
                        x.drag(event)           # <---
            elif event.type == MOUSEBUTTONUP:
                # [...]

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