Introduction to Pie Game
Pie game is actually a game I created by myself. Users can start the game by pressing the'p'key after the program runs. The timing of the game depends on the number of pies drawn by the program. The results are as follows:
The game will use four different libraries - sys (which provides a collection of functions that interact with the interpreter), math (which is a collection of many function modules about mathematics), time (which provides a collection of functions for applications that measure time), and pygame (which is a collection of python modules for writing games).
Catalog
1. Download of Pygame (downloaded to skip)
2. Import and initial setup of Pygame, sys, math, time
1. Download of Pygame (downloaded to skip)
Windows logo key + R, will pop up a run box, enter'cmd', return, enter'pip install pygame'
If you want to check if pyGame is installed, press the Windows logo key + R, enter'cmd'in the run box, enter'python', enter'import pygame' after returning, and the pop-up version number is installed.
2. Import and initial setup of Pygame, sys, math, time
####check#### import pygame,sys,math,time pygame.init()#Initialization sn = pygame.display.set_mode((600,500))#Define window size mt = pygame.font.Font(None,60)#(Font, Size) pygame.display.set_caption('Pie game')
To prevent program errors, I imported using import (errors will occur in a later cycle using from /. import /. see below). Then use display.set_mode((,)) to customize the size of the window, and font.Font(,) customizes the font and its size so that it can be called directly later, and finally displays are used. Set_ Caption ('') to set the top left caption.
3. Settings for while loops
while 1: for event in pygame.event.get():#Repeat traversal of user input key values if event.type in (pygame.QUIT,pygame.KEYDOWN):#Detects whether the user clicks the close in the upper right corner of the window or presses any key sys.exit()#End Program Run pygame.display.update()#Used to refresh the screen for display
The string that follows if needs to be changed later (because the game requires a keyboard) exit() is a function to end the program and add display at the end. Update() to refresh the display, the basic structure of the entire cycle of entry, operation, exit is out
ps: If you use from /. import /. when importing, the result is as follows:
We can see that the program can enter the cycle phase, but it will make errors when it runs (at which point it is not easy to be lazy). Change the program back to:
We can see that the program is working properly. (ps: missed in person)
4. Draw a circle
pygame.draw.circle(sn,'black',(300,300),100,10)
The color, position (note: pygame's (0, 0) is in the upper left corner), radius and width of the circle are set here, and sn is set outside this program so that it can be displayed in an open window.
5. Draw segments
pygame.draw.line(sn,'black',(100,100),(400,450),8)
The effect of sn is the same as that of circle drawing, followed by color, initial position, end position, and line width.
Well, we know that, and then there's a tap on the keyboard
6. Consolidation
Let users choose difficulty first
print('Please choose the difficulty level') print('a For difficulties, b Medium, c For simplicity(Just click on the keyboard key)')
Initialization Definition
import pygame,sys,math,time pygame.init() sn = pygame.display.set_mode((600,500)) mf = pygame.font.Font(None,60) pygame.display.set_caption('Pie game') a = b = c = d = e = f = g = h = a1 = b1 = c1 = d1 = e1 = f1 = a2 = b2 = c2 = d2 = 0 z = 1
We create a while and for loop and its exit statement (press the e key to exit later)
print('Please choose the difficulty level') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') while 1: for event in pygame.event.get():#Be sure to add this loop statement, otherwise the next one will error (called first within the for loop) if event.type == pygame.QUIT: sys.exit()
Then there is the difficulty of judging the user's choice, plus a judgment to judge the user's exit.
if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: #(Statement execution is detailed below) elif event.key == pygame.K_b: #(Statement execution is detailed below) elif event.key == pygame.K_c: #(Statement execution is detailed below) elif event.key == pygame.K_e: print('You have chosen to quit the game') time.sleep(0.7) sys.exit()
Let's start the process of a difficult level. Our eight characters a~h are 0, which are the eight keys in a difficult level. First we need to draw a circle, then divide the circle into eight parts with segments, one letter (or number), and the user presses the keys in any order. The letter (or number) that is pressed will disappear (that is, it will work if you write it again in the same place with the background color) (program in a)
#Drawing in Difficult Mode sn.fill('blue') #Draw a circle pygame.draw.circle(sn,'black',(300,250),200,10) #Draw a segment pygame.draw.line(sn,'black',(165,110),(420,400),14)#Segments can basically be copied and pasted pygame.draw.line(sn,'black',(300,50),(300,450),10) pygame.draw.line(sn,'black',(435,110),(180,400),14) pygame.draw.line(sn,'black',(102,265),(498,265),10) #Add Running Text tm = mf.render('h',True,'red')#Also copy and paste a wave sn.blit(tm,(170,200)) tm = mf.render('n',True,'red') sn.blit(tm,(250,125)) tm = mf.render('8',True,'red') sn.blit(tm,(170,300)) tm = mf.render('1',True,'red') sn.blit(tm,(250,365)) tm = mf.render('l',True,'red') sn.blit(tm,(350,125)) tm = mf.render('t',True,'red') sn.blit(tm,(350,350)) tm = mf.render('z',True,'red') sn.blit(tm,(430,200)) tm = mf.render('4',True,'red') sn.blit(tm,(430,300)) pygame.display.update() z = 2
Next, determine if the user presses the p key to start the game (when a executes, z=2 can execute the following statements, all of which are the same level as if event.key == pygame.K_a:
if z == 2: if event.key == pygame.K_p: z = 2.5 print('Press p Key Start')
Make z 3 followed by the following loops (someone might ask me if it's so cumbersome to judge that it doesn't make sense to just doll-like sleeves one by one). Pop, that's not a good idea (you've been wrong to demonstrate it yourself) and it will prevent the program from proceeding to the next step). Here's one of the more important functions--perf_counter(), this function starts by calling it with time, and ends by calling it again to know the "start time" and "end time" (start time is not zero, so subtract). The last line must zero this number, or else it will b e executed the next time it executes because it satisfies a direct entry here (if a == b == c == d == e == f == g == h == 1:
if z == 2.5: st = time.perf_counter() z = 3 if z == 3: if event.key == pygame.K_h: a = 1 tm = mf.render('h',True,'blue') sn.blit(tm,(170,200)) if event.key == pygame.K_n: b = 1 tm = mf.render('n',True,'blue') sn.blit(tm,(250,125)) if event.key == pygame.K_8: c = 1 tm = mf.render('8',True,'blue') sn.blit(tm,(170,300)) if event.key == pygame.K_1: d = 1 tm = mf.render('1',True,'blue') sn.blit(tm,(250,365)) if event.key == pygame.K_l: e = 1 tm = mf.render('l',True,'blue') sn.blit(tm,(350,125)) if event.key == pygame.K_t: f = 1 tm = mf.render('t',True,'blue') sn.blit(tm,(350,350)) if event.key == pygame.K_z: g = 1 tm = mf.render('z',True,'blue') sn.blit(tm,(430,200)) if event.key == pygame.K_4: h = 1 tm = mf.render('4',True,'blue') sn.blit(tm,(430,300)) pygame.display.update() if a == b == c == d == e == f == g == h == 1: ed = time.perf_counter() if ed - st > 5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a = b = c = d = e = f = g = h = 0
Moderately difficult programs are actually simpler, most of them are copies, some modifications are enough, just like part a, with fewer letters
sn.fill('blue') pygame.draw.circle(sn,'black',(300,250),200,10) pygame.draw.line(sn,'black',(102,265),(498,265),10) pygame.draw.line(sn,'black',(165,110),(420,400),14) pygame.draw.line(sn,'black',(435,110),(180,400),14) tm = mf.render('q',True,'red') sn.blit(tm,(170,200)) tm = mf.render('5',True,'red') sn.blit(tm,(290,125)) tm = mf.render('9',True,'red') sn.blit(tm,(170,300)) tm = mf.render('m',True,'red') sn.blit(tm,(290,365)) tm = mf.render('g',True,'red') sn.blit(tm,(430,200)) tm = mf.render('i',True,'red') sn.blit(tm,(430,300)) pygame.display.update() z = 4
And its execution statements
if z == 4: if event.key == pygame.K_p: z = 4.5 print('Press p Key Start') if z == 4.5: st1 = time.perf_counter() z = 5 if z == 5: if event.key == pygame.K_q: a1 = 1 tm = mf.render('q',True,'blue') sn.blit(tm,(170,200)) if event.key == pygame.K_5: b1 = 1 tm = mf.render('5',True,'blue') sn.blit(tm,(290,125)) if event.key == pygame.K_9: c1 = 1 tm = mf.render('9',True,'blue') sn.blit(tm,(170,300)) if event.key == pygame.K_m: d1 = 1 tm = mf.render('m',True,'blue') sn.blit(tm,(290,365)) if event.key == pygame.K_g: e1 = 1 tm = mf.render('g',True,'blue') sn.blit(tm,(430,200)) if event.key == pygame.K_i: f1 = 1 tm = mf.render('i',True,'blue') sn.blit(tm,(430,300)) pygame.display.update() if a1 == b1 == c1 == d1 == e1 == f1 == 1: ed1 = time.perf_counter() if ed1 - st1 > 4.5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a1 = b1 = c1 = d1 = e1 = f1 = 0
There's nothing to do with the last simple part. Just code it!
sn.fill('blue') pygame.draw.circle(sn,'black',(300,250),200,10) pygame.draw.line(sn,'black',(300,50),(300,450),10) pygame.draw.line(sn,'black',(102,265),(498,265),10) tm = mf.render('d',True,'red') sn.blit(tm,(200,150)) tm = mf.render('h',True,'red') sn.blit(tm,(390,150)) tm = mf.render('0',True,'red') sn.blit(tm,(200,350)) tm = mf.render('3',True,'red') sn.blit(tm,(390,350)) pygame.display.update() z = 6
if z == 6: if event.key == pygame.K_p: z = 6.5 print('Press p Key Start') if z == 6.5: st2 = time.perf_counter() z = 7 if z == 7: if event.key == pygame.K_d: a2 = 1 tm = mf.render('d',True,'blue') sn.blit(tm,(200,150)) if event.key == pygame.K_h: b2 = 1 tm = mf.render('h',True,'blue') sn.blit(tm,(390,150)) if event.key == pygame.K_0: c2 = 1 tm = mf.render('0',True,'blue') sn.blit(tm,(200,350)) if event.key == pygame.K_3: d2 = 1 tm = mf.render('3',True,'blue') sn.blit(tm,(390,350)) pygame.display.update() if a2 == b2 == c2 == d2 == 1: ed2 = time.perf_counter() if ed2 - st2 > 2.5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a2 = b2 = c2 = d2 = 0
7. Source Code
Finally, here's the source code:
import pygame,sys,math,time pygame.init() sn = pygame.display.set_mode((600,500)) mf = pygame.font.Font(None,50) pygame.display.set_caption('Pie game') a = b = c = d = e = f = g = h = a1 = b1 = c1 = d1 = e1 = f1 = a2 = b2 = c2 = d2 = 0 print('Please choose the difficulty level') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') print('Press e Sign out') z = 1 while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: sn.fill('blue') pygame.draw.circle(sn,'black',(300,250),200,10) pygame.draw.line(sn,'black',(165,110),(420,400),14) pygame.draw.line(sn,'black',(300,50),(300,450),10) pygame.draw.line(sn,'black',(435,110),(180,400),14) pygame.draw.line(sn,'black',(102,265),(498,265),10) tm = mf.render('h',True,'red') sn.blit(tm,(170,200)) tm = mf.render('n',True,'red') sn.blit(tm,(250,125)) tm = mf.render('8',True,'red') sn.blit(tm,(170,300)) tm = mf.render('1',True,'red') sn.blit(tm,(250,365)) tm = mf.render('l',True,'red') sn.blit(tm,(350,125)) tm = mf.render('t',True,'red') sn.blit(tm,(350,350)) tm = mf.render('z',True,'red') sn.blit(tm,(430,200)) tm = mf.render('4',True,'red') sn.blit(tm,(430,300)) pygame.display.update() z = 2 elif event.key == pygame.K_b: sn.fill('blue') pygame.draw.circle(sn,'black',(300,250),200,10) pygame.draw.line(sn,'black',(102,265),(498,265),10) pygame.draw.line(sn,'black',(165,110),(420,400),14) pygame.draw.line(sn,'black',(435,110),(180,400),14) tm = mf.render('q',True,'red') sn.blit(tm,(170,200)) tm = mf.render('5',True,'red') sn.blit(tm,(290,125)) tm = mf.render('9',True,'red') sn.blit(tm,(170,300)) tm = mf.render('m',True,'red') sn.blit(tm,(290,365)) tm = mf.render('g',True,'red') sn.blit(tm,(430,200)) tm = mf.render('i',True,'red') sn.blit(tm,(430,300)) pygame.display.update() z = 4 elif event.key == pygame.K_c: sn.fill('blue') pygame.draw.circle(sn,'black',(300,250),200,10) pygame.draw.line(sn,'black',(300,50),(300,450),10) pygame.draw.line(sn,'black',(102,265),(498,265),10) tm = mf.render('d',True,'red') sn.blit(tm,(200,150)) tm = mf.render('h',True,'red') sn.blit(tm,(390,150)) tm = mf.render('0',True,'red') sn.blit(tm,(200,350)) tm = mf.render('3',True,'red') sn.blit(tm,(390,350)) pygame.display.update() z = 6 elif event.key == pygame.K_e: print('You have chosen to quit the game') time.sleep(0.7) sys.exit() if z == 2: if event.key == pygame.K_p: z = 2.5 print('Press p Key Start') if z == 2.5: st = time.perf_counter() z = 3 if z == 3: if event.key == pygame.K_h: a = 1 tm = mf.render('h',True,'blue') sn.blit(tm,(170,200)) if event.key == pygame.K_n: b = 1 tm = mf.render('n',True,'blue') sn.blit(tm,(250,125)) if event.key == pygame.K_8: c = 1 tm = mf.render('8',True,'blue') sn.blit(tm,(170,300)) if event.key == pygame.K_1: d = 1 tm = mf.render('1',True,'blue') sn.blit(tm,(250,365)) if event.key == pygame.K_l: e = 1 tm = mf.render('l',True,'blue') sn.blit(tm,(350,125)) if event.key == pygame.K_t: f = 1 tm = mf.render('t',True,'blue') sn.blit(tm,(350,350)) if event.key == pygame.K_z: g = 1 tm = mf.render('z',True,'blue') sn.blit(tm,(430,200)) if event.key == pygame.K_4: h = 1 tm = mf.render('4',True,'blue') sn.blit(tm,(430,300)) pygame.display.update() if a == b == c == d == e == f == g == h == 1: ed = time.perf_counter() if ed - st > 6.5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a = b = c = d = e = f = g = h = 0 if z == 4: if event.key == pygame.K_p: z = 4.5 print('Press p Key Start') if z == 4.5: st1 = time.perf_counter() z = 5 if z == 5: if event.key == pygame.K_q: a1 = 1 tm = mf.render('q',True,'blue') sn.blit(tm,(170,200)) if event.key == pygame.K_5: b1 = 1 tm = mf.render('5',True,'blue') sn.blit(tm,(290,125)) if event.key == pygame.K_9: c1 = 1 tm = mf.render('9',True,'blue') sn.blit(tm,(170,300)) if event.key == pygame.K_m: d1 = 1 tm = mf.render('m',True,'blue') sn.blit(tm,(290,365)) if event.key == pygame.K_g: e1 = 1 tm = mf.render('g',True,'blue') sn.blit(tm,(430,200)) if event.key == pygame.K_i: f1 = 1 tm = mf.render('i',True,'blue') sn.blit(tm,(430,300)) pygame.display.update() if a1 == b1 == c1 == d1 == e1 == f1 == 1: ed1 = time.perf_counter() if ed1 - st1 > 4.5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a1 = b1 = c1 = d1 = e1 = f1 = 0 if z == 6: if event.key == pygame.K_p: z = 6.5 print('Press p Key Start') if z == 6.5: st2 = time.perf_counter() z = 7 if z == 7: if event.key == pygame.K_d: a2 = 1 tm = mf.render('d',True,'blue') sn.blit(tm,(200,150)) if event.key == pygame.K_h: b2 = 1 tm = mf.render('h',True,'blue') sn.blit(tm,(390,150)) if event.key == pygame.K_0: c2 = 1 tm = mf.render('0',True,'blue') sn.blit(tm,(200,350)) if event.key == pygame.K_3: d2 = 1 tm = mf.render('3',True,'blue') sn.blit(tm,(390,350)) pygame.display.update() if a2 == b2 == c2 == d2 == 1: ed2 = time.perf_counter() if ed2 - st2 > 2.5: print('Challenge Failure') sys.exit() else: print('Challenge Success') print('a For difficulties, b Medium, c For simplicity(Click this key on the keyboard)') a2 = b2 = c2 = d2 = 0
ps: There is a mistake, please point out, and please be more inclusive