Reputation: 21
I am coding a game called the pie game from a book called 'More Python Programming for the Absolute Beginner' but the shapes don't render and a black screen appears.
I want it to display 4 numbers and when you press the keys 1, 2, 3 and 4 on the keyboard, slices of pie/pizza/circular bread/whatever appear (1 arc and 2 lines).
I can't see a problem with the code. Maybe someone has written the same article before, but our code is different.
# -*- coding = utf-8 -*-
# @File : piegame.py
# @Software:PyCharm
import math
import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None,60)
color = 200,80,60
width = 4
x = 300
y = 250
radius = 200
position = x-radius,y-radius,radius*2,radius*2
piece1 = False
piece2 = False
piece3 = False
piece4 = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
piece1 = True
elif event.key == pygame.K_2:
piece2 = True
elif event.key == pygame.K_3:
piece3 = True
elif event.key == pygame.K_4:
piece4 = True
# clear the screen
screen.fill((0,0,200))
# draw the four numbers
testImg1 = myfont.render("1",True,color)
screen.blit(testImg1,(x+radius/2-20,y-radius/2))
testImg2 = myfont.render("2",True,color)
screen.blit(testImg2,(x-radius/2,y-radius/2))
testImg3 = myfont.render("3",True,color)
screen.blit(testImg3,(x-radius/2,y+radius/2-20))
testImg4 = myfont.render("4",True,color)
screen.blit(testImg4,(x+radius/2-20,y+radius/2-20))
#should the pieces be drawn?
if piece1:
start_angle=math.radians(0)
end_angle =math.radians(90)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece2:
start_angle=math.radians(90)
end_angle =math.radians(180)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece3:
start_angle=math.radians(180)
end_angle =math.radians(270)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece4:
start_angle = math.radians(270)
end_angle = math.radians(360)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x, y - radius), width)
pygame.draw.line(screen, color, (x, y), (x + radius, y), width)
#is the pie finished?
if piece1 and piece2 and piece3 and piece4:
color = 0,255,0
pygame.display.update()
Thank you for your comments. The error location is:line 36
elif event.key == pygame.K_1:
piece1 = True
elif event.key == pygame.K_2:
piece2 = True
elif event.key == pygame.K_3:
piece3 = True
elif event.key == pygame.K_4:
piece4 = True
Upvotes: 2
Views: 32
Reputation: 4836
Your code needed a couple of tweaks to get it to work. Basically, it came down to proper indentation. Following is your program with the proper indentation.
import math
import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None,60)
color = 200,80,60
width = 4
x = 300
y = 250
radius = 200
position = x-radius,y-radius,radius*2,radius*2
piece1 = False
piece2 = False
piece3 = False
piece4 = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1: # Revised the if else set here for the key events
piece1 = True
elif event.key == pygame.K_2:
piece2 = True
elif event.key == pygame.K_3:
piece3 = True
elif event.key == pygame.K_4:
piece4 = True
# clear the screen - ALSO - this code needed to be indented in order for it to be run
screen.fill((0,0,200))
# draw the four numbers
testImg1 = myfont.render("1",True,color)
screen.blit(testImg1,(x+radius/2-20,y-radius/2))
testImg2 = myfont.render("2",True,color)
screen.blit(testImg2,(x-radius/2,y-radius/2))
testImg3 = myfont.render("3",True,color)
screen.blit(testImg3,(x-radius/2,y+radius/2-20))
testImg4 = myfont.render("4",True,color)
screen.blit(testImg4,(x+radius/2-20,y+radius/2-20))
#should the pieces be drawn?
if piece1:
start_angle=math.radians(0)
end_angle =math.radians(90)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece2:
start_angle=math.radians(90)
end_angle =math.radians(180)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece3:
start_angle=math.radians(180)
end_angle =math.radians(270)
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
if piece4:
start_angle = math.radians(270)
end_angle = math.radians(360)
pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
pygame.draw.line(screen, color, (x, y), (x, y - radius), width)
pygame.draw.line(screen, color, (x, y), (x + radius, y), width)
#is the pie finished?
if piece1 and piece2 and piece3 and piece4:
color = 0,255,0
pygame.display.update()
First, the "elif" block of code for your key events needed to be indented so that they belonged to the "KEYUP" block. And then, all of the code below the event block also needed to be indented so as to be a part of the "while" loop. In your code above, the only activity that was continually being run were the events. Your program never continued down to the testing and displaying.
With those tweaks, I was able to display your pie game.
I hope that clarifies things.
Regards.
Upvotes: 1