0

I am trying to use the pygame.draw.arc() as a sprite, but its not displaying on the screen. I can achieve the same effect by writing same code in the main loop, but once I try to create a sprite, the effect is not being displayed.

(One part in the main-loop can be un-commented to see the desired effect as well.)

Any pointers could be of great help.

import pygame
import random
import math


BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
RED      = ( 255,   0,   0)

SCREEN_WIDTH = 513
SCREEN_HEIGHT = 513
class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)

        self.color = color
        self.image = pygame.Surface([width, height])

        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        self.center_x = SCREEN_WIDTH/2-15
        self.center_y = SCREEN_HEIGHT/2-15
        # Draw the ellipse, THIS WORKS PERFECTLY
        #pygame.draw.ellipse(self.image, color, [0, 0, width, height])

        self.i=0
        #THIS DOESN'T WORK FOR SOME REASON
        pygame.draw.arc(self.image, (0,255,255),(25,25,450,450),0+(self.i*math.pi)/180,math.pi/6 +(self.i*math.pi)/180,10)
        self.rect = self.image.get_rect()
        self.angle = 0
        self.radius = 210
        self.speed = 0.05

    def update(self):
        self.i += self.speed


pygame.init() 
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

all_sprites_list = pygame.sprite.Group()

block = Block(BLACK, 20, 15) 
all_sprites_list.add(block) 

done = False

clock = pygame.time.Clock()
i=0
while not done:
    for event in pygame.event.get():  
        if event.type == pygame.QUIT: # 
            done = True  
    screen.fill(WHITE)
    all_sprites_list.update()

    all_sprites_list.draw(screen)

    #UNCOMMENT TO SEE THE DESIRED EFFECT
    #i= i+1
    #pygame.draw.arc(screen, (0,255,255),(25,25,450,450),0+(i*math.pi)/180,math.pi/6 +(i*math.pi)/180,10)
    pygame.display.flip() 
    clock.tick(60)

pygame.quit()
ansrivas
  • 597
  • 2
  • 9
  • 14

1 Answers1

0

You draw the arc outside of the surface.

Here you pass the values 20 and 15 to Block:

block = Block(BLACK, 20, 15) 

So you create a Surface with the size 20, 15:

self.image = pygame.Surface([width, height])

Then you draw the arc in the area 25,25,450,450:

pygame.draw.arc(self.image, (0,255,255),(25,25,450,450),0+(self.i*math.pi)/180,math.pi/6 +(self.i*math.pi)/180,10)

But that area is outside the surface, because it is only 20 pixels wide and 15 pixels high.

The area you passed to pygame.draw.arc is relative to self.image.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • One more thing, is it fine to write something like this in update() ? or it should be avoided ? `def update(self):` `self.i = self.i + 1` `self.image.fill(WHITE)` `self.image.set_colorkey(WHITE)` `pygame.draw.arc(self.image, (0,255,255),(0,0,450,450),0+(self.i*math.pi)/180,math.pi/6 +(self.i*math.pi)/180,10)` – ansrivas Feb 19 '15 at 12:55
  • @Sokio I think that's not a problem. If you have multiple `Block` objects and you observe a serious performance loss, it might be useful to cache the surfaces, but that's another topic. – sloth Feb 19 '15 at 13:04