bro
This commit is contained in:
1 parent
de41a02749
commit
9af09cebd3
764 files changed
+54218
-78859
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
"""pygame.examples.testsprite
|
||||
""" pg.examples.testsprite
|
||||
|
||||
Like the testsprite.c that comes with libsdl, this pygame version shows
|
||||
lots of sprites moving around.
|
||||
@@ -7,15 +7,16 @@ lots of sprites moving around.
|
||||
It is an abomination of ugly code, and mostly used for testing.
|
||||
|
||||
|
||||
See pygame.examples.aliens for some prettier code.
|
||||
See pg.examples.aliens for some prettyier code.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import os
|
||||
|
||||
from random import randint
|
||||
from time import time
|
||||
from typing import List
|
||||
|
||||
import pygame
|
||||
import pygame as pg
|
||||
|
||||
main_dir = os.path.split(os.path.abspath(__file__))[0]
|
||||
data_dir = os.path.join(main_dir, "data")
|
||||
@@ -42,18 +43,22 @@ if "-layered_dirty" in sys.argv:
|
||||
|
||||
flags = 0
|
||||
if "-flip" in sys.argv:
|
||||
flags ^= pygame.DOUBLEBUF
|
||||
flags ^= pg.DOUBLEBUF
|
||||
|
||||
if "-fullscreen" in sys.argv:
|
||||
flags ^= pygame.FULLSCREEN
|
||||
flags ^= pg.FULLSCREEN
|
||||
|
||||
if "-sw" in sys.argv:
|
||||
flags ^= pygame.SWSURFACE
|
||||
flags ^= pg.SWSURFACE
|
||||
|
||||
use_rle = True
|
||||
|
||||
if "-hw" in sys.argv:
|
||||
flags ^= pg.HWSURFACE
|
||||
use_rle = False
|
||||
|
||||
if "-scaled" in sys.argv:
|
||||
flags ^= pygame.SCALED
|
||||
flags ^= pg.SCALED
|
||||
|
||||
screen_dims = [640, 480]
|
||||
|
||||
@@ -65,20 +70,37 @@ if "-width" in sys.argv:
|
||||
i = sys.argv.index("-width")
|
||||
screen_dims[0] = int(sys.argv[i + 1])
|
||||
|
||||
if "-alpha" in sys.argv:
|
||||
use_alpha = True
|
||||
else:
|
||||
use_alpha = False
|
||||
use_alpha = "-alpha" in sys.argv
|
||||
|
||||
print(screen_dims)
|
||||
|
||||
|
||||
class Thingy(pygame.sprite.DirtySprite):
|
||||
images = None
|
||||
##class Thingy(pg.sprite.Sprite):
|
||||
## images = None
|
||||
## def __init__(self):
|
||||
## pg.sprite.Sprite.__init__(self)
|
||||
## self.image = Thingy.images[0]
|
||||
## self.rect = self.image.get_rect()
|
||||
## self.rect.x = randint(0, screen_dims[0])
|
||||
## self.rect.y = randint(0, screen_dims[1])
|
||||
## #self.vel = [randint(-10, 10), randint(-10, 10)]
|
||||
## self.vel = [randint(-1, 1), randint(-1, 1)]
|
||||
##
|
||||
## def move(self):
|
||||
## for i in [0, 1]:
|
||||
## nv = self.rect[i] + self.vel[i]
|
||||
## if nv >= screen_dims[i] or nv < 0:
|
||||
## self.vel[i] = -self.vel[i]
|
||||
## nv = self.rect[i] + self.vel[i]
|
||||
## self.rect[i] = nv
|
||||
|
||||
|
||||
class Thingy(pg.sprite.DirtySprite):
|
||||
images: List[pg.Surface] = []
|
||||
|
||||
def __init__(self):
|
||||
## pygame.sprite.Sprite.__init__(self)
|
||||
pygame.sprite.DirtySprite.__init__(self)
|
||||
## pg.sprite.Sprite.__init__(self)
|
||||
pg.sprite.DirtySprite.__init__(self)
|
||||
self.image = Thingy.images[0]
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.x = randint(0, screen_dims[0])
|
||||
@@ -87,7 +109,7 @@ class Thingy(pygame.sprite.DirtySprite):
|
||||
self.vel = [randint(-1, 1), randint(-1, 1)]
|
||||
self.dirty = 2
|
||||
|
||||
def update(self):
|
||||
def update(self, *args, **kwargs):
|
||||
for i in [0, 1]:
|
||||
nv = self.rect[i] + self.vel[i]
|
||||
if nv >= screen_dims[i] or nv < 0:
|
||||
@@ -96,22 +118,22 @@ class Thingy(pygame.sprite.DirtySprite):
|
||||
self.rect[i] = nv
|
||||
|
||||
|
||||
class Static(pygame.sprite.DirtySprite):
|
||||
images = None
|
||||
class Static(pg.sprite.DirtySprite):
|
||||
images: List[pg.Surface] = []
|
||||
|
||||
def __init__(self):
|
||||
pygame.sprite.DirtySprite.__init__(self)
|
||||
pg.sprite.DirtySprite.__init__(self)
|
||||
self.image = Static.images[0]
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.x = randint(0, int(3 * screen_dims[0] / 4))
|
||||
self.rect.y = randint(0, int(3 * screen_dims[1] / 4))
|
||||
self.rect.x = randint(0, 3 * screen_dims[0] // 4)
|
||||
self.rect.y = randint(0, 3 * screen_dims[1] // 4)
|
||||
|
||||
|
||||
def main(
|
||||
update_rects=True,
|
||||
use_static=False,
|
||||
use_layered_dirty=False,
|
||||
screen_dims=[640, 480],
|
||||
screen_dims=(640, 480),
|
||||
use_alpha=False,
|
||||
flags=0,
|
||||
):
|
||||
@@ -130,35 +152,31 @@ def main(
|
||||
if use_layered_dirty:
|
||||
update_rects = True
|
||||
|
||||
pygame.init() # needed to initialise time module for get_ticks()
|
||||
pygame.display.init()
|
||||
pg.init() # needed to initialise time module for get_ticks()
|
||||
pg.display.init()
|
||||
|
||||
# if "-fast" in sys.argv:
|
||||
|
||||
screen = pygame.display.set_mode(screen_dims, flags, vsync="-vsync" in sys.argv)
|
||||
screen = pg.display.set_mode(screen_dims, flags, vsync="-vsync" in sys.argv)
|
||||
|
||||
# this is mainly for GP2X, so it can quit.
|
||||
pygame.joystick.init()
|
||||
num_joysticks = pygame.joystick.get_count()
|
||||
pg.joystick.init()
|
||||
num_joysticks = pg.joystick.get_count()
|
||||
if num_joysticks > 0:
|
||||
stick = pygame.joystick.Joystick(0)
|
||||
stick = pg.joystick.Joystick(0)
|
||||
stick.init() # now we will receive events for the joystick
|
||||
|
||||
screen.fill([0, 0, 0])
|
||||
pygame.display.flip()
|
||||
sprite_surface = pygame.image.load(os.path.join(data_dir, "asprite.bmp"))
|
||||
sprite_surface2 = pygame.image.load(os.path.join(data_dir, "static.webp"))
|
||||
pg.display.flip()
|
||||
sprite_surface = pg.image.load(os.path.join(data_dir, "asprite.bmp"))
|
||||
sprite_surface2 = pg.image.load(os.path.join(data_dir, "static.png"))
|
||||
|
||||
if use_rle:
|
||||
sprite_surface.set_colorkey(
|
||||
[0xFF, 0xFF, 0xFF], pygame.SRCCOLORKEY | pygame.RLEACCEL
|
||||
)
|
||||
sprite_surface2.set_colorkey(
|
||||
[0xFF, 0xFF, 0xFF], pygame.SRCCOLORKEY | pygame.RLEACCEL
|
||||
)
|
||||
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY | pg.RLEACCEL)
|
||||
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY | pg.RLEACCEL)
|
||||
else:
|
||||
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], pygame.SRCCOLORKEY)
|
||||
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], pygame.SRCCOLORKEY)
|
||||
sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY)
|
||||
sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], pg.SRCCOLORKEY)
|
||||
|
||||
if use_alpha:
|
||||
sprite_surface = sprite_surface.convert_alpha()
|
||||
@@ -180,13 +198,13 @@ def main(
|
||||
numsprites = 100
|
||||
sprites = None
|
||||
if use_layered_dirty:
|
||||
## sprites = pygame.sprite.FastRenderGroup()
|
||||
sprites = pygame.sprite.LayeredDirty()
|
||||
## sprites = pg.sprite.FastRenderGroup()
|
||||
sprites = pg.sprite.LayeredDirty()
|
||||
else:
|
||||
if update_rects:
|
||||
sprites = pygame.sprite.RenderUpdates()
|
||||
sprites = pg.sprite.RenderUpdates()
|
||||
else:
|
||||
sprites = pygame.sprite.Group()
|
||||
sprites = pg.sprite.Group()
|
||||
|
||||
for i in range(0, numsprites):
|
||||
if use_static and i % 2 == 0:
|
||||
@@ -196,7 +214,7 @@ def main(
|
||||
frames = 0
|
||||
start = time()
|
||||
|
||||
background = pygame.Surface(screen.get_size())
|
||||
background = pg.Surface(screen.get_size())
|
||||
background = background.convert()
|
||||
background.fill([0, 0, 0])
|
||||
|
||||
@@ -214,23 +232,18 @@ def main(
|
||||
|
||||
rects = sprites.draw(screen)
|
||||
if update_rects:
|
||||
pygame.display.update(rects)
|
||||
pg.display.update(rects)
|
||||
else:
|
||||
pygame.display.flip()
|
||||
pg.display.flip()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type in [
|
||||
pygame.QUIT,
|
||||
pygame.KEYDOWN,
|
||||
pygame.QUIT,
|
||||
pygame.JOYBUTTONDOWN,
|
||||
]:
|
||||
for event in pg.event.get():
|
||||
if event.type in [pg.QUIT, pg.KEYDOWN, pg.QUIT, pg.JOYBUTTONDOWN]:
|
||||
going = False
|
||||
|
||||
frames += 1
|
||||
end = time()
|
||||
print(f"FPS: {frames / (end - start):f}")
|
||||
pygame.quit()
|
||||
pg.quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user