### Thread: Need Help On Drawing Arcs with Pygame

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2

#### Need Help On Drawing Arcs with Pygame

Hello,

So I'm following the Program Arcade Games with Python course, and Chapter 5's lab requires me to draw a pretty picture using computer graphics onto the screen.

I decided that doing a screen of Galaga when all enemies are visible on screen would be a cool idea.

So far, I have the ship done, but I'm working on the bee as you'll see in the upper left corner.

I'm having trouble drawing an arc for his upper arm.

I first tried drawing an arc with an angle of 3pi/2 to pi, but it made a huge circle with my intended start and end coordinates.

Ideally, I want this arc to begin on the end coordinate of the second line of the left eye since its pretty proportionate at that location to put a little arm (aka [70, 99)

Here's my code so far, its super commented for my own purposes near the arc portion so please don't criticize thank you:
Code:
```#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Owner
#
# Created:     14/01/2014
# Copyright:   (c) Owner 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------

# Lab 5:
# Draw a pretty picture using for loops and computer graphics

# Goals:
# Use multiple colors

# Make a coherent picture

# Use multiple types of graphic functions (circles, rectangles, lines, etc)

# Use a while or for loop to create a repeating pattern.

# Do not just redraw the same thing in the same location 10 times.

# Use that index variable as an offset to display what you are drawing.

# Remember that you can contain multiple drawing commands in a loop,
# so you can draw multiple train cars for ex

# Galaga Drawing Lab:

# import pygame:
import pygame

# Define colors:
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 221, 0)

# Define pi for drawing arcs:
PI = 3.141592653

# Define the framerate
frameRate = 60

# Initialize the pygame library:
pygame.init()

# Set the screen size and create the screen:
size = (600, 400)
screen = pygame.display.set_mode(size)

# Set the window caption:
pygame.display.set_caption("Chapter 5 Lab: Galaga Drawing Lab")

# Set the default loop boolean:
done = False

# Set the game clock:
clock = pygame.time.Clock()

# Make the program loop:
while not done:

for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

# Game Logic:

# Drawing Code:
screen.fill(BLACK)

# Draw a circle for testing purposes:
pygame.draw.circle(screen, RED, [300, 200], 5)

# Draw the polygon for the ship:
# Left, center, right
pygame.draw.polygon(screen, WHITE, [[295, 385],[300, 370], [305, 385]], 0)

# Draw the ship's left red arrow design in the center:
pygame.draw.line(screen, RED, [300, 380], [299, 382], 1)

# Draw the ship's right red arrow design in the center:
pygame.draw.line(screen, RED, [300, 380], [301, 382], 1)

# Draw a line for the ship's left wing:
pygame.draw.line(screen, WHITE, [295, 385], [290, 387], 3)

# Draw remaining left tip of left wing:
pygame.draw.line(screen, WHITE, [290, 382], [290, 385], 2)

# Draw Left Laser:
pygame.draw.line(screen, RED, [290, 380], [290, 382], 2)

# Draw a line for the shio's right wing:
pygame.draw.line(screen, WHITE, [305, 385], [310, 387], 3)

# Draw remaining right tip of right wing:
pygame.draw.line(screen, WHITE, [310, 382], [310, 388], 2)

# Draw Right Laser:
pygame.draw.line(screen, RED, [310, 380], [310, 382], 2)

# ROW OF BEES (WITH X and possibly Y displacement)
# Begin at [75, 100] with a bee's head:
pygame.draw.circle(screen, YELLOW, [75,100], 5)

# Draw first line for its left eye:
pygame.draw.line(screen, RED, [73, 95], [73, 99], 1)

# Draw second line for its left eye:
pygame.draw.line(screen, RED, [73, 99], [70 ,99], 1)

# Draw first line for its right eye:
pygame.draw.line(screen, RED, [76, 95], [76, 99], 1)

# Draw second line for its right eye:
pygame.draw.line(screen, RED, [76, 99], [79, 99], 1)

# Draw a rectangle for its body:
pygame.draw.rect(screen, YELLOW, [72, 102, 6, 8], 0)

# Draw first pinstripe for body:
pygame.draw.rect(screen, RED, [72, 104, 6, 2], 0)

# Draw second pinstripe for body:
pygame.draw.rect(screen, RED, [72, 108, 6, 2], 0)

# Draw the tip of the bee's body:
pygame.draw.rect(screen, RED, [73, 109, 4, 4], 0)

# The 9 Parameters For Drawing An Arc:
# 1. Surface
# 2. Color
# 3. Start X-Coord
# 4. Start Y-Coord
# 5. End X-Coord
# 6. End Y-Coord
# 7. Beginning Radian Angle
# 8. Ending Radian Angle
# 9. Line Width

# Draw second line for its left eye:
# pygame.draw.line(screen, RED, [73, 99], [70 ,99], 1)

# Draw bee's upper left arm using an arc: 3pi /2 to pi
pygame.draw.arc(screen, WHITE, [66, 58, 66, 58], PI, 7*PI/6, 1)

# Circle for bees

# Update the screen:
pygame.display.flip()

# Update the clock based on the frame rate
clock.tick(frameRate)

pygame.quit()

# Chapter 5 Lab Link:
# http://programarcadegames.com/index.php?chapter=lab_create_a_picture```

Either way, if you guys have a better idea of how to draw his legs, that would be awesome! I can't use sprite art yet due to the limitations of the assignment only specifying in-engine drawing techniques for graphics.

Here's the picture I'm trying to base this screen off:

http://www.gracecentered.com/wp-content/uploads/2012/10/galaga.jpg

Here's the chapter I'm referring to:

http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#section_5

Here's the lab link just in case:

http://programarcadegames.com/index.php?chapter=lab_create_a_picture
Last edited by HauntJemimah; January 15th, 2014 at 11:55 AM.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
194
Rep Power
3
Edit: Realized that the pygame.draw module does indeed have an arc function so no need to use gfxdraw.

I want to point out that the 3rd argument to pygame.draw.arc is not of the form (start_x, start_y, end_x, end_y). It is actually a rect (x,y,width,height).

The below example illustrates by filling the passed rect a different color. First however I would like to strongly suggest you don't waste too much time on this. Sure you could draw the screen that way, but no one ever would. Once you have learned how to use the draw functions, it is time to move on.

Example:
python Code:
```import sys
import math
import pygame as pg

class Control(object):
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((500,500))
self.screen_rect = self.screen.get_rect()
self.clock = pg.time.Clock()
self.fps = 60.0
self.done = False
self.arc_rect = pg.Rect(0,0,100,100)
self.arc_rect.center = self.screen_rect.center

def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True

def main_loop(self):
while not self.done:
self.event_loop()
self.screen.fill(pg.Color("white"),self.arc_rect)
self.screen.set_at(self.arc_rect.center,pg.Color("black"))
pg.draw.arc(self.screen, pg.Color("red"), self.arc_rect, 0, math.pi)
pg.display.update()
self.clock.tick(self.fps)

if __name__ == "__main__":
app = Control()
app.main_loop()
pg.quit()
sys.exit()```

-Mek
Last edited by Mekire; January 15th, 2014 at 10:57 PM.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2
Oh I gotcha, I was going to just do another enemy, and that's it really.

I would just add x displacements to generate multiple copies within in a for loop for each of the Pygame drawn sprites.

I understand though, later on, its only going to be a matter of using sprite strips, but I figured I would try my best now for fun.