Currently trying to run a pygame script. The Script itself is fine however I think the installation is causing me some problems. I am using a mac and my script is located on my desktop. If I would like to load images on to the script I must place this in '/users/himansu' In addition I am using spyder (anaconda) when running this. I have tried terminal which keeps bringing up the error:
AttributeError: module 'pygame' has no attribute 'init'
When I hit run in spyder it loads the pygame script however my keys are not moving the object on screen rather the cursor remains inside the spyder application.. This I know hasn't got anything to do with the code which is posted below:
import pygame
import os
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
print(x_change)
gameExit = False
while not gameExit:
#event handling loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
#one way to exit the loop
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
The code worked (the keys moved the object) when I was moving some files around however it is now back to normal. My working directory inside spyder is '/users/himansu'. I feel like if I can get this script running in terminal it may function. Thank you.
The code is fine i ran it and it worked. Try reinstalling pygame or adding sys to the import
import pygame, sys, os
direct download link ----> http://pygame.org/ftp/pygame-1.9.2a0.win32-py3.2.msi
Related
This question already has answers here:
Windows/Python pygame.error: video system not initialized after adding Mp3 file
(2 answers)
Closed 5 years ago.
I added some music to my pygame game and pygame.init() to the script to initialize the video system before it is called, but I think the code is so messy that nothing is in the right place even after moving everything to where it needs to be. As a result of this addition, I'm now getting this error still after adding pygame.init():
Traceback (most recent call last):
File "C:\Users\1234\AppData\Local\Programs\Python\Python36-32\My First game ERROR.py", line 31,
in for event in pygame.event.get():
pygame.error: video system not initialized
Here is the code that I have written:
# This just imports all the Pygame modules
import pygame
pygame.init()
class Game(object):
def main(self, screen):
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('St.Patrick game')
Game().main(screen)
clock = pygame.time.Clock()
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.quit():
pygame.quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
pygame.mixer.init(44100, -16,2,2048)
import time
pygame.mixer.music.load('The Tonight Show Star Wars The Bee Gees Stayin Alive Shortened.mp3')
pygame.mixer.music.play(-1, 0.0)
#class Player(pygame.sprite.Sprite):
# def __init__(self, *groups):
# super(Player, self.__init__(*groups)
#self.image = pygame.image.load('Sprite-01.png')
# self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
#def update(self):
# key = pygame
image = pygame.image.load('Sprite-01.png')
# initialize variables
image_x = 0
image_y = 0
image_x += 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
image_x -= 10
if key[pygame.K_RIGHT]:
image_x += 10
if key[pygame.K_UP]:
image_y -= 10
if key[pygame.K_DOWN]:
image_y += 10
screen.fill((200, 200, 200))
screen.blit(image, (image_x, image_y))
pygame.display.flip()
pygame.mixer.music.stop(52)
Your problem can be pygame.quit() inside while loop.
pygame.quit() uninitializes modules initialized with pygame.init() - but it doesn't exit while loop so while-loop tries to use event.get() in next loop. And then you get problem because you uninitialized modules.
Besides, it makes no sense
if event.type == pygame.quit():
it has to be
if event.type == pygame.QUIT:
pygame.quit() is function which ends what pygame.init() started.
pygame.QUIT is constant value - try print(pygame.QUIT) - which you can compare with event.type.
We use UPPER_CASE_NAMES for constant values. Read: PEP 8 -- Style Guide for Python Code
Finally, you need rather
running = True
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
So it exits loop but it doesn't uninitialize modules which you will need in rest of code.
I've just started programming in Python. I've been reading the book called "Making Games with Python & Pygame" and it's been very helpful so far, but I cannot find the reason why this simple program freezes. It should display an image and then move it by pressing left or right. It's strange, since it was working properly --I didn't change anything. I have tried everything so I'd really appreciate your help! Thanks in advance!
import pygame, sys
from pygame import *
# VARIABLES
x_res = 400
y_res = 300
DISPLAYSURF = 0
event = 0
person = 0
posx = 50
posy = 50
pygame.init()
DISPLAYSURF = pygame.display.set_mode((x_res, y_res))
pygame.display.set_caption("Test")
person = pygame.image.load("Person.png")
while True: # main game loop
DISPLAYSURF.blit(person, (posx, posy))
if event in pygame.event.get():
if event.type == KEYUP and event.key == K_RIGHT:
posx = posx + 5
DISPLAYSURF.fill((0,0,0))
if event.type == KEYUP and event.key == K_LEFT:
posx = posx - 5
DISPLAYSURF.fill((0,0,0))
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
pygame.display.update()
I'm not sure what you mean by "freezing" in this case, but I guess the problem is because you don't have an image called "Person.png", since when I tried out your code without an image on my file system called "Person.png" it also froze and gave me the error on the IDLE:
Traceback (most recent call last):
File "/Users/me/Desktop/your_program.py", line 17, in <module>
person = pygame.image.load("Person.png")
pygame.error: Couldn't open Person.png
Instead, if I have an image called "Person.png", it works properly.
Try writing "(wilst) true" instead of while? :)
This is my first post and i have no idea what i am doing hah, but i started learning pygame tonight
and i want to know to add a walk animation. I just want to variable "walk" to change images every .5 or so seconds of holding the
key down
import pygame
import time
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#imports pygame and initializes the module
pygameDisplay = pygame.display.set_mode((800,600))
#creates a screen
pygame.display.set_caption("Snake(;")
gameExit = False
walk = pygame.image.load("pokemon walk sprite sheet.png")
walkx = 350
walky = 330
walkx_change = 0
clock = pygame.time.Clock()
while not gameExit:
pygameDisplay.blit(walk,(walkx,walky))
pygame.display.update()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
walkx_change = -10
walk = pygame.image.load("walk sheet left.png")
#right here i want code to make it switch to another image after .5 or so seconds
if event.key == pygame.K_RIGHT:
walkx_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
walkx_change = 0
walkx += walkx_change
pygameDisplay.fill(white)
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
You can't do animation like that. You have to create a class with Sprite module first. And you have to import every image(frame) of your animation.
class Asker(Sprite):
def __init__(self, konum):
Sprite.__init__(self)
self.resimler = [pygame.image.load("01.png"), pygame.image.load("02.png"),pygame.image.load("03.png"),pygame.image.load("04.png"),pygame.image.load("05.png"),
pygame.image.load("06.png"),pygame.image.load("07.png"),pygame.image.load("08.png"),pygame.image.load("09.png"),pygame.image.load("10.png"),
pygame.image.load("11.png"),pygame.image.load("12.png"),pygame.image.load("13.png"),pygame.image.load("14.png"),pygame.image.load("15.png"),
pygame.image.load("16.png")]
self.image = self.resimler[0]
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = konum
self.say = 0
It's an example from my script, see I load 16 pictures, you have to split them first with gimp or photoshop.That pictures I splitted them from an animation. Then you have to rotate them etc etc. You should check some tutorials of Sprite, before it, learn basic Pygame.
When I am trying to catch keys pressed, they are printed in Terminal, but not caught by pygame and the script. Script is executed as follows:
>>>import scriptname
>>>scriptname.wa()
scriptname file:
import pygame
from pygame.locals import *
def wa():
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
alive_key = True
while alive_key:
for event in pygame.event.get():
if event.type == QUIT:
alive_key = False
elif event.type == KEYDOWN and event.key == K_q:
print '\nThis is not happening\n'
screen.fill((0, 0, 0))
if pygame.mouse.get_pressed()[0]:
pygame.event.post(
pygame.event.Event(KEYDOWN, key=K_q, mod=0, unicode=u'q'))
pygame.display.update()
pygame.quit()
If events are created on mouse press (as presented in code), they work.
I am using OS X 10.8.5, python 2.7, pygame2.7 1.9.1. Everything works perfectly in Windows 7 with similar configuration.
Thanks!
change your code to:
elif event.type == KEYDOWN or event.type == pygame.KEYDOWN:
# for testing purpose
print event
if event.key == pygame.K_q:
print '\nThis is not happening\n
I'm using OS X 10.9.5, python 2.7.7, pygame-1.9.2pre-py2.7-macosx10.7. Had the similar problem as yours earlier. I found this solution here: http://content.gpwiki.org/index.php/Python:Pygame_keyboard_input
I think this is not mac's fault, possibly.
This is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption('window')
black = (0,0,0)
white = (255, 255, 255)
logo = pygame.image.load('logo.png').convert_alpha()
clock = pygame.time.Clock()
# Sprites
m1 = pygame.image.load('m1.png').convert_alpha()
m2 = pygame.image.load('m2.png').convert_alpha()
mci = 1
x, y = 0, 0
run = True
while run:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x -= 10
if event.type == pygame.K_RIGHT:
x += 10
if event.type == pygame.K_UP:
y -= 10
if event.type == pygame.K_DOWN:
y += 10
window.fill(white)
pygame.draw.rect(window, black,(x,y, 50, 50))
pygame.display.flip()
clock.tick(10)
Everything shows up, but I can't move the rectangle with my arrow keys, and I always get an error after I quit out of it, help.. Thanks in advance!
P.S I'm obviously copying from a tutorial, but i'm not sure what I've done wrong?
As you worked out, you need to use event.key == .... You probably also want to watch the nesting of your loop, currently you have:
while running:
for event in list_of_events:
process_event
draw_to_screen
wait_a_while
This caused a problem in another question (https://stackoverflow.com/a/13866804/2372604). What you probably want is something more like:
while running:
for event in list_of_events:
process_event
draw_to_screen
wait_a_while
You might also want to change pygame.quit(); sys.exit() to run = false and then add pygame.quit() at the end of the program.
The mistake what I wrote event.type instead of event.key.