import simplegui, time, random, math
time = 0
count = 0
initial = [[387, 513],[600, 600],[813, 513],[300, 300],[900, 300],[387, 87],
[600, 0],[813, 87]]
这是由具有所有节拍的song1函数调用的节拍类。
class Beat(object):
def __init__(self, speed, creationtime, index):
self.index = index
self.speed = speed
self.XY = [[387, 513],[600, 600],[813, 513],[300, 300],[900, 300],
[387, 87],[600, 0],[813, 87]]
self.posXY = self.XY[self.index]
self.spawn = self.XY[self.index]
#print self.spawn
self.creationtime = creationtime - 453
self.radius = 10
self.line_width = 5
self.line_color = "Red"
self.fill_color = "Black"
self.keys = [[49,97],[50,98],[51,99],[52,100],[54,102],[55,103],
[56,104],[57,105]]
self.key=self.keys[self.index]
self.show=False
def moveBeat(self):
dx = 600-self.posXY[0]
dy = 300-self.posXY[1]
dist=math.hypot(dx,dy)
if(dist!=0):
dx/=dist
dy/=dist
self.posXY[0]+=dx * self.speed
self.posXY[1]+=dy * self.speed
return self.posXY
Keys类将按键推入到那里的数字,值和位置。
class Buttons(object):
def __init__(self):
self.center = [(575, 325),(600, 340),(625, 325),(560, 300),(640,
300),(575, 275),(600, 260),(625, 275)]
self.radius = 15
self.line_width = 5
self.line_color = "Silver"
self.fill_color = ["Black"]*8
self.key = [[49,97],[50,98],[51,99],[52,100],[54,102],[55,103],[56,104],[57,105]]
self.index = 0
def timer_handler():
global time
time += 1
print initial
设置所有内容的Gamerun类。
class gamerun():
global time
def __init__(self):
self.first = True
self.CanvasW = 1200
self.CanvasH = 600
self.sound = simplegui.load_sound('https://drive.google.com/uc? export=download&id=1EThueLDIluVn14TTgPUq_GSqyiKaO98g')
self.Buttons = Buttons()
self.song = self.song1()
self.frame = simplegui.create_frame("Iron Rythm", self.CanvasW, self.CanvasH)
self.timer = simplegui.create_timer(1, timer_handler)
self.frame.add_button('START', self.buttonSTARTGAME)
self.frame.add_button('RESET', self.buttonRESETGAME)
self.frame.add_label('Esc to pause game')
self.frame.set_keydown_handler(self.Keydown)
self.frame.set_keyup_handler(self.Keyup)
self.frame.set_draw_handler(self.draw)
self.frame.start()
所有值均已重置且游戏重新开始。
def buttonRESETGAME(self):
global time, initial
self.sound.rewind()
time = 0
count =0
self.timer.stop()
for i in range(len(self.song)):
self.song[i].posXY=initial[self.song[i].index]
print initial[self.song[i].index]
self.song[i].show=False
def buttonSTARTGAME(self):
self.sound.play()
self.timer.start()
由定时器产生歌曲的节拍,然后隐藏一个问题是,在第二次重置后节拍的位置发生了变化,我不明白为什么。
def song1(self):
self.beat1=Beat(4,1898,0)
self.beat2=Beat(4,1949,1)
self.beat3=Beat(4,2691,0)
self.beat4=Beat(4,2727,1)
self.beat5=Beat(4,2838,2)
return [self.beat1,self.beat2,self.beat3,self.beat4,self.beat5]
def Keydown(self, key):
if (key == 27):
global count
count +=1
if (count % 2) == 0:
self.timer.start()#Even
self.sound.play()
else:
self.timer.stop()#Odd
self.sound.pause()
for i in range(len(self.Buttons.key)):
if(key == self.Buttons.key[i][0] or key == self.Buttons.key[i] [1]):
self.Buttons.index = i
print self.Buttons.index
print time
for beat in self.song:
if(int(beat.posXY[0]) in range(560,590)):
if(int(beat.posXY[1]) in range(310,340)):
print beat.posXY
beat.show=False
self.Buttons.fill_color[self.Buttons.index] = 'Grey'
def Keyup(self,key):
for i in range(len(self.Buttons.key)):
if(key == self.Buttons.key[i][0] or key == self.Buttons.key[i] [1]):
self.Buttons.index = i
#print self.Buttons.index
self.Buttons.fill_color[self.Buttons.index] = 'Black'
def draw(self, canvas):
#print time
#Mid point
canvas.draw_circle((600, 300), 10, 5, "Red") #5
canvas.draw_circle((600, 300), 300, 1, "Green") #spawn circle outer
canvas.draw_circle((387, 513), 15, 1, "Green") #spawn circle1
canvas.draw_circle((600, 600), 15, 1, "Green") #spawn circle2
canvas.draw_circle((813, 513), 15, 1, "Green") #spawn circle3
canvas.draw_circle((300, 300), 15, 1, "Green") #spawn circle4
canvas.draw_circle((900, 300), 15, 1, "Green") #spawn circle6
canvas.draw_circle((387, 87), 15, 1, "Green") #spawn circle7
canvas.draw_circle((600, 0), 15, 1, "Green") #spawn circle8
canvas.draw_circle((813, 87), 15, 1, "Green") #spawn circle9
#Key Circles
for i in range(8):
canvas.draw_circle(self.Buttons.center[i], self.Buttons.radius,
self.Buttons.line_width, self.Buttons.line_color,
self.Buttons.fill_color[i])
if(i<=3):
canvas.draw_text(str(i+1),(self.Buttons.center[i][0]-2,self.Buttons.center[i][1]+5),14,"White")
else:
canvas.draw_text(str(i+2),(self.Buttons.center[i] [0]-2,self.Buttons.center[i][1]+5),14,"White")
#canvas.draw_circle(self.Beats.center[i], self.Beats.radius,self.Beats.line_width, self.Beats.line_color, self.Beats.fill_color[i])
#canvas.draw_circle(center, radius, line_width, line_color,fill_color)
#print time
for beat in self.song:
if(time>=beat.creationtime):
canvas.draw_circle(beat.moveBeat(),beat.radius,beat.line_width,beat.line_color,beat.fill_color)
gamerun()