I'm trying to learn to make games using pygame module in python.
I'm using the ubuntu terminal in Windows 10. This is my program.
# 1 - Import library
import os
os.environ['SDL_AUDIODRIVER'] = 'dummy'
import pygame
from pygame.locals import *
# 2 - Initialize the game
print("-1")
pygame.init()
pygame.display.list_modes()
print("0")
width, height = 640, 480
print("00")
screen=pygame.display.set_mode((width, height))
print("1")
# 3 - Load images
player = pygame.image.load("resources/images/virus.png")
print("3")
# 4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
print("4")
# 6 - draw the screen elements
screen.blit(player, (100,100))
print("5")
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
As soon as I run the program, I get this error.
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
-1
Traceback (most recent call last):
File "pythongame.py", line 10, in <module>
pygame.display.list_modes()
pygame.error: video system not initialized
I looked at many posts but of no use. Please tell me how to resolve this.
If you inizialize the pygame "modules" that need to using pygame.init() and an error is thrown, this error is not displayed or anithyng like that.
You can initialize all "internal pygame modules" using something like pygame.display.init() If you do it this way a traceback get's printed. I also don't have any trouble using pycharm to execute this program.
Maybe it's something with the unbuntu terminal that you use. Try initializing the display class using pygame.display.init()
Here a link about what I said: pygame.org
Related
I'm trying right know to begin with the pygame library and I follow a tutorial (you can find it right here:
https://dr0id.bitbucket.io/legacy/pygame_tutorial01.html )
My objective is to display an image on the screen and the transform it (e.g. move it, set alpha, background...)
So far, I've tried to search similar errors as mine, but found no precise examples in internet.
My code looks like this :
from pygame import *
def main():
#initialize
init()
#logo and caption
logo = image.load("logo32x32.png")
display.set_icon(logo)
display.set_caption("minimal game")
#create and size the screen
screen = display.set_mode((1366,700))
#set running var for the main loop
running = True
#Display an image, refresh the screen
blit = image.load("logo32x32.png")
screen.blit(blit, (32,32))
display.flip()
#main loop
while running:
#exit the loop if event is QUIT
for event in event.get():
if event.type == QUIT:
running = False
#run main only if the module execute as a main script
if __name__ == "__main__":
main()
The result I get by executing this script is a window with the image I wanted in the top left-hand corner for like 2 secs and then the application close by itself.
When I come back in VS code, it shows the following error:
PS C:\Users\Sacha PERUCHON\Documents\Cours\SNT\Cycle2\Python Scripts> & "C:/Users/Sacha
PERUCHON/AppData/Local/Microsoft/WindowsApps/python3.9.exe" "c:/Users/Sacha PERUCHON/Documents/Cours/SNT/Cycle2/Python Scripts/pygame_minimal.py"
pygame 2.1.2 (SDL 2.0.18, Python 3.9.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "c:\Users\Sacha PERUCHON\Documents\Cours\SNT\Cycle2\Python Scripts\pygame_minimal.py", line 30, in <module>
main()
File "c:\Users\Sacha PERUCHON\Documents\Cours\SNT\Cycle2\Python Scripts\pygame_minimal.py", line 24, in main
for event in event.get():
UnboundLocalError: local variable 'event' referenced before assignment
Any help is would be appreciated.
Try for event in pygame.event.get()
Also, just do import pygame, not from pygame import *
I am trying to write my first game using Python3 & Pygame in the Pycharm Community IDE. I am trying to make a simple Pong game :-D lol I am a beginner so please help me if you can.
When I run my code, I am getting the following error:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/TechSlugz/PycharmProjects/Pong-Project/Pong1.py", line 7, in <module>
clock = pygame.time.clock()
AttributeError: module 'pygame.time' has no attribute 'clock'
Process finished with exit code 1
The code is just the basic set up for a game window, check it out
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.setmode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
Can you notice anything that I am doing wrong to get this issue? None of my IDEs seem to recognise time as a pygame module but I am pretty damn sure it is...
The clock is a class, so you should use C instead of c.
clock = pygame.time.Clock()
and you have another problem, setmode is not an available function. You meant to write set_mode
The code that works fine for me:
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.Clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
This question already has answers here:
i keep getting the error 'module' object has no attribute 'init' [duplicate]
(2 answers)
Pygame.init() not working [duplicate]
(3 answers)
Closed 4 years ago.
When i run my pygame code, i get the following error:
>>>
RESTART: C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py
Traceback (most recent call last):
File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame/pygame 2.py", line 1, in <module>
import pygame
File "C:/Users/lanra/Desktop/2018 backups/2018 python/pygame\pygame.py", line 3, in <module>
pygame.init()
AttributeError: module 'pygame' has no attribute 'init'
My code:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("first game")
x = 50
y = 50
width = 40
height = 60
vel = 5
run= True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(win, (255,0,0))
pygame.quit()
Looks like you have a script called pygame.py locally to your test script. This is getting imported instead of the library.
The fix is to rename your local pygame.py script (which is presumably another version of this one as you are figuring out Pygame) so it does not conflict. In general, avoid naming your project files the same as the libraries that you are using.
You also have other errors in your code (please read the Pygame documentation and examples), but this will be the first fix you need to apply, and it is not specific to Pygame.
Here is a working version of your code:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("first game")
x = 50
y = 50
width = 40
height = 60
vel = 5
while True:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.draw.rect(win, (255,0,0), win.get_rect())
pygame.display.update()
Note the extra required param to pygame.draw.rect and call to pygame.display.update(). Also modified the while loop, as once you have called pygame.quit() you don't want to call things like pygame.event.get() else you will get error messages about no video system.
So I downloaded the windows wheel for Video Capture in my Pygame. But when I create a webcam instance I'm getting the error below. The code worked well until I started to implement the camera module. I've seen very few answers and they were from 2013. I was hoping Pygame would've improved on Windows more since then, but it's most likely me that's causing the problem. Any suggestions?
import pygame
import pygame.camera
pygame.init()
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#videocapture need to get the VideoCapture whl
pygame.camera.init()
webcam = pygame.camera.Camera(0, (640, 480), "RGB")
webcam.start()
pygame.display.set_caption("S.H.A.N.E.")
icon = pygame.image.load("SHANE pic.jpg")
pygame.display.set_icon(icon)
background = pygame.image.load("SHANE pic.jpg")
# (WIDTH, HEIGHT)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
running = True
while running:
# STARTING POINT (L/R, T/B), ENDING POINT (L/R, T/B)
screen.fill((0, 0, 0))
img = webcam.get_image()
screen.blit(img, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Here's the error I'm getting.
Traceback (most recent call last):
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gui2.py", line 10, in <module>
webcam = pygame.camera.Camera(0, (640, 480), "RGB")
File "C:\Users\Notebook\AppData\Local\Programs\Python\Python37-32\lib\site-<packages\pygame\_camera_vidcapture.py", line 60, in __init__
self.dev.setresolution(width, height)
vidcap.Error: Cannot set capture resolution.
webcam = pygame.camera.Camera("/dev/video0",(640,480))
You do not place the coordinates/size/scale of the camera where you have placed them, that is reserved for the file input.
Cheers!
Docs:
https://www.pygame.org/docs/tut/CameraIntro.html
You can use the following code to check what camera(s) you have.
pygame.camera.init()
camlist = pygame.camera.list_cameras()
print(camlist)
For example, mine prints ['FaceTime HD Camera']. Then you go
cam = pygame.camera.Camera('FaceTime HD Camera', (640,480))
If you mean yours prints ['0'], I wonder if you can fix the problem by simply changing 0 to '0'.
I'm very new to pygame, and am using the book "Beginning Game developement with Python and Pygame". I have pretty much typed the example code and keep getting the same
"pygame error: file not a windows.bmp file"
and would like to be able to load jpg/png as in the example in the book. I'm pretty sure I'm in the right directory for mine to work and the images I wanted to use are the same format as in the example. I have also searched for solutions but none of the answers seemed to work for me.
The code from the book is as follows (I have python 2.7.4, Ubuntu 13.04 and (I think) pygame 1.2.15):
background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'fugu.png'
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
my version of the code so far:
import os.path
background = os.path.join('Documents/Python/Pygame','Background.jpg')
cursor_image = os.path.join('Documents/Python/Pygame','mouse.png')
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
Thanks for your help :)
I can almost guarantee that you're getting your paths wrong. In your code, you've put in relative paths, meaning that pygame is looking for your assets in subfolders of the working directory (the directory where you execute your code).
A demo of how I think you would have to have things laid out and where your code is looking is below - in this example you would have a command prompt open in /home/your_username/Documents/my_games (or ~/Documents/my_games) and you'd be running python your_game_script.py.
|---home
|---your_username
|---Documents
|---some_subfolder
|---my_games
|---your_game_script.py
|---Documents
|---Python
|---Pygame
|---Background.jpg
|---mouse.png
This would work, but I suspect you don't have your folders set up this way, and that's the reason it's not working. If you run an interactive python prompt in the same folder as your game script, try the following:
import os
os.path.isfile('Documents/Python/Pygame/Background.jpg')
os.path.isfile('Documents/Python/Pygame/mouse.png')
I suspect the result will be false for both - meaning the files couldn't be found at that subfolder location. I would recommend that you have the following structure for your game files:
|---my_game
|---your_game_script.py
|---images
|---Background.jpg
|---mouse.png
Then in your_game_script.py you can load the files in the following way:
background = 'images/Background.jpg' #relative path from current working dir
cursor_image = 'images/mouse.png' #relative path from current working dir
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()