Import "pygame" could not be resolved - python

I installed pygame using the pip in command prompt but it still shows an error. I have already attempted changing the path so pip could properly be accessed and using the line "pygame.init()" but the error still appears.
Here's the code:
import pygame, sys
pygame.init()
w, h = 600, 600
r = (255, 0, 0)
s = pygame.display.set_mode( (w, h) )
s.fill(r)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()

by the way if it still isn't working you could try using Pycharm. If you can't do that you could try using replit and downloading the pygame package, then exporting it with Github.

Related

Pygame window not opening until all processes are complete macOS 12.2

macOS Monterey 12.2 M1 Pro
Version: pygame 2.1.2 (SDL 2.0.18, Python 3.10.7)
Pygame will not display a window until all my code has run and the program is waiting for input to exit. Once program is complete, Pygame window shows. If I didn't use the following code:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
then the window wouldn't even display at all.
Tried uninstalling and reinstalling pygame. Tried installing older version of pygame (which doesn't work, keep getting errors). Tried installing most recent development version. (2.1.3.dev8)
Having to transfer files to old windows laptop to check if program even works properly. Any ideas why this is happening? Is there an older dev version of pygame that will work instead?
example:
import pygame
w_in_squares = 8
h_in_squares = 8
square_dim = 50
drawing_window = pygame.display.set_mode((w_in_squares*square_dim, h_in_squares*square_dim))
drawing_window.fill((127, 127, 127))
the_colour_white = (255, 255, 255)
the_colour_black = ( 0, 0, 0)
current_colour = the_colour_white
for i in range(0, h_in_squares):
for j in range(0, w_in_squares):
pygame.draw.rect(drawing_window, current_colour, (i * square_dim, j * square_dim, square_dim, square_dim))
pygame.display.update()
pygame.time.delay(100)
if current_colour == the_colour_white:
current_colour = the_colour_black
else:
current_colour = the_colour_white
if current_colour == the_colour_white:
current_colour = the_colour_black
else:
current_colour = the_colour_white
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
The point of this program is that you are supposed to be able to watch each square of a checkerboard being drawn one by one. Though, only after every square is drawn does the window display.
I am not sure on your entire code, but try adding a pygame.display.update() in your while loop, this should make pygame keep the screen refreshed.
Again this could not solve your solution because you do not show your full program.

Endless attribute errors

I've been trying out a python application that displays text on the full screen. Couldnt convert it into an exe file to run on windows so tried it online.
The latest is AttributeError: 'NoneType' object has no attribute 'fill'.
There seems no end to attribute errors. It began off with a small compact code but what with attribute error after another whenever I added the suggestions from StackOverflow it's a bit bloated now. Would appreciate some way ahead.
I'm running it on the Colaboratory site (https://colab.research.google.com/#)
It's always come up with numerous blocks that after all the amendments it looks like this.
!apt-get -qq install -y libarchive-dev && pip install -U libarchive
import libarchive
!pip install pygame
import pygame
pygame.init()
pygame.init()
def borders(cells):
width=get_screen_width()
height=get_screen_height()
pygame.display.fullscreen = True
pygame.display
pygame.init()
import time
pygame.font.init()
import platform
if platform.system() == "Windows":
def play_game():
display = pygame.display.set_mode((800,600))
bg_color = (230,230,230)
!pip install pgzero
import pgzero
from pgzero.game import screen
screen: pgzero.screen.Screen
screen.fill(bg_color)
myfont = pygame.font.Font(None, 90)
f = open("/Users/bgr/Desktop/Advice.txt","r")
for line in f:
for word in line.split():
text = myfont.render(word,1,(0,255,0))
screen.blit(text, (300,300))
pygame.display.update()
time.sleep(0.2)
screen.fill((background))
pygame.display.update()
pygame.display.quit() ````
Thanks
Your question is a little too broad, so I'm just going to answer a couple
I've been trying out a python application that displays text on the full screen.
pgzero aim to be super simple and hiding a lot of details, it has hooks that pgzero will call if you provides them in the script, to display something on the screen you need to provide draw() hooks (function).
import pgzrun
import pygame
WIDTH = 800
HEIGHT = 600
green = (0, 128, 0)
white = (255, 255, 255)
def draw():
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
screen.fill(green)
screen.draw.text("Hello world", (100, 100), color=white)
pgzrun.go()
I'm running it on the Colaboratory site (https://colab.research.google.com/#)
I think you should your local pc instead, see this

"pygame.error: video system not initialized"

Does anyone know why this error keeps coming up? I'm not sure what's wrong with the code. I've copied this from a video just to test mouse tracking and yet my version isn't working even though the two sets of code are identical (I believe). Here is the video I took the code from: https://www.youtube.com/watch?v=dxhNcQ-FDaY
import pygame
from pygame.locals import *
import sys
class MousePos(object):
def __init(self):
pygame.init()
self.myFont = pygame.font.SysFont("Arial", 24)
pygame.display.set_caption("Mouse Events")
self.myScreen = pygame.display.set_mode((400, 400), 0 ,32)
self.myScreen.fill((255, 255, 255))
pygame.display.update()
def coords(self):
text = self.myFont.render(str(pygame.mouse.get_pos()), True, (0, 0, 0))
pygame.draw.rect(self.myScreen, (255, 255, 255), (200, 200, 100, 100), 0)
self.myScreen.blit(text, (200, 200))
pygame.display.update()
if __name__ == "__main__":
newMouse = MousePos()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
newMouse.coords()
The problem with your code is that your initialization method __init__, is missing a underscore. Since your class is never being initialized, your never creating a Pygame screen. Thus the error
Change this line: def __init(self):, To this: def __init__(self):
My guess would be a missing dependency or library mismatch.
How did you install pygame, and were there any errors.
I recommend you use pip or your distribution's package manager.
Also, you should make sure that any non-python APIs and modules that you might need are all installed.

pygame.error: file not a windows.bmp file (have looked at other similar questions but unsuccessful so far)

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()

Pygame.error: file is not a WINDOWS BMP file (mac osx)

Hi after much research I cant find the answer.
running mac osx 10.8.4 python 2.7.5 and pygame 1.9.2.
all modules were found in the build of pygame and reinstalling doesnt fix the issue
while running:
import pygame
import math
import random
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
blue = (0,0,255)
green = (0,255,0)
pygame.init()
print pygame.image.get_extended()
size = (1000,700)
screen = pygame.display .set_mode(size)
pygame.display.set_caption("My game")
done = False
clock = pygame.time.Clock()
background_image = pygame.image.load("red_x.png").convert()
while done == False:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
screen.fill(black)
screen.blit(background_image,[0,0])
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(20)
pygame.quit()
I get a file is not a valid windows BMP error.
pygame.images.get_extended() returns 0
and
try:
import SDL_image
print "Loaded SDL_image"
except:
print "Failed to import SDL_image"
try:
import libpng
print "Loaded libpng"
except:
print "Failed to import libpng"
returns both failed import messages. I think thats all the tests i saw while searching for this and all of their solutions didnt work.
I had the same problem, but I managed to solve it installing the following version of Pygame: http://pygame.org/ftp/pygame-1.9.1release-python.org-32bit-py2.7-macosx10.3.dmg

Categories