Endless attribute errors - python

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

Related

Import "pygame" could not be resolved

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.

pygame, screen not updating or filling with correct color

Trying to start a new pygame project. Very basic stuff. Just trying to color the screen. Currently the screen opens, and goes white. There are no errors in the console.
here is my code:
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
while True:
screen.fill((0,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip() # same result with .update
run_game()
id be happy if it would simply show a green screen. Any ideas what my issue could be?
Upgrading to the most recent version of pygame fixed this.
pip3 install pygame --upgrade
thanks to #rabbid76 for the solution

"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