Play WAV file in Python

You can use PyAudio. An example here on my Linux it works: #!usr/bin/env python #coding=utf-8 import pyaudio import wave #define stream chunk chunk = 1024 #open a wav format music f = wave.open(r”/usr/share/sounds/alsa/Rear_Center.wav”,”rb”) #instantiate PyAudio p = pyaudio.PyAudio() #open stream stream = p.open(format = p.get_format_from_width(f.getsampwidth()), channels = f.getnchannels(), rate = f.getframerate(), output = True) #read … Read more

How to move a sprite according to an angle in Pygame

you just need a little basic trig def calculat_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy.X + (speed*math.cos(angle_in_radians)) new_y = old_xy.Y + (speed*math.sin(angle_in_radians)) return new_x, new_y — edit — Here is your code from above edited to work import pygame, math, time screen=pygame.display.set_mode((320,240)) clock=pygame.time.Clock() pygame.init() def calculate_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy[0] + (speed*math.cos(angle_in_radians)) new_y = old_xy[1] + (speed*math.sin(angle_in_radians)) return new_x, … Read more

Pygame: Collision by Sides of Sprite

There is no function to get sides collision in PyGame. But you could try to use pygame.Rect.collidepoint to test if A.rect.midleft, A.rect.midright, A.rect.midtop, A.rect.midbottom, A.rect.topleft, A.rect.bottomleft , A.rect.topright, A.rect.bottomright are inside B.rect (pygame.Rect). EDIT: Example code. Use arrows to move player and touch enemy. (probably it is not optimal solution) import pygame WHITE = (255,255,255) … Read more

How do I focus light or how do I only draw certain circular parts of the window in pygame?

I suggest a solution, which combines a clipping region pygame.Surface.set_clip and drawing a black rectangle with a circular transparent area in the center. Define a radius and create a square pygame.Surface with twice the radius. radius = 50 cover_surf = pygame.Surface((radius*2, radius*2)) Set a white color key which identifies the transparent color (set_colorkey) a nd … Read more

Displaying unicode symbols using pygame

The unicode character is not provided by the “Tahoma” font. Use the “segoeuisymbol” font if your system supports it: seguisy80 = pygame.font.SysFont(“segoeuisymbol”, 80) Note, the supported fonts can be print by print(pygame.font.get_fonts()). Alternatively download the font Segoe UI Symbol and create a pygame.font.Font seguisy80 = pygame.font.Font(“seguisym.ttf”, 80) Use the font to render the sign: queenblack … Read more

How do I convert an OpenCV image (BGR and BGRA) to a pygame.Surface object

The shape attribute of a numpy.array is the number of elements in each dimension. The first element is the height, the second the width and the third the number of channels. A pygame.Surface can be generated by pygame.image.frombuffer. The 1st argument can be a numpy.array and the 2nd argument is the format (RGB or RGBA). … Read more

tech