def __init__(self, window_width, window_height): self.window_width = window_width self.arena_bottom, self.exit_left, self.exit_right = window_height - 80, window_width / 2 - 50, window_width / 2 + 50 self['level_str'] = sf.String() self['level_str'].SetColor(sf.Color.White) self['level_str'].SetPosition(60., window_height - 60) self['score_str'] = sf.String() self['score_str'].SetColor(sf.Color.White) self['score_str'].SetPosition(260., window_height - 60) self.exit_rect = sf.Shape.Rectangle(self.exit_left, 0, self.exit_right, self.arena_top, sf.Color.Black) self.reset() self.update_arena_rect()
def __init__(self, parent = None, string = u"", *args, **kwargs): string = unicode (string) # HACK? if not isinstance(string, unicode): raise TypeError("tf: Error: String " + string + " is not unicode.") Component.__init__(self, parent) self._sprite = sf.String(string, *args, **kwargs) self._string = string self._recheck_size ()
def __init__(self, screen_width, screen_height): self.selection = 0 text_color = sf.Color(220, 220, 20, 255) self.spacing = screen_height / 7 self.title = sf.String("PyWorm!") self.title.SetColor(text_color) self.title.SetPosition(screen_width / 2 - 80., self.spacing) levels = ["Very Easy", "Easy", "Medium", "Hard"] x_align = [-80., -50., -70., -50.] self.strings = [] for i in range(0, 4): string = sf.String(levels[i]) string.SetColor(text_color) string.SetPosition(screen_width / 2 + x_align[i], (2 + i) * self.spacing + 20) self.strings.append(string) self.rectangle = sf.Shape.Rectangle(0, 0, screen_width, 40, sf.Color(50, 50, 10))
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()
#include PySFML from PySFML import sf #Create the main window window = sf.RenderWindow(sf.VideoMode(1024, 768), "PySFML test") #Create a string to display text = sf.String("Hello SFML") #Start the game loop running = True while running: event=sf.Event() while window.GetEvent(event): if event.Type == sf.Event.Closed: running = False #Clear screen, draw text, update window window.Clear() window.Draw(text) window.Display()
def main(): # Create main window App = sf.RenderWindow(sf.VideoMode(800, 600), "SFML OpenGL") App.PreserveOpenGLStates(True) # 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) # 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.String("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
def main(): nombreGentils = 100 nombreMechants = 100 taillex = 800.0 tailley = 700.0 window = sf.RenderWindow(sf.VideoMode(int(taillex), int(tailley)), "Test de LU") running = True while (running): event = sf.Event() while window.GetEvent(event): if event.Type == sf.Event.Closed: running = False texte = sf.String() terre = lu.Planet(taillex, tailley) for i in range(nombreGentils): terre.newAnimal(reprod=0.95, vie=150) for i in range(nombreMechants): terre.newAnimal(1, vie=150) terre.newRandomFood(1) terre.newRandomFood(0) terre.newRandomFood(1) terre.newRandomFood(0) window.Clear() window.Display() a = 0 for i in range(500): window.Clear() a += 1 drawlist = terre.round() print "ROUND : " + str(a) pointList = [] for t in range(len(drawlist.a) - 1): if drawlist.d[t] == True: if drawlist.c[t] == 0: rectangle = sf.Shape.Circle(drawlist.a[t], drawlist.b[t], 1, sf.Color.Green) pointList.append(rectangle) if drawlist.c[t] == 1: rectangle = sf.Shape.Circle(drawlist.a[t], drawlist.b[t], 1, sf.Color.Blue) pointList.append(rectangle) for f in pointList: window.Draw(f) for f in terre.foodList: if f.duree > 0: if f.sorte == 0: rectangle = sf.Shape.Circle(f.x, f.y, 3, sf.Color.White) if f.sorte == 1: rectangle = sf.Shape.Circle(f.x, f.y, 3, sf.Color.Yellow) window.Draw(rectangle) texte.SetText(str(terre.nombreAnim)) texte.SetSize(30) window.Draw(texte) window.Display() running = False