简体   繁体   中英

Getting quarter of a circle with pygame.draw.circle()

Me and my friend is trying to get a circle sprite, we did it with the rectangle but we are appearantly getting a quarter of a circle with our code. (btw "sirkel" means circle, and "firkant" mean rectangle in our language"

We have no idea why this is happening. It doesnt make any sense. We tried to change the position to see if there was something overlapping it, but there wasnt. We are using a website called Repl.it, so there might be some problems with that, because its pretty useul that it messes stuff up but not this much.

This is main.py:

import pygame
from firkant import Firkant
from sirkel import Sirkel
pygame.init()
PLAYER=0
screen = pygame.display.set_mode([600, 600])

from pygame.locals import (
  K_UP,
  K_DOWN,
  K_LEFT,
  K_RIGHT,
  K_ESCAPE,
  KEYDOWN,
  QUIT,
)

GREEN = (40,40,40)

pos1=60
pos2=235
pos3=410
posses = [pos1, pos2, pos3]

Firkant1 = Firkant(GREEN, 130, 130)
Firkant1.rect.x = pos1
Firkant1.rect.y = pos1
Firkant2 = Firkant(GREEN, 130, 130)
Firkant2.rect.x = pos2
Firkant2.rect.y = pos1
Firkant3 = Firkant(GREEN, 130, 130)
Firkant3.rect.x = pos3
Firkant3.rect.y = pos1
Firkant4 = Firkant(GREEN, 130, 130)
Firkant4.rect.x = pos1
Firkant4.rect.y = pos2
Firkant5 = Firkant(GREEN, 130, 130)
Firkant5.rect.x = pos2
Firkant5.rect.y = pos2
Firkant6 = Firkant(GREEN, 130, 130)
Firkant6.rect.x = pos3
Firkant6.rect.y = pos2
Firkant7 = Firkant(GREEN, 130, 130)
Firkant7.rect.x = pos1
Firkant7.rect.y = pos3
Firkant8 = Firkant(GREEN, 130, 130)
Firkant8.rect.x = pos2
Firkant8.rect.y = pos3
Firkant9 = Firkant(GREEN, 130, 130)
Firkant9.rect.x = pos3
Firkant9.rect.y = pos3
Sirkel1 = Sirkel((0,0,0), 400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(Firkant1, Firkant2, Firkant3, Firkant4, Firkant5, Firkant6, Firkant7, Firkant8, Firkant9, Sirkel1)

for a in posses:
  x = a
  for b in posses:
    pass
running = True
while running:
  for event in pygame.event.get():
    if event.type == KEYDOWN:
      if event.key == K_ESCAPE:
        running = False
    screen.fill((100, 100, 100))
    all_sprites_list.update()
    all_sprites_list.draw(screen)

    pygame.display.flip()

pygame.quit()

This is square.py:

import pygame
WHITE = (255, 255, 255)
 
SS = 600
class Firkant(pygame.sprite.Sprite):
    
    def __init__(self, color, width, height):
        super().__init__()       
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.rect(self.image, color, (0, 0, width, height))        
        self.rect = self.image.get_rect()

This is circle.py

import pygame
WHITE = (255, 255, 255)
class Sirkel(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, color, (width, height), 200)
        self.rect = self.image.get_rect()

The 3rd parameter of pygame.draw.circle is the center of the circle. If the rectangle is ( width , height ) size, the center point is ( width // 2 , height // 2 ):

pygame.draw.circle(self.image, color, (width, height), 200)

radius = min(width, height) // 2
pygame.draw.circle(self.image, color, (width // 2, height // 2), radius)

You can als use the center attribute of the pygame.Rect object:

self.rect = self.image.get_rect()
pygame.draw.circle(self.image, color, self.rect.center, min(self.rect.center))

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