【问题标题】:how to write Object-oriented programming in python如何在python中编写面向对象编程
【发布时间】:2022-10-05 03:30:06
【问题描述】:

我正在努力学习pygame,p5.js很容易写,但是pygame很难理解,因为我没有做面向对象的编程Cha1.尺寸,我不断收到错误:

类型错误:窗格。在里面() 缺少 1 个必需的位置参数:“大小”

不知道为什么它一直发生,因为我认为我通过添加返回来修复错误:

def get_Size(self): 返回 self.Size

import pygame
import sys
from pygame.locals import *



white = (255,255,255)
black = (0,0,0)


xPos = 100
yPos = 200
#size=heigh=width
Size = 25
xSize = 200 
ySize = 200



'''
class Character(object):
    def __init__(self, Size):
        pygame.init()
        self.Size = Size
    
        Cha1 = Character(25)
        Cha2 = Character(25)
'''



class Pane(object):
    def __init__(self, Size):
        pygame.init()
        
        self.Size = Size
    

        self.font = pygame.font.SysFont('Arial', Cha1.Size)
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()

    def get_Size(self):
        return self.Size

    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 100))
        self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 10))
        pygame.display.update()

if __name__ == '__main__':
    Pan3 = Pane()
    Cha1 = Pane(25)
    Pan3.addRect()
    Pan3.addText()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

这就是它在 p5.js 中的样子,但我希望它在 pygame 中:

  var Char1 = {
  Size: 12
  };


function setup() {
  createCanvas(400, 400);
}


function textHere(){
  
  fill(0, 0, 0);
  textSize(Char1.Size);
  text("Hehe", 20, 70);
}


function RectHere(){
  fill(0,255, 240);
  rect(10, 30, 100,199);
}

function draw() {
  background(220);
  
  RectHere();
  textHere();
  
}

【问题讨论】:

    标签: python oop


    【解决方案1】:

    我看到你的错误,当你写 Pan3 = Pane() 时,你忘了把大小放在 Pane() 里面,因为当你定义那个特定的类时,你添加了一个 Size 参数,所以每当你必须调用 Pane() 时,你必须输入一个数字代表大小。

    还有一件事,如果你是 pygame 或 python 的新手(我只是假设)为什么从面向对象编程开始,我的意思是我理解你所有的代码,但是你可以在没有面向对象编程的情况下做同样的事情最好不要混淆自己。

    import pygame
    import sys
    from pygame.locals import *
    
    
    
    white = (255,255,255)
    black = (0,0,0)
    
    
    xPos = 100
    yPos = 200
    #size=heigh=width
    Size = 25
    xSize = 200 
    ySize = 200
    
    
    
    '''
    class Character(object):
        def __init__(self, Size):
            pygame.init()
            self.Size = Size
        
            Cha1 = Character(25)
            Cha2 = Character(25)
    '''
    
    
    
    class Pane(object):
        def __init__(self, Size):
            pygame.init()
            
            self.Size = Size
        
    
            self.font = pygame.font.SysFont('Arial', Cha1.Size)
            pygame.display.set_caption('Box Test')
            self.screen = pygame.display.set_mode((600,400), 0, 32)
            self.screen.fill((white))
            pygame.display.update()
    
        def get_Size(self):
            return self.Size
    
        def addRect(self):
            self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
            pygame.display.update()
    
        def addText(self):
            self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 100))
            self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 10))
            pygame.display.update()
    
    if __name__ == '__main__':
        Pan3 = Pane(25) #put a size value inside this
        Cha1 = Pane(25)
        Pan3.addRect()
        Pan3.addText()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit(); sys.exit();
    

    【讨论】:

    • 你能写代码吗,因为我刚刚收到新错误: Pan3 = Pane(Size)#add Size Cha1 = Pane(25)#name 'Cha1' is not defined。而且我不知道哪个是“OOP”,因为我认为“类”是 OOP,直到你说它不是 OOP,所以你能在代码中写下我在类和 OOP 中的错误。基本上是我现在使用的“1 in class”和“1 in OOP”。
    • 您能否详细解释一下您的意思,我似乎无法理解您在此评论中的意思。
    • 简单,只需通过编写“代码中的答案”来更正代码 - 我的目标是“Cha1.Size”而不是“Size”,仅类似于 p5.js 中的 OOP
    • 请检查我所做的编辑,希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多