예제 #1
0
 def __init__(self):
     apple_img = sf.Image()  # Apple's image
     if not apple_img.LoadFromFile("./data/apple.png"):
         pass
         # print "Could not load data/apple.png"
     sf.Sprite.__init__(self, apple_img)
     self.SetCenter(apple_img.GetWidth() / 2, apple_img.GetHeight() / 2)
     self.size = apple_img.GetWidth()
예제 #2
0
 def __init__(self, difficulty):
     self.parts_per_frame = 1 + difficulty
     self.angle = 0
     self.direction = 0  # 0, 1 or -1 according to the key pressed
     self.rond = sf.Image()
     self.level_completed = False
     if not self.rond.LoadFromFile("./data/rond2.png"):
         pass
예제 #3
0
def load_image(filename, basedir=_base_image_dir):
    global _loaded_images
    fname = os.path.join(basedir, filename)
    if filename not in _loaded_images:
        img = sf.Image()
        if not img.LoadFromFile(fname):
            raise RuntimeError("Image %s couldn't be loaded" % fname)
        _loaded_images[fname] = img
    return _loaded_images[fname]
예제 #4
0
def create_screenshot(window, name):
    lc = time.localtime()
    f = "%s-screenshot-%04d-%02d-%02d-%02d:%02d:%02d.png" % \
        (name, lc[0], lc[1], lc[2], lc[3], lc[4], lc[5])
    i = sf.Image()
    ret = i.CopyScreen(window.window)
    assert ret
    # SFML BUG http://www.sfml-dev.org/todo/index.php?do=details&task_id=68
    # Image is y-flipped.
    ret = i.SaveToFile(f)
    assert ret

    try:
        subprocess.Popen(["convert", "-flip", f, f])
    except OSError:
        # Probably on Windows, or a unix without the convert program.
        pass
예제 #5
0
    def __init__(self, filename, magic = False):
        """Do not call this directly."""
        self.img = sf.Image()
        self.filename = filename
        if not tf.PRODUCTION:
            self.sprites = set()

        self._do_update(0)

        if not tf.PRODUCTION:
            # This makes the stat() checks spread out
            self.last_gametick_updated += \
                ((len(self.all_images)+1)
                 % self.GAMETICKS_BEFORE_UPDATE_CHECK)

        if magic is True and self.filename not in self.all_images:
            self.all_images[self.filename] = self
        else:
            raise tf.TribeFlameException(\
                "Warning, you are not using imagefile.py properly. " + \
                    "Call imagefile.new_image instead.")
예제 #6
0
    def __init__(self, filename, animation_name, image_folder, x=0, y=0):
        # file name / animation_name
        self.filename = filename.rstrip(os.path.sep)
        self.animation_name = animation_name

        # background image
        self.image = sf.Image()
        fullfilename = os.path.join(image_folder, animation_name, filename)
        if not self.image.LoadFromFile(fullfilename):
            raise RuntimeError("Image '%s' couldn't be loaded" % filename)
        width, height = self.image.GetWidth(), self.image.GetHeight()

        # background sprite
        self.sprite = sf.Sprite(self.image)
        self.sprite.SetCenter(width / 2., height / 2.)
        self.sprite.SetPosition(x, y)

        # information string
        string_repr = os.path.join(self.animation_name, self.filename)
        self.string = sf.String(string_repr)
        self.string.SetCenter(self.string.GetRect().GetWidth() / 2., 0.)
        self.string.SetPosition(x, y + height / 2.)

        # sprite outline rect
        self.rect = sfml_make_rect_from_def(x - width / 2.,
                y - height / 2., width, height)
        self.rect.EnableOutline(True)
        self.rect.EnableFill(False)
        self.rect.SetOutlineWidth(1)
        self.rect = sfml_set_rect_outline_color(self.rect, sf.Color.White)

        # offset for hitboxes
        self.offsetX, self.offsetY = x, y

        # hitboxes
        self.drawable_list = DrawableHitboxList()
예제 #7
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import threading, time, sys
import reseau.client as net
from PySFML import sf
from pdb import set_trace as rrr
from twisted.internet import reactor
import animenv, persoclient, son
import random, sys

# globaux
FREQ = 30.0
APPW, APPH = 1920, 1080

_FOND = sf.Image()
_FOND.LoadFromFile("./img/map.bmp")
FOND = sf.Sprite(_FOND)
FOND.SetPosition(0, 0)  #(APPW/2, APPH/2)
FOND.Resize(APPW, APPH)

KEYS = {
    ord('z'): 'z',
    ord('s'): 's',
    ord('q'): 'q',
    ord('d'): 'd',
    277: ' ',
}

GLOB = {}
GLOB['objets'] = {}
예제 #8
0
파일: opengl.py 프로젝트: tsurupettan/SFML
def main():

    # Create main window
    App = sf.RenderWindow(sf.VideoMode(800, 600), "SFML OpenGL")
    App.SetActive()

    # Create a sprite for the background
    BackgroundImage = sf.Image()
    if not BackgroundImage.LoadFromFile(
            "../../samples/bin/datas/opengl/background.jpg"):
        return
    Background = sf.Sprite(BackgroundImage)

    # Load an OpenGL texture.
    # We could directly use a sf.Image as an OpenGL texture (with its Bind() member function),
    # but here we want more control on it (generate mipmaps, ...) so we create a new one

    Image = sf.Image()
    if not Image.LoadFromFile("../../samples/bin/datas/opengl/texture.jpg"):
        return
    # The next line is a bit different from the C++ version
    Texture = glGenTextures(1)  # instead of glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture)
    # It is almost the same line there, except in C++, the last argument was Image.GetPixelsPtr().
    # In python, GetPixels simply returns a string.
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(),
                      Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
                      Image.GetPixels())
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR)

    # Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST)
    glDepthMask(GL_TRUE)
    glClearDepth(1.)

    # Setup a perspective projection
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90., 1., 1., 500.)

    # Bind our texture
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, Texture)
    glColor4f(1., 1., 1., 1.)

    # Create a clock for measuring the time elapsed
    Clock = sf.Clock()

    # Start game loop
    while App.IsOpened():
        # Process events
        Event = sf.Event()
        while App.GetEvent(Event):
            # Close window : exit
            if Event.Type == sf.Event.Closed:
                App.Close()

            # Escape key : exit
            if (Event.Type == sf.Event.KeyPressed) and (Event.Key.Code
                                                        == sf.Key.Escape):
                App.Close()

            # Adjust the viewport when the window is resized
            if Event.Type == sf.Event.Resized:
                glViewport(0, 0, Event.Size.Width, Event.Size.Height)

        # Draw background
        App.Draw(Background)

        # Active window to be able to perform OpenGL commands.
        App.SetActive()

        # Clear depth buffer
        glClear(GL_DEPTH_BUFFER_BIT)

        # Apply some transformations
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(0, 0, -200)
        glRotatef(Clock.GetElapsedTime() * 50, 1, 0, 0)
        glRotatef(Clock.GetElapsedTime() * 30, 0, 1, 0)
        glRotatef(Clock.GetElapsedTime() * 90, 0, 0, 1)

        # Draw a cube
        glBegin(GL_QUADS)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., -50.)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., 50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(-50., -50., 50.)

        glTexCoord2f(0, 0)
        glVertex3f(50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 1)
        glVertex3f(-50., -50., 50.)
        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(0, 0)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)

        glEnd()

        # Draw some text on top of our OpenGL object
        Text = sf.Text("This is a rotating cube")
        Text.SetPosition(230., 300.)
        Text.SetColor(sf.Color(128, 0, 128))
        App.Draw(Text)

        # Finally, display the rendered frame on screen
        App.Display()

# Don't forget to destroy our texture
    # In C++, the call to this function was a bit different
    glDeleteTextures(Texture)  # instead of glDeleteTextures(1, &Texture);

    return
예제 #9
0
    'd/dj2',
    'd/pl1',
    'd/pl2'  # decor
]
_IMG += ['n/m/{}'.format(i) for i in range(1, 11)]
_IMG += ['e/m/{}'.format(i) for i in range(1, 9)]
_IMG += ['e/c/{}'.format(i) for i in range(1, 10)]
_IMG += ['a/baf/b{}'.format(i) for i in range(1, 5)]

STATICOFS = {
    'n/b/b': (13, 18),
    'n/b/m': (0, 0),
    'e/b/1': (13, 22),
}

IMG = {nom: sf.Image() for nom in _IMG}
for img in IMG:
    IMG[img].LoadFromFile("./img/{0}.png".format(img))  # loadfromfiles

NAINSEQ = {
    A_MEURT: [
        {
            'd': 0.2,
            'i': IMG['n/m/1'],
            'o': (0, 0)
        },
        {
            'd': 0.2,
            'i': IMG['n/m/2'],
            'o': (0, 0)
        },
예제 #10
0
# Create a text.
text = sf.Text(u"Hello SFML from Python!", cheese, 50)
text.SetOrigin(text.GetRect().GetSize()[0] / 2,
               text.GetRect().GetSize()[1] / 2)
text.SetPosition(400, 300)
text.SetColor(sf.Color(0, 100, 0, 100))

# Create a text for FPS display.
fpstext = sf.Text(u"FPS: --", cheese)
fpstext.SetColor(sf.Color(0, 0, 0))
currentfps = 0
fpsclock = sf.Clock()

# Load apple image from file.
appleimage = sf.Image()
appleimage.LoadFromFile("data/apple.png")

# Create some apples with random position, speed, rotation and color.
apples = [Apple(appleimage) for num in range(0, 100)]
for apple in apples:
    apple.sprite.SetOrigin(appleimage.GetWidth() / 2,
                           appleimage.GetHeight() / 2)
    apple.sprite.SetPosition(
        random.randint(apple.sprite.GetOrigin()[0],
                       Resolution[0] - apple.sprite.GetOrigin()[0]),
        random.randint(apple.sprite.GetOrigin()[1],
                       Resolution[1] - apple.sprite.GetOrigin()[1]))
    apple.sprite.SetColor(
        sf.Color(random.randint(100, 255), random.randint(100, 255),
                 random.randint(100, 255)))