So in every Pygame example I've seen, there seems to be a resolution the game is designed to be played on and that's it. No options for setting higher or lower resolutions. If you do change the resolution via display.set_mode, then the scale of the game textures/graphics get out of whack and the game becomes unplayable.
In all the examples I've seen, it looks like you would have to actually create different sized texture sets for each target resolution... Which seems ridiculous. That leads me to my question.
If I design a game based on a standard resolution of 480x270, is it possible to simply scale the output of that surface to 1080p (or 1440p, 4k, etc) while still utilizing the game assets built for 480x270? If so, can anyone post an example of how this can be accomplished? Any tips or pointers would also be appreciated.
You could make a dummy surface at the design resolution and scale it to the resolution the game is running at:
window = pygame.set_mode([user_x, user_y])
w = pygame.Surface([design_x, design_y])
def draw():
frame = pygame.transform.scale(w, (user_x, user_y))
window.blit(frame, frame.get_rect())
pygame.display.flip()
Draw everything on the dummy surface and it will be scaled to the screen. This requires a fixed screen height to screen width ratio.
Spent some time tinkering and put together a demo showcasing how you could go about tackling this problem. Find yourself an image for testing and put the string path to that image in the script for the value of imagePath.
The functionality of this script is simple. As you hit either left or right on the arrow keys, the screen resolution cycles through a tuple of acceptable resolutions and the screen resizes accordingly while scaling your test image to the new resolution.
import pygame,sys
from pygame import *
from pygame.locals import *
displayIndex = 0
pygame.init()
##standard 16:9 display ratios
DISPLAYS = [(1024,576),(1152,648),(1280,720),(1600,900),(1920,1080),(2560,1440)]
screen = pygame.display.set_mode(DISPLAYS[displayIndex])
screen.fill((0,0,0))
### change image path to a string that names an image you'd like to load for testing. I just used a smiley face from google image search.
### Put it in the same place as this file or set up your paths appropriately
imagePath = "Smiley-icon.png"
class Icon(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.smileyImage = pygame.image.load(imagePath)
self.image = self.smileyImage.convert_alpha()
### need to assume a default scale, DISPLAYS[0] will be default for us
self.rect = self.image.get_rect()
self.posX = x
self.posY = y
self.rect.x = x
self.rect.y = y
self.defaultx = (float(self.rect[2])/DISPLAYS[0][0])*100
self.defaulty = (float(self.rect[3])/DISPLAYS[0][1])*100
## this is the percent of the screen that the image should take up in the x and y planes
def updateSize(self,):
self.image = ImageRescaler(self.smileyImage,(self.defaultx,self.defaulty))
self.rect = self.image.get_rect()
self.rect.x = self.posX
self.rect.y = self.posY
def ImageRescaler(image,originalScaleTuple): #be sure to restrict to only proper ratios
newImage = pygame.transform.scale(image,(int(DISPLAYS[displayIndex][0]*(originalScaleTuple[0]/100)),
int(DISPLAYS[displayIndex][1]*(originalScaleTuple[1]/100))))
return newImage
def resizeDisplay():
screen = pygame.display.set_mode(DISPLAYS[displayIndex])
## this is where you'd have'd probably want your sprite groups set to resize themselves
## Just gonna call it on icon here
icon.updateSize()
icon = Icon(100,100)
while True:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
displayIndex -=1
if displayIndex < 0:
displayIndex = 5
resizeDisplay()
elif event.key == K_RIGHT:
displayIndex+=1
if displayIndex > 5:
displayIndex = 0
resizeDisplay()
screen.blit(icon.image,(icon.rect.x,icon.rect.y))
pygame.display.update()
The best way to go about this is by downscaling images, in order to preserve image quality. Here are two options:
Option 1
This method is probably the fastest.
Create images to be compatible with the largest resolution you are intending to support.
Create a screen with the user's desired size.
screen = pygame.display.set_mode((user_x, user_y))
Downscale images at load time
image = pygame.image.load("a.png").convert_alpha()
pygame.transform.scale(image, (screen.width() / your_max_width, screen.height() / your_max_height), DestSurface=image)
Now, just blit them normally. It should run at normal speeds.
Option 2
Create a screen with the user's desired size.
screen = pygame.display.set_mode((user_x, user_y))
Next, create a pygame.Surface with the highest resolution you are intending to support.
surface = pygame.Surface((1920, 1080))
Then, blit everything to that surface.
surface.blit(image, rect)
# Etc...
After all blits are completed, downscale the surface (as necessary) to whatever resolution the user desires. Preferably, you would only allow resolutions with the same aspect ratio.
pygame.transform.scale(surface, ((screen.width() / your_max_width, screen.height() / your_max_height), DestSurface=surface)
Finally, blit the surface to the screen, and display it.
screen.blit(surface, (0, 0))
pygame.display.update()
This process (downscaling) allows you to preserve image quality while still allowing the user to choose their screen resolution. It will be slower because you are constantly manipulating images.
I am trying to make a tic-tac-toe game with pygame. An important thing I want is being able to make my images (eg. X and O) slightly translucent for when my user is only hovering over a grid tile. I also use opacity to visually show whose turn it is.
This is what I have tried:
x_tile = pygame.image.load('x_tile').convert()
x_tile.set_alpha(100)
This works fine when I'm blitting x_tile directly onto the display like this:
# This is for simplicity's sake. The actual blit process is all being done in an infinite loop
screen = pygame.display.set_mode((300, 300))
screen.blit(x_file, x_file.get_rect())
But my game is using another image that represents the grid, and that is what I'm blitting onto. So I'm blitting this board onto the display, then blitting the actual X and O tiles on the board.
screen = pygame.display.set_mode((300, 300))
screen.blit(board, board_rect)
board.blit(x_tile, x_tile.get_rect(center=grid[0].center)) # I have a list of Rects that make a grid on the board image. grid[0] is the top left
When I do it that way, x_tile.set_alpha(100) seems to have no effect and I don't know what to do.
Edit: I am using pygame 2.0.1. I'm on Windows 10.
Here is the entire code
import os
import pygame
from pygame.locals import *
# Game constants
WIN_SIZE = WIN_WIDTH, WIN_HEIGHT = 800, 600
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
BLUE = 0, 0, 255
# Game functions
class NoneSound:
"""dummy class for when pygame.mixer did not init
and there is no sound available"""
def play(self): pass
def load_sound(file):
"""loads a sound file, prepares it for play"""
if not pygame.mixer:
return NoneSound()
music_to_load = os.path.join('sounds', file)
try:
sound = pygame.mixer.Sound(music_to_load)
except pygame.error as message:
print('Cannot load following sound:', music_to_load)
raise SystemExit(message)
return sound
def load_image(file, colorkey=None, size=None):
"""loads image into game"""
image_to_load = os.path.join('images', file)
try:
image = pygame.image.load(image_to_load).convert()
except pygame.error as message:
print('Cannot load following image:', image_to_load)
raise SystemExit(message)
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
if size is not None:
image = pygame.transform.scale(image, size)
return image
# Game class
class TTTVisual:
"""Controls game visuals"""
def __init__(self, win: pygame.Surface):
self.win = win
# Load in game images
self.board = load_image('board.png', size=(600, 450), colorkey=WHITE)
self.x_tile = load_image('X_tile.png', size=(100, 100), colorkey=BLACK)
self.o_tile = load_image('O_tile.png', size=(100, 100), colorkey=BLACK)
# Translucent for disabled looking tile
self.x_tile_trans = self.x_tile.copy()
self.o_tile_trans = self.o_tile.copy()
self.x_tile_trans.set_alpha(100)
self.o_tile_trans.set_alpha(100)
# Used to let user know whose turn it is
self.x_turn = pygame.transform.scale(self.x_tile, (50, 50))
self.o_turn = pygame.transform.scale(self.o_tile, (50, 50))
self.x_turn_trans = pygame.transform.scale(self.x_tile_trans, (50, 50))
self.o_turn_trans = pygame.transform.scale(self.o_tile_trans, (50, 50))
self.get_rects()
self.grid = self.setup_grid()
def get_rects(self):
"""Creates coords for some visual game assets"""
self.board_rect = self.board.get_rect(
center=self.win.get_rect().center)
self.x_turn_rect = self.x_turn.get_rect(top=10, left=10)
self.o_turn_rect = self.o_turn.get_rect(top=10, left=WIN_WIDTH-60)
def setup_grid(self):
grid = []
left = 0
top = 150
row = 0
for i in range(9):
if (i != 0) and (i % 3 == 0):
row += 1
left = 0
grid.append(pygame.Rect(left, row*top, 200, 150))
left += 200
return grid
def update_turn_status(self):
"""Updates the X and O tiles on the top left and right to
let user know whose turn it is"""
self.win.blits((
(self.x_turn_trans, self.x_turn_rect),
(self.o_turn, self.o_turn_rect)
))
def update_grid(self):
"""Updates board"""
self.win.blit(self.board, self.board_rect)
# Here is where you could change board to win and see that the tile changes in opacity
self.board.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=self.grid[0].center))
def update(self):
self.win.fill(WHITE)
self.update_turn_status()
self.update_grid()
pygame.display.flip()
def main():
pygame.init()
win = pygame.display.set_mode(WIN_SIZE)
tttvisual = TTTVisual(win)
tttfunc = TTTFunc(tttvisual)
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
running = False
tttvisual.update()
pygame.quit()
if __name__ == "__main__":
main()
The issue is caused by the line:
self.board.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=self.grid[0].center))
You don't blit the image on the display Surface, but on the self.board Surface. When a Surface is blit, it is blended with the target. When you draw on a Surface, it changes permanently. Since you do that over and over again, in every frame, the source Surface appears to by opaque. When you decrease the alpha value (e.g. self.x_tile_trans.set_alpha(5)), a fade in effect will appear.
Never draw on an image Surface. Always draw on the display Surface. Cleat the display at begin of a frame. Draw the entire scene in each frame and update the display once at the end of the frame.
class TTTVisual:
# [...]
def update_grid(self):
"""Updates board"""
self.win.blit(self.board, self.board_rect)
# Here is where you could change board to win and see that the tile changes in opacity
x, y = self.grid[0].center
x += self.board_rect.x
y += self.board_rect.y
self.win.blit(self.x_tile_trans, self.x_tile_trans.get_rect(center=(x, y)))
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
I have a little problem when trying to learn about sprites in Pygame.
To begin with, i have already put a great amount of effort into this with sprites but i dont get it to work.
I have done exactly what guides on the internet are showing when drawing an object onto a sprite but object doesnt show up while running program.
Any suggestions would be more than welcome!
tick = 0
sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)
rect = sprite.get_rect()
rect.x = 400
rect.y = 300
while tick < 100:
screen.fill(black)
pygame.draw.rect(sprite, red, [rect.x, rect.y, 20, 20])
pygame.display.update()
clock.tick(10)
tick += 1
pygame.quit()
quit()
So, there are two major problems with your code.
First, you are filling the surface you are calling sprite with "white" and then just setting "white" to be treated as a transparent color for that surface, with the call to set_colorkey. So, nothing could show up anyway.
Second, you are drawing your rectangle on the Surface itself, not on your screen. So, you do suceed in painting it red, but you don't "stamp" it on the screen - you call pygame.draw.rect to draw on the Surface you are calling sprite itself, not on the screen.
Your code does not show you declaring a screen at all - I suppose you did that correctly, nonetheless keep in mind you always should post a complete example that will get the behavior you get there. If there is an error in your code to set up the screen, I can't know about it. Also, I've changed our call to draw.rect to call screen.blit : that will stamp whatever image is currently on the sprite on the surface owner of the blit method - in this case, the screen itself:
import pygame
screen = pygame.display.set_mode((800,600))
white = 255, 255, 255
tick = 0
sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)
rect = sprite.get_rect()
rect.x = 400
rect.y = 300
while tick < 100:
screen.fill(black)
screen.blit(sprite, (rect.x, rect.y))
pygame.display.update()
clock.tick(10)
tick += 1
pygame.quit()
quit()
With that you should see a white rectangle shortly - from them on you can start improving your code to delay the program ending, or wait an user event to close the screen and get things rolling.
By the way, in Pygame talk, a Surface is not quite considered a "sprite" - a Sprite is a special class meant to be used as members of "groups" upon which a series of helper functions will work when you start organizing a more complete game or animated sequence.
Pygame documents is a good friend.
1.Why would you fill sprite white and then set its color key to white? This will just make the sprite transparent. See set_colorkey.
sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)
If you need a red sprite, just create one and fill it red.
sprite = pygame.Surface((20, 20))
sprite.fill(red)
2.What pygame.draw.rect do is just drawing a rectangular shape on the Surface. So if you want to draw a red rect on the screen, just
pygame.draw.rect(screen, red, (0, 0, 20, 20))
Or if you want to show the sprite on the screen, which is usually more effective, use blit
screen.blit(sprite, (0, 0))
Have fun with pygame :)
If you want to start doing some programming with Pygame directly using sprites, it would be a good idea to do some more preliminary study of available tutorials to get some basic understanding how applications using GUIs and capable of handling user events work in general.
Another option is to start with easier things where it is possible to see the result directly without the need of writing the necessary (sprites need Groups and a class) voluminous preliminary code.
There are beside a huge amount of hard to understand tutorials on Pygame sprites also some which provide nice examples of code and quite good explanations. I suggest you take a closer look at:
Introduction to sprites --- sprite_collect_blocks.py
and then come back here with further questions, if any.
Below I have provided two pieces of code, so that you can compare them to each other to see what I have been speaking above about.
The first piece of the two is your own code with slight modifications. It creates three blocks on the screen and what it actually does has nothing to do with sprites (as it is also the case in the another answer), but shows how to populate the displayed Pygame screen with rectangles. Read carefully also the another answers as they provide good explanations of the issues your own code comes with.
import pygame
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
BLUE = (0,0,255)
# Set the height and width of the screen:
DISPLAY_width = 640
DISPLAY_height = 480
DISPLAY = pygame.display.set_mode([DISPLAY_width, DISPLAY_height])
DISPLAY.fill(WHITE)
# See here: [(3) stackoverflow.com/.../pygame-drawing-a-rectangle][3]
pygame.draw.rect(DISPLAY,BLACK,(120,40,50,50))
pygame.draw.rect(DISPLAY,RED ,(520,355,50,50))
pygame.draw.rect(DISPLAY,BLUE ,(320,240,50,50))
pygame.display.flip() # without .flip() you get only black screen
pygame.display.init() # IMPORTANT !
clock = pygame.time.Clock()
tick = 0
while tick < 100:
clock.tick(10)
tick += 1
# BETTER do it as follows:
"""
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
"""
pygame.quit()
import sys
sys.exit()
Here a screenshot of what the code above creates:
The other piece of code is actual code using sprites. Sprites are usually used together with images. This makes it necessary to get some images in the scripts directory first:
And here goes the second piece of code (with TRUE sprites) which produces much MOTION on the screen (here only a static screenshot). All of the dots are in movement with different speeds filling the initially black screen with yellow color:
strPythonScriptHeader = """
# pygCodeSample_UsageOfGroupsAndSprites.py updated: 2017-04-12 09:17
# Copyright (c) 2005, Claudio at stackoverflow.com created: 2005-01-24 19:43
"""
# #############################################################################
# VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
def pygCodeSample_UsageOfGroupsAndSprites():
strMainInfo = """/*
WATCH OUT using pygame.sprite.Sprite and pygame.sprite.RenderClear classes:
- the order of Sprites in a Group is not necessary the order in which the
Sprites are drawn to the screen.
- the .clear() method of a Group clears the entire image square, not as
expected only the non-transparent part of it.
*/"""
print( strMainInfo )
strSpriteTutorial = """/*
The 'pygame.sprite' library provides classes representing images and
group of images which are to be drawn to the screen during the game.
The concept of a 'Sprite' is to provide a class which objects represent
real-world physical objects which can be viewed on the computer screen.
The Sprite class is not used directly. It is used to derive own classes
of customized Sprites from it:
class CustomizedSpriteDerivedFromSpriteClass(pygame.sprite.Sprite): ...
Within the definition of the CustomizedSpriteDerivedFromSpriteClass
it is necessary to define a member variable
.image (class CustomizedSpriteDerivedFromSpriteClass)
storing an 'Image' object which is to draw to the screen and the
member variable
.rect (class CustomizedSpriteDerivedFromSpriteClass)
storing a 'Rect' object holding the target position of the 'Image'
on the screen.
Usually both variables are set when running the
.__init__() (class CustomizedSpriteDerivedFromSpriteClass)
function which should contain as first command:
pygame.sprite.Sprite.__init__( self )
Further is it necessary to define also an
.update() (class CustomizedSpriteDerivedFromSpriteClass)
function which provides the code of rules telling how the by the Sprite
graphically represented physical objects should change when the .update()
function is executed.
The main purpose of a 'Sprite' is to be added to a container(called Group)
which is an object of the pygame.sprite.RenderClear class:
pGobjGroupOfSprites = pygame.sprite.RenderClear()
ready to hold any number of Sprite objects. A single Sprite is added to it
using
pGobjGroupOfSprites.add(pGobjCustomizedSpriteDerivedFromSpriteClass)
call.
The pGobjGroupOfSprites object of the pygame.sprite.RenderClear class is
equipped with following methods:
pGobjGroupOfSprites.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground)
pGobjGroupOfSprites.update()
pGobjGroupOfSprites.draw( pGobjSCREENsurface)
.draw() draws all Sprites to as parameter passed pGobjSCREENsurface
using the .image and .rect properties defined as member variables
within the CustomizedSpriteDerivedFromSpriteClass class.
.clear() draws those parts of the pGobjImage_SCREENsurfaceBackground image
to the pGobjSCREENsurface which are the Rects representing the
positions of the Sprites on the screen using the .rect property of Sprites.
.update() runs the .update() method of each Sprite stored in the GroupOfSprites.
Finally the main game loop looks like:
while True:
evaluateInputEvents()
pGobjGroupOfSprites_Hero.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground)
pGobjGroupOfSprites_Hero.update()
pGobjGroupOfSprites_Hero.draw( pGobjSCREENsurface)
pGobjGroupOfSprites_Enemy.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground)
pGobjGroupOfSprites_Enemy.update()
pGobjGroupOfSprites_Enemy.draw( pGobjSCREENsurface)
pGobjGroupOfSprites_Widgets.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground)
pGobjGroupOfSprites_Widgets.update()
pGobjGroupOfSprites_Widgets.draw( pGobjSCREENsurface)
adjustFrameRateTo(intTargetFrameRate) # time.sleep()/time.clock()
pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface # == pygame.display.flip
#:while
How many and which Groups should be used and how to spread all the Sprites
used in the game over the defined Groups is part of the architecture of
the game code which is designed according to the needs of the game and the
individual programming style.
*/"""
print( strSpriteTutorial )
mPGvDctRectOfSCREENsurface = { 'left' : 0, 'right' : 640, 'top' : 0, 'bottom' : 480 }
mPGvIntScreenOriginX = mPGvDctRectOfSCREENsurface['left']
mPGvIntScreenOriginY = mPGvDctRectOfSCREENsurface['top']
mPGvIntScreenWidth = mPGvDctRectOfSCREENsurface['right'] - mPGvDctRectOfSCREENsurface['left']
mPGvIntScreenHeight = mPGvDctRectOfSCREENsurface['bottom'] - mPGvDctRectOfSCREENsurface['top']
mPGvTplScreenSize = (mPGvIntScreenWidth, mPGvIntScreenHeight)
import pygame
pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface = pygame.display.flip
class pGobjCustomizedSprite_Dot( pygame.sprite.Sprite ):
def __init__( self, strColorOfDot, tplVelocity ):
# Intialize the Sprite class:
pygame.sprite.Sprite.__init__( self )
# .image property is used by the pGobjGroup hosting the sprite, so it is
# necessary to set it here in order to make this Sprite useful:
self.image = pygame.image.load(
r'dot-32x32_'+strColorOfDot+'.gif'
) # picture shows a coloured dot and has a transparent background
# .rect property is used by the pGobjGroup hosting the sprite, so it is
# necessary to set it here in order to make this Sprite useful:
self.rect = self.image.get_rect()
self.rect.centerx = 320
self.rect.centery = 240
# definition of other properties not necessary for usage of this class
# as a Sprite, but useful for internal purposes of setting the position
# of the rectangle within the .update() method:
self.x_velocity = tplVelocity[0]
self.y_velocity = tplVelocity[1]
#:def
# .update() method is used by the pGobjGroup hosting the sprite in its
# .update() command, so it is necessary to define it here in order to
# make this Sprite useful:
def update( self ):
self.rect.move_ip( (self.x_velocity, self.y_velocity) )
if(self.rect.left <= 0 or self.rect.right >= mPGvIntScreenWidth):
self.x_velocity = -(self.x_velocity)
#:if
if self.rect.top <= 0 or self.rect.bottom >= mPGvIntScreenHeight:
self.y_velocity = -(self.y_velocity)
#:if
#:def
#:class
pygame.init()
# pGobjSCREENsurface = pygame.display.set_mode(mPGvTplScreenSize, pygame.constants.DOUBLEBUF)
pGobjSCREENsurface = pygame.display.set_mode(mPGvTplScreenSize) # faster than with DOUBLEBUF
pGobjGroupOfSprites_spriteDot = pygame.sprite.RenderClear()
pGobjImage_SCREENsurfaceBackground = pygame.image.load(
r'rect-640x480_yellow.bmp'
) # picture shows yellow background
intLstLength = 0
dctTplVelocity = {}
import random
while(intLstLength < 5):
tplVelocity = (random.randint( 1, 5 ), random.randint( 1, 5 ))
if(tplVelocity in dctTplVelocity): pass
else:
intLstLength+=1
dctTplVelocity[tplVelocity] = intLstLength
#:if/else
#:while
lstTplVelocity = list(dctTplVelocity.keys())
# pGspriteRedDot = pGobjCustomizedSprite_Dot('red' ,lstTplVelocity[0])
# pGspriteWhiteDot = pGobjCustomizedSprite_Dot('purple',lstTplVelocity[1])
# pGspriteGreyDot = pGobjCustomizedSprite_Dot('grey' ,lstTplVelocity[2])
# pGspriteBlueDot = pGobjCustomizedSprite_Dot('blue' ,lstTplVelocity[3])
# pGspriteOrangeDot = pGobjCustomizedSprite_Dot('orange',lstTplVelocity[4])
pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('red' ,lstTplVelocity[0]))
pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('purple',lstTplVelocity[1]))
pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('grey' ,lstTplVelocity[2]))
pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('blue' ,lstTplVelocity[3]))
pGobjGroupOfSprites_spriteDot.add(pGobjCustomizedSprite_Dot('orange',lstTplVelocity[4]))
print
print( ' target frame rate [frames/second] == 100.0 : ' )
intLoopCounter = 0
import time
clock = time.clock
sleep = time.sleep
import sys
fltTime=clock()
fltStartTime = clock()
blnExitMainGameLoop = False
fltFrameRate = 100.0
fltTimeToNextFrame = 1.0 / fltFrameRate
import time
while True:
time.sleep(0.03)
# ---
# -----------------------------------------------------
# Processing of user input:
pGlstObjEvent = pygame.event.get()
for pGobjEvent in pGlstObjEvent:
if pGobjEvent.type == pygame.constants.QUIT:
blnExitMainGameLoop = True
#:if
if pGobjEvent.type == pygame.constants.KEYDOWN:
if pGobjEvent.key == pygame.constants.K_ESCAPE:
blnExitMainGameLoop = True
if pGobjEvent.key == pygame.constants.K_s:
time.sleep(21)
blnExitMainGameLoop = False
#:if
#:if
#:for
if(blnExitMainGameLoop):
pygame.display.quit()
break
#:if
# ---
# -----------------------------------------------------
# output of texts with infos to console window:
if(intLoopCounter%100 == 99):
print( ' %5.2f '%(fltFrameRate/(clock()-fltTime),), )
fltTime=clock()
#:if
intLoopCounter += 1
# print intLoopCounter,
# ---
# -----------------------------------------------------
# preparing and drawing graphic output to screen:
pGobjGroupOfSprites_spriteDot.update()
pGobjGroupOfSprites_spriteDot.clear(pGobjSCREENsurface, pGobjImage_SCREENsurfaceBackground)
pGobjGroupOfSprites_spriteDot.draw( pGobjSCREENsurface)
# ---
# -----------------------------------------------------
# adjusting frame rate to 100 frames/second:
# fltFrameRate = 100.0
# fltTimeToNextFrame = 1.0 / fltFrameRate
fltTargetTime = fltTimeToNextFrame*intLoopCounter
fltTimeDiff = fltTargetTime-(clock()-fltStartTime)
if(fltTimeDiff > 0.8*fltTimeToNextFrame): sleep(0.8*fltTimeToNextFrame)
fltTimeDiff = fltTargetTime-(clock()-fltStartTime)
if(fltTimeDiff > 0.1*fltTimeToNextFrame): sleep(0.1*fltTimeToNextFrame)
fltTimeDiff = fltTargetTime-(clock()-fltStartTime)
while(fltTimeDiff > 0):
fltTimeDiff = (fltTimeToNextFrame*intLoopCounter)-(clock()-fltStartTime)
#:while
# ---
# -----------------------------------------------------
# displaying prepared graphic output:
pGdefUpdateScreenWithDataStoredIn_pGobjSCREENsurface()
#:while
#:def pygCodeSample_UsageOfGroupsAndSprites()
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if __name__ == '__main__':
print( strPythonScriptHeader )
pygCodeSample_UsageOfGroupsAndSprites()
import sys
if int(sys.version[0]) < 3 : # Python 2 :
raw_input('(exit with ENTER) #> OK? ')
sys.exit()
else: # Python 3 :
input('(exit with ENTER) #> OK? ')
sys.exit()
#:if
pygameConstantsForUsageInKeyEVENTS = ['ACTIVEEVENT', 'ANYFORMAT', 'ASYNCBLIT', 'AUDIO_S16', 'AUDIO_S16LSB', 'AUDIO_S16MSB', 'AUDIO_S16SYS', 'AUDIO_S8', 'AUDIO_U16', 'AUDIO_U16LSB', 'AUDIO_U16MSB', 'AUDIO_U16SYS', 'AUDIO_U8', 'BIG_ENDIAN', 'BLEND_ADD', 'BLEND_MAX', 'BLEND_MIN', 'BLEND_MULT', 'BLEND_PREMULTIPLIED', 'BLEND_RGBA_ADD', 'BLEND_RGBA_MAX', 'BLEND_RGBA_MIN', 'BLEND_RGBA_MULT', 'BLEND_RGBA_SUB', 'BLEND_RGB_ADD', 'BLEND_RGB_MAX', 'BLEND_RGB_MIN', 'BLEND_RGB_MULT', 'BLEND_RGB_SUB', 'BLEND_SUB', 'BUTTON_X1', 'BUTTON_X2', 'DOUBLEBUF', 'FULLSCREEN', 'GL_ACCELERATED_VISUAL', 'GL_ACCUM_ALPHA_SIZE', 'GL_ACCUM_BLUE_SIZE', 'GL_ACCUM_GREEN_SIZE', 'GL_ACCUM_RED_SIZE', 'GL_ALPHA_SIZE', 'GL_BLUE_SIZE', 'GL_BUFFER_SIZE', 'GL_DEPTH_SIZE', 'GL_DOUBLEBUFFER', 'GL_GREEN_SIZE', 'GL_MULTISAMPLEBUFFERS', 'GL_MULTISAMPLESAMPLES', 'GL_RED_SIZE', 'GL_STENCIL_SIZE', 'GL_STEREO', 'GL_SWAP_CONTROL', 'HAT_CENTERED', 'HAT_DOWN', 'HAT_LEFT', 'HAT_LEFTDOWN', 'HAT_LEFTUP', 'HAT_RIGHT', 'HAT_RIGHTDOWN', 'HAT_RIGHTUP', 'HAT_UP', 'HWACCEL', 'HWPALETTE', 'HWSURFACE', 'IYUV_OVERLAY', 'JOYAXISMOTION', 'JOYBALLMOTION', 'JOYBUTTONDOWN', 'JOYBUTTONUP', 'JOYHATMOTION', 'KEYDOWN', 'KEYUP', 'KMOD_ALT', 'KMOD_CAPS', 'KMOD_CTRL', 'KMOD_LALT', 'KMOD_LCTRL', 'KMOD_LMETA', 'KMOD_LSHIFT', 'KMOD_META', 'KMOD_MODE', 'KMOD_NONE', 'KMOD_NUM', 'KMOD_RALT', 'KMOD_RCTRL', 'KMOD_RMETA', 'KMOD_RSHIFT', 'KMOD_SHIFT', 'K_0', 'K_1', 'K_2', 'K_3', 'K_4', 'K_5', 'K_6', 'K_7', 'K_8', 'K_9', 'K_AMPERSAND', 'K_ASTERISK', 'K_AT', 'K_BACKQUOTE', 'K_BACKSLASH', 'K_BACKSPACE', 'K_BREAK', 'K_CAPSLOCK', 'K_CARET', 'K_CLEAR', 'K_COLON', 'K_COMMA', 'K_DELETE', 'K_DOLLAR', 'K_DOWN', 'K_END', 'K_EQUALS', 'K_ESCAPE', 'K_EURO', 'K_EXCLAIM', 'K_F1', 'K_F10', 'K_F11', 'K_F12', 'K_F13', 'K_F14', 'K_F15', 'K_F2', 'K_F3', 'K_F4', 'K_F5', 'K_F6', 'K_F7', 'K_F8', 'K_F9', 'K_FIRST', 'K_GREATER', 'K_HASH', 'K_HELP', 'K_HOME', 'K_INSERT', 'K_KP0', 'K_KP1', 'K_KP2', 'K_KP3', 'K_KP4', 'K_KP5', 'K_KP6', 'K_KP7', 'K_KP8', 'K_KP9', 'K_KP_DIVIDE', 'K_KP_ENTER', 'K_KP_EQUALS', 'K_KP_MINUS', 'K_KP_MULTIPLY', 'K_KP_PERIOD', 'K_KP_PLUS', 'K_LALT', 'K_LAST', 'K_LCTRL', 'K_LEFT', 'K_LEFTBRACKET', 'K_LEFTPAREN', 'K_LESS', 'K_LMETA', 'K_LSHIFT', 'K_LSUPER', 'K_MENU', 'K_MINUS', 'K_MODE', 'K_NUMLOCK', 'K_PAGEDOWN', 'K_PAGEUP', 'K_PAUSE', 'K_PERIOD', 'K_PLUS', 'K_POWER', 'K_PRINT', 'K_QUESTION', 'K_QUOTE', 'K_QUOTEDBL', 'K_RALT', 'K_RCTRL', 'K_RETURN', 'K_RIGHT', 'K_RIGHTBRACKET', 'K_RIGHTPAREN', 'K_RMETA', 'K_RSHIFT', 'K_RSUPER', 'K_SCROLLOCK', 'K_SEMICOLON', 'K_SLASH', 'K_SPACE', 'K_SYSREQ', 'K_TAB', 'K_UNDERSCORE', 'K_UNKNOWN', 'K_UP', 'K_a', 'K_b', 'K_c', 'K_d', 'K_e', 'K_f', 'K_g', 'K_h', 'K_i', 'K_j', 'K_k', 'K_l', 'K_m', 'K_n', 'K_o', 'K_p', 'K_q', 'K_r', 'K_s', 'K_t', 'K_u', 'K_v', 'K_w', 'K_x', 'K_y', 'K_z', 'LIL_ENDIAN', 'MOUSEBUTTONDOWN', 'MOUSEBUTTONUP', 'MOUSEMOTION', 'NOEVENT', 'NOFRAME', 'NUMEVENTS', 'OPENGL', 'OPENGLBLIT', 'PREALLOC', 'QUIT', 'RESIZABLE', 'RLEACCEL', 'RLEACCELOK', 'SCRAP_BMP', 'SCRAP_CLIPBOARD', 'SCRAP_PBM', 'SCRAP_PPM', 'SCRAP_SELECTION', 'SCRAP_TEXT', 'SRCALPHA', 'SRCCOLORKEY', 'SWSURFACE', 'SYSWMEVENT', 'TIMER_RESOLUTION', 'USEREVENT', 'USEREVENT_DROPFILE', 'UYVY_OVERLAY', 'VIDEOEXPOSE', 'VIDEORESIZE', 'YUY2_OVERLAY', 'YV12_OVERLAY', 'YVYU_OVERLAY', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
The code above is equipped with voluminous comments. As I have written it years and years ago starting on pygame and trying to understand sprites, the goal was to write it so clear that it will be easy to understand. Now you can judge yourself how successful I was with my style of self-explanatory variable naming and the provided explanations and informations.