PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


Miscellaneous

About PyGame

Related Stack Overflow questions:

Version issues

Related Stack Overflow questions:

The current PyGame release, 1.9.6 doesn’t support Python 3.9. I fyou don’t want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):

pip install pygame==2.0.0.dev20

or try to install a pre-release version by enabling the --pre option:

pip install pygame --pre

Windows

Related Stack Overflow questions:

Mac

Related Stack Overflow questions:

Ubuntu

Android

PyCharm

Exe

Related Stack Overflow questions:

“cx_freeze”

Related Stack Overflow questions:

py2app

Related Stack Overflow questions:

PyInstaller

Related Stack Overflow questions:

Pygame2Exe Errors that I can’t fix

Related Stack Overflow questions:

IDE

Visual Studio Code Intellisense

Related Stack Overflow questions:

Pylint

Related Stack Overflow questions:

File management

Related Stack Overflow questions:

File access

Related Stack Overflow questions:

It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
The name and relative path of the python file can be retrieved with __file__. So the absolute path to the python script is os.path.abspath(__file__)). The current working directory can be changed with os.chdir(path).
The current working directory can be get by os.getcwd() and can be print with print(os.getcwd()).

Copy the following at the beginning of your code to set the working directory to the same as the script’s directory:

import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))

An alternative solution is to find the absolute path. If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:

import pygame
import os

# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))

# [...]

# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')

surface = pygame.image.load(filePath)

The same can be achieved with the pathlib module. Change the working directory

import os, pathlib

os.chdir(pathlib.Path(__file__).resolve().parent)

or create an absolute filepath:

import pathlib

# [...]

filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)

Host online (Browser, Web, Html)

Related Stack Overflow questions:

Debug

Related Stack Overflow questions:

Hardware issues

Related Stack Overflow questions:

Lag

Related Stack Overflow questions:

Errors

Attribute error _init_

Related Stack Overflow questions:

SDL errors

Related Stack Overflow questions:

No module named ‘pygame’

Related Stack Overflow questions:

‘pygame’ has no attribute ‘init’

Related Stack Overflow questions:

No module called pygame.base

Related Stack Overflow questions:

No available video device

Related Stack Overflow questions:

Video system not initialized

Related Stack Overflow questions:

Local and global variables

Python Global Keyword

Related Stack Overflow questions:

In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.
A variable declared inside the function’s body or in the local scope is known as a local variable.
Reading a local variable x before it is declared causes the error:

UnboundLocalError: local variable x referenced before assignment

You have to use the global statement if you want to be interpret the variable as global variable.

Modul

Related Stack Overflow questions:

Subclass

Related Stack Overflow questions:

Class and instance variables

Related Stack Overflow questions:

Math

State engine

Pygame how to handle states

Compression

How to output pygame.image.save to a variable instead of a file?

Performance and speed

Where is 27 coming from?

I am wondering why it says the distance has to be less than 27? Why less than 27? Where is 27 coming from?

Collision detection in pygame not working

Collection

Pygame Basic calculator
scene

How to draw with mouse and save as 1s and 0s
scene

Pygame low frame rate on simple game
scene

How can I create a window where my program draws a dot ( which is point.png here ) wherever I click on the screen?
scene

How to code a rectangles that start from right in circular motion in pygame
scene

Pygame problem: how to execute conditional on collision?
scene

How do i correctly update the position and velocity of 100 class objects individually drawing with pygame?
scene

How can I blit an image on the screen where i click after pressing a button

pygame.mouse.get_pressed() not responding
scene

How to shade all the cells that will be useless when you click on them?
scene

How can I make my recursive function go back to continue recursing other cells
scene

How do I add a rubber band for mouse dragging in PyGame?
How do I add a rubber band for mouse dragging in PyGame?

Pygame Lever Images Display On Key Click Problem
Pygame Lever Images Display On Key Click Problem

Adding rects in list for displaying in 7-segment digits in pygame
Adding rects in list for displaying in 7-segment digits in pygame
Adding rects in list for displaying in 7-segment digits in pygame
Adding rects in list for displaying in 7-segment digits in pygame

📁 Minimal example - segment display

repl.it/@Rabbid76/PyGame-7SegementDisplay

Colours not printing - connect 4
Colours not printing - connect 4

My parabola is working fine alone, but it’s wrong in Pygame
My parabola is working fine alone, but it's wrong in Pygame

📁 Minimal example - parabola