简体   繁体   English

Pygame 画一条线我就是看不到?

[英]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.我是 OOP 的新手,并试图了解使用类和方法的要点。 In this particular case, I've used a class to create two red nodes and managed to use MOUSEBUTTONDOWN with my class.在这种特殊情况下,我使用 class 创建了两个红色节点,并设法通过我的 class 使用MOUSEBUTTONDOWN

However, when I try to use a MOUSEBUTTONDOWN event to draw a line, nothing seems to happen.但是,当我尝试使用MOUSEBUTTONDOWN事件绘制一条线时,似乎什么也没有发生。 I've used test print statements in multiple places to ensure that I'm "reaching" my class and that the method is executing.我在多个地方使用了测试打印语句,以确保我“到达”我的 class 并且该方法正在执行。 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.我还将 draw 语句从方法中移出,接近我的游戏循环的末尾并且它正确显示。

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. pygame.mouse.get_pressed()不是一个事件,而是给出鼠标按钮的当前 state。 Rnode represents a node and should not draw a line or handle the events. Rnode代表一个节点,不应该画线或处理事件。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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