简体   繁体   中英

Is Pygame Drawing a Line and I Just Can't See It?

I'm new to OOP and trying to get the gist of using Classes and Methods. In this particular case, I've used a class to create two red nodes and managed to use MOUSEBUTTONDOWN with my class.

However, when I try to use a MOUSEBUTTONDOWN event to draw a line, nothing seems to happen. I've used test print statements in multiple places to ensure that I'm "reaching" my class and that the method is executing. Nothing, however, can seem to make my red line appear.

I've also moved the draw statement out of the method to near the end of my game loop and it appears correctly.

What am I misunderstanding about classes and methods?

import pygame

    class Rnode():
        def __init__(self, x, y, image_rednode):
          self.x = x
          self.y = y
          self.image_rednode = image_rednode
          self.rect = self.image_rednode.get_rect()
          self.rect.topleft = (x, y)
          self.clicked = False
          self.wired = False
         
         # draw node line
    
        def put(self):
          
            screen.blit(self.image_rednode, (self.x, self.y))
    
          #get mouse position
            pos = pygame.mouse.get_pos()
          
          
          #check mouseover and clicked
            if self.rect.collidepoint(pos):
               if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                  self.clicked = True
                  print('gotcha' + str(self))
                  self.wired = True
            
             
             
               if pygame.mouse.get_pressed()[0] == 0:
                  self.clicked = False
    
        def draw_line(self):
            if pygame.mouse.get_pressed()[0]:
               self.pos = pygame.mouse.get_pos()
               pygame.draw.line(screen,red,(self.x + 15, self.y + 15),(self.pos), 3)
            
            
    
    # these are the colors 
    green = (48, 141, 70) 
    grey = (211, 211, 211)
    lime = (201, 223, 202)
    purplish = (116,137,192)
    orange = (234,168,0)
    brown = (59,47,47)
    blue = (0,91,150)
    red = (255,8,0)
    
    screen = pygame.display.set_mode((1280, 720))
    pygame.display.set_caption('Classy, Baby!')
    running = 1
    xb = pygame.image.load(r'xb7white.png').convert_alpha()
    rednode = pygame.image.load('redhole.svg').convert_alpha()
    rednode = pygame.transform.scale(rednode, (100, 100))
    
    # make node instances
    
    r1 = Rnode(300, 300, rednode)
    r2 = Rnode(500, 300, rednode)
    
    
    while running:
         screen.fill((0, 0, 0))
         event = pygame.event.poll()
         if event.type == pygame.QUIT:
             running = 0
         if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
        #  if event.type == pygame.MOUSEBUTTONDOWN: # if the user pressed a mouse button 
        #    pos = pygame.mouse.get_pos() # get the mouse pos 
        #    if g1.rect.collidepoint(pos):
          
          
         r1.put()
         r2.put()
         
         if r1.wired:
             r1.draw_line()
    
         
         pygame.display.flip()

pygame.mouse.get_pressed() is not an event, but gives the current state of the mouse buttons. Rnode represents a node and should not draw a line or handle the events. Handle the event in an event loop and add the lines to a list:

import pygame

class Rnode():
    def __init__(self, x, y, image_rednode):
        self.image_rednode = image_rednode
        self.rect = self.image_rednode.get_rect(center = (x, y))
    def put(self):
        screen.blit(self.image_rednode, self.rect)

class Line():
    def __init__(self, nodeFrom, nodeTo):
        self.form = nodeFrom
        self.to = nodeTo
    def draw(self):
        p1 = self.form.rect.center
        p2 = self.to.rect.center
        pygame.draw.line(screen, "yellow", p1, p2, 3)

screen = pygame.display.set_mode((300, 300))
pygame.display.set_caption('Classy, Baby!')
clock = pygame.time.Clock()

#rednode = pygame.image.load('redhole.svg').convert_alpha()
#rednode = pygame.transform.scale(rednode, (100, 100))
rednode = pygame.Surface((40, 40), pygame.SRCALPHA)
pygame.draw.circle(rednode, "red", (20, 20), 20)

nodes = [
    Rnode(100, 100, rednode), Rnode(200, 100, rednode),
    Rnode(100, 200, rednode), Rnode(200, 200, rednode)]
lines = []
start = None

running = 1
while running:
    clock.tick(60)
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = 0
        
        if event.type == pygame.MOUSEBUTTONDOWN:
            for node in nodes:
                if node.rect.collidepoint(event.pos):
                    if start and start != node:
                        lines.append(Line(start, node))
                        start = None
                    else:
                        start = node
                    break

                
    screen.fill((0, 0, 0))
    for line in lines:
        line.draw()
    if start:
        pygame.draw.line(screen, "yellow", start.rect.center, pygame.mouse.get_pos(), 3)  
    for node in nodes:
        node.put()
    pygame.display.flip()

pygame.quit()

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