代码之家  ›  专栏  ›  技术社区  ›  Marcus Brown

如何使用类在pygame中绘制圆?复制

  •  0
  • Marcus Brown  · 技术社区  · 1 年前

    我正在尝试构建一个小型的太空入侵者类型的游戏,我想在使用类的同时画很多东西(为了更高效)。然而,虽然我的程序在我想要的时候运行,但它从不显示圆圈,只显示空白背景。

    我尝试的代码如下所示:

    import math
    import random
    import time
    import datetime
    import os
    import re
    import sys
    import pygame
    
    pygame.init()
    
    canvas = pygame.display.set_mode((600,600))
    
    class Invader:
        def __init__(self,xp,yp):
            self.xp = int(xp)
            self.yp = int(yp)
        def Movement(self):
            pygame.draw.circle(canvas,(0,255,0),(self.xp,self.yp),0)
            
    
    invader = Invader(300,300)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        invader.Movement()
        pygame.display.update()
    

    我本想看到显示窗口上的圆圈,但正如我所说,除了黑色背景外,什么都没有出现。

    1 回复  |  直到 1 年前
        1
  •  -1
  •   user24714692    1 年前

    您需要通过一个半径:

    pygame.draw.circle(canvas, (0, 255, 0), (self.xp, self.yp), 100)
    canvas.fill((0, 0, 0))
    

    密码

    import sys
    import pygame
    
    pygame.init()
    canvas = pygame.display.set_mode((600, 600))
    
    class Invader:
        def __init__(self, xp, yp):
            self.xp = int(xp)
            self.yp = int(yp)
    
        def movement(self):
            pygame.draw.circle(canvas, (0, 255, 0), (self.xp, self.yp), 100) 
    
    
    invader = Invader(300, 300)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        canvas.fill((0, 0, 0)) 
        invader.movement()
        pygame.display.update()