OSDN Git Service

first master
authorTomoki_Imai <tomo0319@users.sourceforge.jp>
Sat, 18 Sep 2010 03:15:58 +0000 (12:15 +0900)
committerTomoki_Imai <tomo0319@users.sourceforge.jp>
Sat, 18 Sep 2010 03:15:58 +0000 (12:15 +0900)
14 files changed:
circle.png [new file with mode: 0644]
circlet.png [new file with mode: 0644]
gutil.py [new file with mode: 0644]
gutil.pyc [new file with mode: 0644]
key.txt [new file with mode: 0644]
line.png [new file with mode: 0644]
line2.png [new file with mode: 0644]
line2t.png [new file with mode: 0644]
linet.png [new file with mode: 0644]
main.py [new file with mode: 0644]
square.png [new file with mode: 0644]
squaret.png [new file with mode: 0644]
triangle.png [new file with mode: 0644]
trianglet.png [new file with mode: 0644]

diff --git a/circle.png b/circle.png
new file mode 100644 (file)
index 0000000..a770d66
Binary files /dev/null and b/circle.png differ
diff --git a/circlet.png b/circlet.png
new file mode 100644 (file)
index 0000000..8c102f7
Binary files /dev/null and b/circlet.png differ
diff --git a/gutil.py b/gutil.py
new file mode 100644 (file)
index 0000000..1e3d8de
--- /dev/null
+++ b/gutil.py
@@ -0,0 +1,91 @@
+
+import pygame
+
+from OpenGL.GL import *
+from OpenGL.GLU import *
+
+def init(w, h):
+    pygame.init()
+    pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF|pygame.FULLSCREEN)
+#    pygame.display.set_mode((w,h),pygame.OPENGL|pygame.DOUBLEBUF)
+
+    glClearColor(0.0, 0.0, 0.0, 1.0)
+    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluOrtho2D(0, w, 0, h);
+    glMatrixMode(GL_MODELVIEW);
+
+    #set up texturing
+    glEnable(GL_TEXTURE_2D)
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
+
+
+def load_image(image):
+    textureSurface = pygame.image.load(image)
+
+    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
+
+    width = textureSurface.get_width()
+    height = textureSurface.get_height()
+
+    texture = glGenTextures(1)
+    glBindTexture(GL_TEXTURE_2D, texture)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
+    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )
+
+    return texture, width, height
+
+
+def delTexture(texture):
+    glDeleteTextures(texture)
+
+
+def createTexDL(texture, width, height):
+    newList = glGenLists(1)
+    glNewList(newList,GL_COMPILE);
+    glBindTexture(GL_TEXTURE_2D, texture)
+    glBegin(GL_QUADS)
+    glTexCoord2f(0, 0); glVertex2f(0, 0)    # Bottom Left Of The Texture and Quad
+    glTexCoord2f(0, 1); glVertex2f(0, height)    # Top Left Of The Texture and Quad
+    glTexCoord2f(1, 1); glVertex2f( width,  height)    # Top Right Of The Texture and Quad
+    glTexCoord2f(1, 0); glVertex2f(width, 0)    # Bottom Right Of The Texture and Quad
+    glEnd()
+    glEndList()
+
+    return newList
+
+def create_texture(image):
+    textureSurface = pygame.image.load(image)
+
+    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
+
+    width = textureSurface.get_width()
+    height = textureSurface.get_height()
+
+    texture = glGenTextures(1)
+    glBindTexture(GL_TEXTURE_2D, texture)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
+    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )
+
+    newList = glGenLists(1)
+    glNewList(newList,GL_COMPILE);
+    glBindTexture(GL_TEXTURE_2D, texture)
+    glBegin(GL_QUADS)
+    glTexCoord2f(0, 0); glVertex2f(0, 0)    # Bottom Left Of The Texture and Quad
+    glTexCoord2f(0, 1); glVertex2f(0, height)    # Top Left Of The Texture and Quad
+    glTexCoord2f(1, 1); glVertex2f( width,  height)    # Top Right Of The Texture and Quad
+    glTexCoord2f(1, 0); glVertex2f(width, 0)    # Bottom Right Of The Texture and Quad
+    glEnd()
+    glEndList()
+
+    return newList
+
+
+
+def delDL(list):
+    glDeleteLists(list, 1)
diff --git a/gutil.pyc b/gutil.pyc
new file mode 100644 (file)
index 0000000..7799d87
Binary files /dev/null and b/gutil.pyc differ
diff --git a/key.txt b/key.txt
new file mode 100644 (file)
index 0000000..0dec8a7
--- /dev/null
+++ b/key.txt
@@ -0,0 +1,12 @@
+#60 がド
+#Key,Sound,X,Y
+K_a,56,200,200
+K_s,57,300,300
+K_d,58,400,400
+K_f,59,500,500
+K_g,60,200,400
+K_h,61,500,400
+K_j,62,600,100
+K_k,63,700,300
+K_l,64,800,200
+K_SEMICOLON,65,100,200
diff --git a/line.png b/line.png
new file mode 100644 (file)
index 0000000..5693b08
Binary files /dev/null and b/line.png differ
diff --git a/line2.png b/line2.png
new file mode 100644 (file)
index 0000000..71bd986
Binary files /dev/null and b/line2.png differ
diff --git a/line2t.png b/line2t.png
new file mode 100644 (file)
index 0000000..e97b239
Binary files /dev/null and b/line2t.png differ
diff --git a/linet.png b/linet.png
new file mode 100644 (file)
index 0000000..050750f
Binary files /dev/null and b/linet.png differ
diff --git a/main.py b/main.py
new file mode 100644 (file)
index 0000000..62fbc6f
--- /dev/null
+++ b/main.py
@@ -0,0 +1,322 @@
+#!/usr/bin/env python
+#coding:utf-8
+
+import pygame
+import pygame.midi
+import gutil as gu
+import os
+import math
+import random
+import threading
+from pygame.locals import *
+from OpenGL.GL import *
+import psyco
+import time
+import sys
+
+CURRENT_DIR = os.path.dirname(__file__)
+INSTRUMENT = 0
+
+WIDTH = 1024
+HEIGHT = 768
+KEY_MAP = os.path.join(CURRENT_DIR,"key.txt")
+FPS = 60
+MIDI_PORT = 2
+
+#min_note = 46
+#max_note = 75
+#number_of_notes = max_note - min_note + 1
+#theta2r = math.pi / 180
+
+new_key = [48,50,52,53,54,55,57,59,60,62,64,65,67,69,71,72,74,76]
+number_of_notes = len(new_key)
+space = WIDTH / number_of_notes
+
+class AnoEngine:
+    def __init__(self,writer,output=0):
+        pygame.midi.init()
+        self.midiout = pygame.midi.Output(MIDI_PORT)
+        self.INSTRUMENT = 0 #0:Piano#19:Organ,56:Trympet
+        self.set_instrument(self.INSTRUMENT)
+        self.sustain = False
+        self.default_volume = 128
+        self.writer = writer
+        self.on_notes = []
+        self.keys = {}
+        self.mouse_key = 0
+        #for i in xrange(number_of_notes + 1):
+        #    self.keys[i] = min_note + i
+        self.key_map = self.load_key_map(KEY_MAP)
+
+    def set_instrument(self,instrument):
+        self.INSTRUMENT = instrument
+        self.midiout.set_instrument(instrument)
+
+    def rebus_sustain(self):
+        if self.sustain:
+            self.sustain = False
+        else:
+            self.sustain = True
+
+    def note_on(self,no,vol=100):
+        if not no in self.on_notes:
+            self.midiout.note_on(no,vol)
+            self.on_notes.append(no)
+
+    def note_off(self,no):
+        if no in self.on_notes:
+            self.midiout.note_off(no,0)
+            self.on_notes.remove(no)
+
+    def load_key_map(self,filename):
+        dic = {}
+        for line in open(filename,"r"):
+            if line[0] == "#":
+                pass
+            else:
+                [key,no,x,y] = [l.strip() for l in line.split(",")]
+                dic[eval(key)] = (int(no),(int(x),int(y)))
+        return dic
+
+    def on_event(self,events):
+        for event in events:
+            if event.type == KEYDOWN:
+                if event.key in self.key_map:
+                    self.note_on(self.key_map[event.key][0])
+                    self.writer.start(self.key_map[event.key][1])
+                if event.key == K_ESCAPE:
+                    exit()
+            elif event.type == KEYUP:
+                if event.key in self.key_map:
+                    self.note_off(self.key_map[event.key][0])
+            elif event.type == MOUSEBUTTONDOWN:
+                if event.button == 1:
+                    (x,y) = pygame.mouse.get_pos()
+                    y = HEIGHT - y
+                    #key = self.keys[x // space]
+                    key = new_key[x // space]
+                    if y < (HEIGHT / 2):
+                        self.note_on(key)
+                        self.writer.start((x,y))
+                        self.mouse_key = key
+                    else:
+                        self.note_on(key)
+                        self.writer.start((x,y))
+                        self.mouse_key = key
+                elif event.button == 2:
+                    self.rebus_sustain()
+            elif event.type == MOUSEBUTTONUP:
+                if event.button == 1:
+                    (x,y) = pygame.mouse.get_pos()
+                    y = HEIGHT - y
+                    key = [x // space]
+                    self.note_off(self.mouse_key)
+            elif event.type == QUIT:
+                exit()
+
+class Circle:
+    def __init__(self,tex_lis):
+        self.maxrate = 2.0
+        self.tex_lis = tex_lis
+        self.on = False
+        self.x,self.y = (-100,-100)
+        self.ratep = 0.02
+        self.rate = 0
+
+    def draw(self):
+        if self.on:
+            glLoadIdentity()
+            glTranslatef(self.x - self.rate * 500,self.y - self.rate * 500,0)
+            glScale(self.rate,self.rate,0)
+            glCallList(self.tex_lis)
+
+    def start(self,(x,y)):
+        self.on = True
+        self.x,self.y = x,y
+        self.rate = 0
+
+    def update(self):
+        if self.on:
+            self.rate = self.rate + self.ratep
+            if self.rate > self.maxrate:
+                self.on = False
+                self.x,self.y = (-100,-100)
+
+class Line:
+    def __init__(self,tex_lis):
+        self.maxrate = 1.0
+        self.tex_lis = tex_lis
+        self.on = False
+        self.x,self.y = 0,0
+        self.ratep = 0.02
+        self.rate = 0
+        self.theta = 0
+
+    def draw(self):
+        if self.on:
+            glLoadIdentity()
+            glTranslatef(self.x - 750,self.y - 750,0)
+            glTranslatef(750,750,0)
+            glRotatef(self.theta,0,0,1)
+            glTranslatef(-750,-750,0)
+            glCallList(self.tex_lis)
+
+    def start(self,(x,y)):
+        self.on = True
+        self.x,self.y = x,y
+        self.rate = 0
+        self.theta = random.randint(0,360)
+
+    def update(self):
+        if self.on:
+            self.rate = self.rate + self.ratep
+            if self.rate > self.maxrate:
+                self.on = False
+                self.x,self.y = 0,0
+
+class Square:
+    def __init__(self,tex_lis):
+        self.maxrate = 2.0
+        self.tex_lis = tex_lis
+        self.on = False
+        self.x,self.y = (-100,-100)
+        self.ratep = 0.02
+        self.rate = 0
+        self.angle = 0
+        self.anglep = 1
+
+    def draw(self):
+        if self.on:
+            glLoadIdentity()
+            glTranslatef(self.x - self.rate * 500,self.y - self.rate * 500,0)
+            glScale(self.rate,self.rate,0)
+            glTranslatef(500,500,0)
+            glRotatef(self.angle,0,0,1)
+            glTranslatef(-500,-500,0)
+            glCallList(self.tex_lis)
+
+    def start(self,(x,y)):
+        self.on = True
+        self.x,self.y = x,y
+        self.rate = 0
+        self.anglep = random.choice((-1,1))
+        self.angle = random.randint(0,360)
+
+    def update(self):
+        if self.on:
+            self.rate = self.rate + self.ratep
+            self.angle = self.angle + self.anglep
+            if self.rate > self.maxrate:
+                self.on = False
+                self.x,self.y = (-100,-100)
+
+class Triangle:
+    def __init__(self,tex_lis):
+        self.maxrate = 2.0
+        self.tex_lis = tex_lis
+        self.on = False
+        self.x,self.y = (-100,-100)
+        self.ratep = 0.02
+        self.rate = 0
+        self.angle = 0
+        self.anglep = 1
+
+    def draw(self):
+        if self.on:
+            glLoadIdentity()
+            glTranslatef(self.x - self.rate * 500,self.y - self.rate * 500,0)
+            glScale(self.rate,self.rate,0)
+            glTranslatef(500,500,0)
+            glRotatef(self.angle,0,0,1)
+            glTranslatef(-500,-500,0)
+            glCallList(self.tex_lis)
+
+    def start(self,(x,y)):
+        self.on = True
+        self.x,self.y = x,y
+        self.rate = 0
+        self.anglep = random.choice((-1,1))
+        self.angle = random.randint(0,360)
+
+    def update(self):
+        if self.on:
+            self.rate = self.rate + self.ratep
+            self.angle = self.angle + self.anglep
+            if self.rate > self.maxrate:
+                self.on = False
+                self.x,self.y = (-100,-100)
+
+
+class Writer:
+    def __init__(self):
+        circle_tex = gu.create_texture(os.path.join(CURRENT_DIR,"circlet.png"))
+        square_tex = gu.create_texture(os.path.join(CURRENT_DIR,"squaret.png"))
+        line_tex = gu.create_texture(os.path.join(CURRENT_DIR,"line2t.png"))
+        triangle_tex = gu.create_texture(os.path.join(CURRENT_DIR,"trianglet.png"))
+        self.circles = [Circle(circle_tex) for i in xrange(8)]
+        self.squares = [Square(square_tex) for i in xrange(8)]
+        self.lines = [Line(line_tex) for i in xrange(8)]
+        self.triangles = [Triangle(triangle_tex) for i in xrange(8)]
+
+    def draw(self):
+        for c in self.circles:
+            c.draw()
+        for s in self.squares:
+            s.draw()
+        for l in self.lines:
+            l.draw()
+        for t in self.triangles:
+            t.draw()
+
+    def start(self,(x,y)):
+        r = random.choice((0,1,2,3))
+        if r == 0:
+            for c in self.circles:
+                if not c.on:
+                    c.start((x,y))
+                    return
+        elif r == 1:
+            for s in self.squares:
+                if not s.on:
+                    s.start((x,y))
+                    return
+        elif r == 2:
+            for l in self.lines:
+                if not l.on:
+                    l.start((x,y))
+                    return
+        elif r == 3:
+            for t in self.triangles:
+                if not t.on:
+                    t.start((x,y))
+                    return
+
+    def update(self):
+        for c in self.circles:
+            c.update()
+        for s in self.squares:
+            s.update()
+        for l in self.lines:
+            l.update()
+        for t in self.triangles:
+            t.update()
+
+def main():
+    psyco.full()
+    screen = gu.init(WIDTH,HEIGHT)
+    pygame.mouse.set_visible(True)
+    writer = Writer()
+    ano = AnoEngine(writer,MIDI_PORT)
+    clock = pygame.time.Clock()
+
+    while True:
+        clock.tick(FPS)
+        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+        glLoadIdentity()
+        ano.on_event(pygame.event.get())
+        writer.update()
+        writer.draw()
+        pygame.display.flip()
+
+if __name__ == "__main__":
+    main()
diff --git a/square.png b/square.png
new file mode 100644 (file)
index 0000000..587372c
Binary files /dev/null and b/square.png differ
diff --git a/squaret.png b/squaret.png
new file mode 100644 (file)
index 0000000..8567d10
Binary files /dev/null and b/squaret.png differ
diff --git a/triangle.png b/triangle.png
new file mode 100644 (file)
index 0000000..2fcfefe
Binary files /dev/null and b/triangle.png differ
diff --git a/trianglet.png b/trianglet.png
new file mode 100644 (file)
index 0000000..ffd89c3
Binary files /dev/null and b/trianglet.png differ