将 pygame keyDown 事件与 pygame 混音器一起使用时出错
Error while using pygame keyDown event with pygame mixer
所以我试图弄乱 Pygame
模块,我使用了 pygame.mixer
和 pygame.key
。但是,当我 运行 以下代码块时,它会生成错误。
代码:
import pygame, sys
pygame.mixer.init()
# Assume the sound files exist and are found
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_a]:
pygame.mixer.Sound.play(kick)
if keyPressed[pygame.K_d]:
pygame.mixer.Sound.play(clap)
错误信息:
*** error for object 0x101008fd0: pointer being freed was not allocated
任何帮助都会很棒!
我尝试了您的代码并进行了修改,它可以工作 - Linux Mint,Python 2.7.10
import pygame
pygame.init() # init all modules
window = pygame.display.set_mode((600,400)) # required by event
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
pygame.event.get() # required by get_pressed()
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_a]:
print "A"
pygame.mixer.Sound.play(kick)
if keyPressed[pygame.K_d]:
print "D"
pygame.mixer.Sound.play(clap)
但是你可能有不同的问题,我帮不了你。
您的代码不起作用的原因有很多,请参阅下面我的。
import pygame, sys
pygame.init()
window = pygame.display.set_mode((600,400))
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
kick.play()
if event.key == pygame.K_d:
clap.play()
if event.type == pygame.QUIT:
pygame.quit()
quit()
首先,你必须为pygame到运行.
创建一个显示window
window = pygame.display.set_mode((600,400))
Second,请注意,您将 Sound 对象分配给 kick 和 clap 变量。这些是具有可以用点运算符引用的 play() 方法的 Sound 对象。这不是错误,只是有点不必要。阅读 documentation 以查看 Sound 和 play() 参数。你可以简单地做:
kick.play()
最后,一种更传统的事件处理方式。
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
这是一个 malloc "double free" 错误。
Multiple people 看到了这个错误,在浏览了很多网站之后,他们基本上说了同样的话:
You'll find out what the object is when you break in the debugger. Just look up the call stack and you will find where you free it. That will tell you which object it is.
The easiest way to set the breakpoint is to:
- Go to Run -> Show -> Breakpoints (ALT-Command-B)
- Scroll to the bottom of the list and add the symbol
malloc_error_break
以上是 link linked.
接受的答案
所以我试图弄乱 Pygame
模块,我使用了 pygame.mixer
和 pygame.key
。但是,当我 运行 以下代码块时,它会生成错误。
代码:
import pygame, sys
pygame.mixer.init()
# Assume the sound files exist and are found
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_a]:
pygame.mixer.Sound.play(kick)
if keyPressed[pygame.K_d]:
pygame.mixer.Sound.play(clap)
错误信息:
*** error for object 0x101008fd0: pointer being freed was not allocated
任何帮助都会很棒!
我尝试了您的代码并进行了修改,它可以工作 - Linux Mint,Python 2.7.10
import pygame
pygame.init() # init all modules
window = pygame.display.set_mode((600,400)) # required by event
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
pygame.event.get() # required by get_pressed()
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_a]:
print "A"
pygame.mixer.Sound.play(kick)
if keyPressed[pygame.K_d]:
print "D"
pygame.mixer.Sound.play(clap)
但是你可能有不同的问题,我帮不了你。
您的代码不起作用的原因有很多,请参阅下面我的。
import pygame, sys
pygame.init()
window = pygame.display.set_mode((600,400))
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
kick.play()
if event.key == pygame.K_d:
clap.play()
if event.type == pygame.QUIT:
pygame.quit()
quit()
首先,你必须为pygame到运行.
创建一个显示windowwindow = pygame.display.set_mode((600,400))
Second,请注意,您将 Sound 对象分配给 kick 和 clap 变量。这些是具有可以用点运算符引用的 play() 方法的 Sound 对象。这不是错误,只是有点不必要。阅读 documentation 以查看 Sound 和 play() 参数。你可以简单地做:
kick.play()
最后,一种更传统的事件处理方式。
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
这是一个 malloc "double free" 错误。
Multiple people 看到了这个错误,在浏览了很多网站之后,他们基本上说了同样的话:
You'll find out what the object is when you break in the debugger. Just look up the call stack and you will find where you free it. That will tell you which object it is.
The easiest way to set the breakpoint is to:
- Go to Run -> Show -> Breakpoints (ALT-Command-B)
- Scroll to the bottom of the list and add the symbol
malloc_error_break
以上是 link linked.
接受的答案