import * only allowed at module level. Python 3.3 using Pyscripter - python

I'm fairly new to programming in general and I just started using python to try and make a simple game using pygame. If I run the following code in the Python IDLE shell it works fine but if I use Pyscripter I get the error:
SyntaxError: import * only allowed at module level
I really like using Pyscripter because so far it has made learning the syntax much easier but now I don't understand what is wrong. Any help would be great. Thanks.
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300),0,32)
pygame.display.set_caption('Hello World!')
while True: #main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

The problem is when you execute from pygame.locals import * you are accessing all from a file, not a module. When you do something like from pygame import *, it should work. It is just that you can only use import * at the module level

Related

pygame is not responding after playing an audio file

I'm newbie in pygame and not a very experienced python programmer,so hope you'll help. I'm trying to make my own audio player on python using pygame and tkinter. So there is my code:
from tkinter import *
from tkinter.filedialog import *
import pygame
import sys
from pygame import*
mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
def play(event):
mixer.music.load("Chillingmusic.wav")
mixer.music.play()
while mixer.music.get_busy():
time.Clock().tick(10)
def pause(event):
pygame.mixer.music.pause()
song.pause()
root=Tk()
txt=Text(root)
m=Menu(root)
root.config(menu=m)
fm=Menu(m)
m.add_cascade(label="File",menu=fm)
fm.add_command(label="Open",command_=open)
but=Button(text=">",bg="lightgreen")
but.grid(row=0,column=0)
but.bind("<Button-1>",play)
but1=Button(text="p",bg="lightblue")
but1.grid(row=0,column=1)
but1.bind("<Button-1>",pause)
root.mainloop()
pygame.quit()
Problem is when I start playing music by using play function audiofile is playing,but after that interface stop responding so I can't for example use my pause function.
I use python 3.6 and pygame 1.9.3 on Windows 10(64 bits).
Your play function will not finish until the song is finished. That means that your program has to wait for it to finish and cannot execute anything else during that time, making everything else unresponsive. Don't use the while loop.
def play(event):
mixer.music.load("Chillingmusic.wav")
mixer.music.play()
mixer is using another thread than your main thread, so the music will play and your own code can execute simultaneously.

Python pygame IndentationError

So I just finished up with a good amount of learning python and working on learning pygame. So I am following along with this free online book, and the first thing of code in the book of making a window with hello world on the top is giving me an error:
Heres the code:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Heres the error:
File "pygame.py", line 9
if event.type == QUIT:
^
IndentationError: expected an indented block
Thanks in advance!
As presented, this code works fine at this end. If you are still having a problem I suggest you look at tabs vs spaces in your editor.
Be aware that Python is a space sensitive language. The compiler actually cares whether there is a mix of spaces and tabs throughout the program.
Stick to one of either kind throughout your code and this will resolve the issue.

Importing a module and using its methods

I am looking at a snippet of code and I just don't understand how it works:
import pygame, sys
from pygame.locals import *
on the first line pygame is imported, and on the second line, all the methods of a subset of pygame is invoked. If the first line imports all of pygame, why do we have to specifically import a subset of the module again? Why doesn't a mere import pygame do the job in the first place?
A mere import pygame would suffice, but the author wanted to have a shorthand access to the constants of pygame. For example, instead of:
import pygame
...
resolution = pygame.locals.TIMER_RESOLUTION
it may be sometimes preferable to have
import pygame
from pygame.locals import *
...
resolution = TIMER_RESOLUTION
Note that you should still import pygame itself to be able to access to other methods/properties (other than pygame.locals.) of pygame.
The idea is that you can call all the functions in pygame.locals without using pygame.locals.someFunction, but instead someFunction.

Why am I getting this Pygame Music Error?

pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.error: Unrecognized music format
there is the error. As you can see I am using an ogg file. Here is the code:
import pygame, sys, Funk
from tileC import Tile
from object_classes import *
from interaction import interaction
from A_Star import A_Star
pygame.init()
pygame.font.init()
pygame.mixer.init()
pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.mixer.music.play(-1)
Any ideas? Thanks.
Try with this if you want the background to infinite loop:
pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.mixer.music.play(-1, 0.0)
Or this if you want it to play only one time:
pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.mixer.music.play()
(I'm still new to python so i don't know if this will work for you, for me it work)
Oh yeah, and have you tried to set up a window? because i think it can't really work if you don't set up a window.

PYGAME: how to activate event by pressing a key

So I'm having trouble with activating sound, when pressed a key.
what i have so far is
if event.type == pygame.key(K_a):
self.sound.play()
I get the error code called global name 'K_a' is not defined.
If anyone can help me correct this code? Thanks!
You should use pygame.K_a. This way:
if event.type == pygame.key(pygame.K_a):.
If it doesn't works for you, then you've probably didn't import pygame as needed. Import pygame this way: from pygame import *. You may have an error with pygame.key, Because you shouldn't use it this way. The best practice is to use the following line:
if event.type == pygame.K_a:

Categories