pygame simulate

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pygame,sys,random,time
from pygame.locals import*
 
FPS = 30
 
green =(0,255,0)
darkgreen =(0,155,0)
red =(255,0,0)
darkred =(155,0,0)
blue =(0,0,255)
darkblue =(0,0,155)
yellow =(255,255,0)
darkyellow =(155,155,0)
 
gray = (0,255,255)
purple = (255,0,255)
white = (255,255,255)
black = (0,0,0)
bgcolor = black
flashdelay = 200
 
winx = 640
winy = 480
boardsize = 200
boardgap = 20
 
xmargin = int(winx-(boardsize*2+boardgap))/2
ymargin = int(winy-(boardsize*2+boardgap))/2
 
greenrect = pygame.Rect(xmargin,ymargin,boardsize,boardsize)
redrect = pygame.Rect(xmargin+boardsize+boardgap,ymargin,boardsize,boardsize)
bluerect = pygame.Rect(xmargin,ymargin+boardsize+boardgap,boardsize,boardsize)
yellowrect = pygame.Rect(xmargin+boardsize+boardgap,ymargin+boardsize+boardgap,boardsize,boardsize)
 
def main():
    global fpsclock,disp,beep1,beep2,beep3,beep4
 
    pygame.init()
    disp = pygame.display.set_mode((winx,winy))
    pygame.display.set_caption('simulate')
    fpsclock = pygame.time.Clock()
     
 
    basicfont = pygame.font.Font('freesansbold.ttf',16)
    infosurf = basicfont.render('match the pattern',True,black)
    inforect = infosurf.get_rect()
    inforect.topleft = (10,winy - 25)
 
    beep1 = pygame.mixer.Sound('beep1.ogg')
    beep2 = pygame.mixer.Sound('beep2.ogg')
    beep3 = pygame.mixer.Sound('beep3.ogg')
    beep4 = pygame.mixer.Sound('beep4.ogg')
     
    pattern = []
    currentstep = 0
    lasttime = 0
    score = 0
    waitingforinput = False
 
    while True:
 
        clickedbutton = None
        disp.fill(bgcolor)
        drawbuttons()
 
        scoresurf = basicfont.render('score:%s'%str(score),1,white)
        scorerect = scoresurf.get_rect()
        scorerect.topleft = (winx-100,10)
        disp.blit(scoresurf,scorerect)
        disp.blit(infosurf,inforect)
 
        checkforquit()
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONUP:
                mousex,mousey = event.pos
                clickedbutton = getclicked(mousex,mousey)
            elif event.type == KEYDOWN:
                if event.key == K_q:
                    clickedbutton = darkgreen
                elif event.key == K_w:
                    clickedbutton = darkred
                elif event.key ==K_a:
                    clickedbutton == bluerect
                elif event.key == K_s:
                    clickedbutton == yellowrect
 
        if not waitingforinput:
            pygame.display.update()
            pygame.time.wait(1000)                       
            pattern.append(random.choice((darkgreen,darkred,darkblue,darkyellow)))
            for button in pattern:
                flashbutton(button)
                pygame.time.wait(flashdelay)
            waitingforinput = True
        else:
            if clickedbutton and clickedbutton == pattern[currentstep]:
                flashbutton(clickedbutton)
                currentstep+= 1
                lasttime = time.time()
                 
                if currentstep == len(pattern):
                    changebackgroundanimation()
                    score+= 1
                    waitingforinput = False
                    currentstep = 0
            elif (clickedbutton and clickedbutton!= pattern[currentstep]):
                gameoveranimation()
                pattern = []
                currentstep = 0
                waitingforinput = False
                score = 0
                pygame.time.wait(1000)
                changebackgroundanimation()
        pygame.display.update()
        fpsclock.tick(FPS)
             
def terminal():
    pygame.quit()
    sys.exit()
 
def checkforquit():
    for event in pygame.event.get(QUIT):
        terminal()
    for event in pygame.event.get(KEYUP):
        if event.key == K_ESCAPE:
            terminal()
        pygame.event.post(event)
         
def drawbuttons():
    pygame.draw.rect(disp,darkgreen,greenrect)
    pygame.draw.rect(disp,darkred,redrect)
    pygame.draw.rect(disp,darkblue,bluerect)
    pygame.draw.rect(disp,darkyellow,yellowrect)
     
def flashbutton(color ,animationspeed = 20):
    if color == darkgreen:
        flashcolor = green
        rectangle = greenrect
        beep = beep1
    elif color == darkred:
        flashcolor = red
        rectangle = redrect
        beep = beep2
    elif color == darkblue:
        flashcolor = blue
        rectangle = bluerect
        beep = beep3
    elif color == darkyellow:
        flashcolor = yellow
        rectangle = yellowrect
        beep = beep4
         
         
    osurf = disp.copy()
    flashsurf = pygame.Surface((boardsize,boardsize))
    flashsurf = flashsurf.convert_alpha()
    r,g,b = flashcolor
    beep.play()
    for start,end,step in ((0,255,1),(255,0,-1)):
        for alpha in range(start,end,animationspeed*step):
            checkforquit()
            disp.blit(osurf,(0,0))
            flashsurf.fill((r,g,b,alpha))
            disp.blit(flashsurf,rectangle.topleft)
            pygame.display.update()
            fpsclock.tick(FPS)
             
    disp.blit(osurf,(0,0))
             
 
def getclicked(x,y):
    if greenrect.collidepoint(x,y):
        return darkgreen
    elif redrect.collidepoint(x,y):
        return darkred
    elif bluerect.collidepoint(x,y):
        return darkblue
    elif yellowrect.collidepoint(x,y):
        return darkyellow
    return None
 
def changebackgroundanimation(animationspeed = 40):
    global bgcolor
    newcolor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
 
    newsurf = pygame.Surface((winx,winy))
    newsurf = newsurf.convert_alpha()
    r,g,b = newcolor
    for alpha in range(0,255,animationspeed):
        disp.fill(bgcolor)
        newsurf.fill((r,g,b,alpha))
        disp.blit(newsurf,(0,0))
 
        drawbuttons()
        pygame.display.update()
        fpsclock.tick(FPS)
    bgcolor = newcolor
 
def gameoveranimation(color = white,animationspeed = 50):
    osurf = disp.copy()
    flashsurf = pygame.Surface(disp.get_size())
    flashsurf = flashsurf.convert_alpha()
    beep1.play()
    beep2.play()
    beep3.play()
    r,g,b = color
    for i in range(3):
        for start,end,step in ((0,255,1),(255,0,-1)):
            for alpha in range(start,end,animationspeed*step):
                checkforquit()
                flashsurf.fill((r,g,b,alpha))
                disp.blit(osurf,(0,0))
                disp.blit(flashsurf,(0,0))
                drawbuttons()
                pygame.display.update()
                fpsclock.tick(FPS)
         
         
 
 
 
if __name__=='__main__':
    main()