Python Forum
How can I make music with python/pygame?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make music with python/pygame?
#8
This pysynth looks interesting, but which file am I supposed to download? The only module I've ever installed is pygame, and with that I had to enter a command in my computer's command prompt.

As for game files, honestly, I prefer everything to be contained within the code of the game. For instance, my sprite function draws a sprite from a list of values (simply put, each value indicates a color, while its placement in the list is used to determine where its drawn on the screen). Here's the code I came up with that does this (I'm planning on making it into a custom class so I don't have to copy-paste all of this into each program):

import pygame
pygame.init()

mainWindow=pygame.display.set_mode((500,500))

x=200
y=250
pixel=10

row1=[0,0,0,1,1,0,0,0]
row2=[0,0,1,1,1,1,0,0]
row3=[0,1,1,1,1,1,1,0]
row4=[1,1,0,1,1,0,1,1]
row5=[1,1,1,1,1,1,1,1]
row6=[0,0,1,0,0,1,0,0]
row7=[0,1,0,1,1,0,1,0]
row8=[1,0,1,0,0,1,0,1]

sprite=row1+row2+row3+row4+row5+row6+row7+row8

def makeSprite():
    pixelx=x
    pixely=y
    r=0
    g=0
    b=0

    for a in sprite:
        if a==1:
            r=255
            g=255
            b=255
            pygame.draw.rect(mainWindow, (r,g,b),(pixelx,pixely,pixel,pixel))
        pixelx=pixelx+pixel
        if (pixelx-x)%(8*pixel)==0:
            pixelx=x
            pixely=pixely+pixel

def main():
    global x
    global y
    while True:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        key=pygame.key.get_pressed()
        if key[pygame.K_UP]:
            y=y-pixel
        if key[pygame.K_DOWN]:
            y=y+pixel
        if key[pygame.K_RIGHT]:
            x=x+pixel
        if key[pygame.K_LEFT]:
            x=x-pixel

        mainWindow.fill((0,0,0))
        makeSprite()
        pygame.display.update()

main()
Of course, this is an overly simplified version I made trying to figure out how to do this. I plan to have the thing be able to display more than 2 different colors, and also for the final version it will be possible to adjust both the dimensions of a sprite in pixels, and the exact dimensions of each pixel. Essentially, the class will be able to take 6 values: the x and y coordinates, the width and height of the sprite, and the width and height of the pixels. In my example program, the pixels are 10 screen pixels square, and the sprite is made up of an 8x8 grid of these. Doing this btw, I found that the pixel dimensions of the aliens in space invaders apparently aren't all 8x8 (one of them is 11x8), which is what inspired me to make it so that you can actually customize the dimensions of each sprite.

Anyway, as we see all the code for the sprite itself is stored within the program itself. I would also prefer to be able to synthesize music from within my programs themselves. When I do get around to making a sprite and music maker, honestly I'll probably have them output the files as a list that I can copy-paste into my programs, rather than having them make actual files.

Oh, and for the record, I've since learned to use set a framerate without using the time.delay command. I haven't bothered to do it with this program though since I only made it to learn how to draw sprites; I don't intend to actually use any of this in a program.
Reply


Messages In This Thread
RE: How can I make music with python/pygame? - by xBlackHeartx - Sep-24-2019, 03:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make enemy chase me in pygame? Jan_97 3 4,510 Nov-23-2021, 05:08 PM
Last Post: Jan_97
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,341 Nov-10-2021, 03:38 PM
Last Post: metulburr
  [PyGame] How to loop music with pygame finndude 2 4,030 Nov-09-2021, 10:54 AM
Last Post: metulburr
  cant make a door automatically close a few seconds after i open it in pygame cooImanreebro 2 2,287 Dec-15-2020, 08:40 PM
Last Post: michael1789
  Trying to make boundries for the pygame bluewing101 2 2,074 Mar-23-2020, 01:35 AM
Last Post: Windspar
  Problem with music - Pygame.error GaseBall 1 3,235 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020