def initialize_contents(self): try: self._exit_text = sf.Text('Are you sure you want to exit?') self._exit_text.font = GameManager.game_font self._exit_text.character_size = 25 x_offset = (self._window.size.x - self._exit_text.global_bounds.width) / 2 y_offset = 160 self._exit_text.position = x_offset, y_offset y_offset = y_offset + self._exit_text.global_bounds.height + 120 self._menu_items = [sf.Text(x) for x in ['Yes', 'No']] for i, item in enumerate(self._menu_items): item.font = GameManager.game_font item.character_size = 20 item.origin = item.global_bounds.width / 2, item.global_bounds.height / 2 item.position = self._window.size.x / 2, y_offset + i * 85 item.color = GameManager.blue_color self._selection_cursor = 0 self.set_selected(self._selection_cursor) self.set_background_file("Data/Images/plain.png") except IOError: exit(1)
def __init__(self, world): self.window = sf.RenderWindow(sf.VideoMode(1024, 600), "Emergency Facility Location Problem") self.facilities = [] self.world = [] font = sf.Font.from_file("font.ttf") for x in xrange(world.shape[0]): for y in xrange(world.shape[1]): demand = sf.Text(str(world[x, y]), font) demand.character_size = 10 demand.color = sf.Color.BLUE demand.position = (x * 20, y * 20) area = sf.RectangleShape() area.size = (20, 20) area.fill_color = sf.Color(0, 0, 0, world[x, y] / 10 * 255) area.position = (x * 20, y * 20) self.world.append((area, demand)) self.stats = sf.Text( "Iterations : 0000000000000000000 \nFitness : 0000000000000000000", font) self.stats.character_size = 14 self.stats.color = sf.Color.MAGENTA self.stats.position = (self.window.size.x - self.stats.local_bounds.width, 0)
def initialize_contents(self): try: self._game_logo = sf.Text('Connect 4') self._game_logo.font = GameManager.logo_font self._game_logo.character_size = 75 x_offset = (self._window.size.x - self._game_logo.global_bounds.width) / 2 y_offset = 60 self._game_logo.position = x_offset, y_offset y_offset = y_offset + self._game_logo.global_bounds.height + 120 self._menu_items = [ sf.Text(x) for x in ['Single Player', 'Two Players', 'About', 'Exit Game'] ] for i, item in enumerate(self._menu_items): item.font = GameManager.game_font item.character_size = 20 item.origin = item.global_bounds.width / 2, item.global_bounds.height / 2 item.position = self._window.size.x / 2, y_offset + i * 85 item.color = GameManager.blue_color self._selection_cursor = 0 self.set_selected(self._selection_cursor) self.set_background_file("Data/Images/plain.png") except IOError: exit(1)
def render(self): self.window.clear() self.window.draw(self.ball) self.window.draw(self.p_left) self.window.draw(self.p_right) #skor tutma kısmı scr_lft_text = sf.Text(str(self.left_score)) scr_lft_text.font = self.font scr_lft_text.character_size = 30 x = (self.window.size.x / 2) - scr_lft_text.global_bounds.width - MARGIN y = 50 scr_lft_text.position = sf.Vector2(x, y) scr_lft_text.color = sf.Color.WHITE self.window.draw(scr_lft_text) scr_rgt_text = sf.Text(str(self.right_score)) scr_rgt_text.font = self.font scr_rgt_text.character_size = 30 x = (self.window.size.x / 2) - scr_rgt_text.global_bounds.width + MARGIN y = 50 scr_rgt_text.position = sf.Vector2(x, y) scr_rgt_text.color = sf.Color.WHITE self.window.draw(scr_rgt_text) self.window.display()
def display_error(): # Create the main window window = sf.RenderWindow(sf.VideoMode(800, 600), 'SFML Shader example') # Define a string for displaying the error message error = sf.Text("Sorry, your system doesn't support shaders") error.position = (100.0, 250.0) error.color = sf.Color(200, 100, 150) # Start the game loop while window.opened: # Process events for event in window.iter_events(): # Close window: exit if event.type == sf.Event.CLOSED: window.close() # Escape key: exit if (event.type == sf.Event.KEY_PRESSED and event.code == sf.Keyboard.ESCAPE): window.close() # Clear the window window.clear() # Draw the error message window.draw(error) # Finally, display the rendered frame on screen window.display()
def make_text(text_x,x,y,font,color,size): txt = sf.Text(text_x) txt.font = font txt.character_size = size txt.position = (x,y) txt.color = color return txt
def main(args): window = sf.RenderWindow(sf.VideoMode(640, 480), 'SFML sound streaming example') window.framerate_limit = 60 running = True stream = None error_message = None if len(args) > 1: stream = CustomStream(args[1]) stream.play() else: error_message = sf.Text( "Error: please provide an audio file as a command-line argument\n" "Example: ./soundstream.py sound.wav") error_message.color = sf.Color.BLACK error_message.character_size = 17 while running: for event in window.iter_events(): if event.type == sf.Event.CLOSED: running = False window.clear(sf.Color.WHITE) if error_message is not None: window.draw(error_message) window.display()
def draw(self): self.window.view = self.view self.window.draw(self.background) self.window.draw(self.player) for creature in self.creatures: self.window.draw(creature) for heal in self.lives: self.window.draw(heal) if self.has_treasure: self.window.draw(self.treasure) if self.is_dark: self.window.draw(self.dark_overlay) else: self.window.draw(self.overlay) self.window.view = self.window.default_view text = sf.Text("Find the treasure!") text.color = sf.Color.YELLOW text.position = (0, HEIGHT - 20) text.character_size = 12 if self.has_treasure: self.window.draw(text) self.window.draw(self.life_point_display) self.window.draw(self.stamina_display)
def draw(self, window): self.box.position = (self.x, self.y) text = sf.Text(self.text) text.font = self.font text.position = (self.x + 20, self.y + 20) text.character_size = 12 text.color = sf.Color.RED messageText = sf.Text(self.message) messageText.font = self.font messageText.position = (15, 15) messageText.character_size = 20 messageText.color = sf.Color.RED window.draw(text) window.draw(messageText) window.draw(self.box)
def draw(self, window): text = sf.Text(str(self.hp)) text.font = self.font text.position = (self.x + 20, self.y + 20) text.character_size = 50 text.color = sf.Color.RED window.draw(text) window.draw(self.box)
def draw(self, target, states): text = "\n".join(self.history[-self.lines_drawn:]) sftext = sf.Text(string=text, font=self.font, character_size=self.char_size) sftext.color = self.color sftext.position = self.position target.draw(sftext, states)
def draw(self, target, states): text = "Your cards:\n" text += "\n".join("{}: {} {}".format(p + 1, c.color.name, c.type.name) for p, c in enumerate(self.hand)) sftext = sf.Text(string=text, font=self.font) sftext.position = (0, 300) sftext.color = sf.Color.RED target.draw(sftext)
def __init__(self, text): super(TextEntity, self).__init__() self.text = sf.Text(text) self.text.font = FM.get("fonts/toscuchet.otf") self.text.color = sf.Color.WHITE self.text.character_size = 72 self.__renderstate = sf.RenderStates()
def __init__(self, window): self._box = sf.RectangleShape() self._box.size = (92, 38) self._box.fill_color = sf.Color.WHITE self._text = sf.Text("00000", settings.monospaceFont, 30) self._text.color = sf.Color.BLACK self._window = window
def __init__(self, name, label, position=(0, 0)): b.BaseComponent.__init__(self, name) self._label = label self._children = [] self._checked = False self._on_click = None self._opened = False self._hovered = False self._itm = sf.Text(self._label) self._itm.position = position
def draw(self, target, states): if not self.font: return text = self.head + ("\n" if self.head else "") text += "\n".join( "{}: {}".format(p + 1, c) for p, c in list(enumerate(self.pile))[-self.draw_last:]) sftext = sf.Text(string=text, font=self.font) sftext.position = self.pos sftext.color = self.color target.draw(sftext)
def draw(self, window): y = 0 text = sf.Text('Last events, with event attributes:', self.font, 14) text.color = sf.Color.BLACK window.draw(text) for event in reversed(self.events): y += text.global_bounds.height text.string = str(event) text.color = sf.Color.BLACK text.y = y window.draw(text)
def __init__( self, dData): # sComponentID, xPos, yPos, width, height, text, font): Component.__init__(self, "TEXTLINE:%s" % (dData['componentID']), False, 1) self._text = sf.Text(dData['text'], dData['font']) self._text.color = sf.Color.BLACK self._text.style = sf.Text.UNDERLINED self._text.x = int(dData['x']) + int( dData['width']) / 2.0 - self._text.global_bounds.width / 2.0 self._text.y = int(dData['y']) + int( dData['height']) / 2.0 - self._text.global_bounds.height / 2.0
def initialize_contents(self): try: self._text = sf.Text('Coming Soon!') self._text.font = GameManager.game_font self._text.character_size = 30 self._text.origin = self._text.global_bounds.width / 2, self._text.global_bounds.height / 2 self._text.position = self._window.size.x / 2, self._window.size.y / 2 self.set_background_file("Data/Images/plain.png") except IOError: exit(1)
def _draw(self, target): # Overloading BaseComponent abstract '_draw' method target.draw(self.x_rect) if not self.focused and self.text == '': shadow = sf.Text(self.shadowtext) shadow.font = self.shadowfont # sf.Font.from_file("./font/eurof56.ttf") shadow.character_size = self.fontsize shadow.color = self.shadow_color shadow.position = sf.Vector2(self.x_rect.global_bounds.left + 5, self.x_rect.global_bounds.top + 5) target.draw(shadow) if self.text != "": x_text = sf.Text(self.text) x_text.font = self.forefont x_text.character_size = self.fontsize x_text.color = self.font_color x_text.position = sf.Vector2(self.x_rect.global_bounds.left + 5, self.x_rect.global_bounds.top + 5) target.draw(x_text) self._run_event('OnDraw')
def __init__(self, font): sf.Drawable.__init__(self) self.time_per_text_update = sf.seconds(1 / UPDATES_PER_SECOND) self.update_time = sf.seconds(0) self.num_frames = 0 self.fps = deque([], UPDATES_PER_SECOND) self.t_update = deque([], UPDATES_PER_SECOND) self.t_render = deque([], UPDATES_PER_SECOND) self.text = sf.Text() self.text.font = font self.text.position = (5, 5) self.text.character_size = 14 self.text.color = sf.Color(220, 220, 100, 220) self.settings_text = sf.Text() self.settings_text.font = font self.settings_text.position = (5, settings.HEIGHT - 65) self.settings_text.character_size = 14 self.settings_text.color = sf.Color(220, 220, 100, 220) self.num_entities = 0 self.collision_checks = 0 self.help_text = sf.Text() self.help_text.font = font self.help_text.position = (settings.WIDTH - 300, 5) self.help_text.character_size = 14 self.help_text.color = sf.Color(220, 220, 100, 220) r = re.compile(r'(.+=) *sf\.Keyboard\.(.*)') with open("settings.py") as s: t = r.findall(s.read()) t = '\n'.join(map(' '.join, t)) self.help_text.string = "KEYBINDINGS:\n" + "------------\n" + t self.help = settings.HELP_ON
def loop(self, background): self._scoretext.string = "Points " + str(self._game_menu._points) self._scoretext_left = sf.Text("Points "+str(self._game_menu._points), self._font, 50) self._scoretext_up = sf.Text("Points "+str(self._game_menu._points), self._font, 50) self._scoretext_right = sf.Text("Points "+str(self._game_menu._points), self._font, 50) self._scoretext_down = sf.Text("Points "+str(self._game_menu._points), self._font, 50) self._scoretext.position = ((self._window.size[0]-self._titletext.local_bounds.size[0])/2,150) self._scoretext_left.position = ((self._window.size[0]-self._titletext.local_bounds.size[0])/2-1.5,150) self._scoretext_up.position = ((self._window.size[0]-self._titletext.local_bounds.size[0])/2,150-1.5) self._scoretext_right.position = ((self._window.size[0]-self._titletext.local_bounds.size[0])/2+1.5,150) self._scoretext_down.position = ((self._window.size[0]-self._titletext.local_bounds.size[0])/2,150+1.5) self._scoretext_left.color = sf.Color.BLACK self._scoretext_up.color = sf.Color.BLACK self._scoretext_right.color = sf.Color.BLACK self._scoretext_down.color = sf.Color.BLACK background.draw(self._window) self._window.draw(self._titletext_left) self._window.draw(self._titletext_up) self._window.draw(self._titletext_right) self._window.draw(self._titletext_down) self._window.draw(self._titletext) self._window.draw(self._enternametext_left) self._window.draw(self._enternametext_up) self._window.draw(self._enternametext_right) self._window.draw(self._enternametext_down) self._window.draw(self._enternametext) self._window.draw(self._scoretext_left) self._window.draw(self._scoretext_up) self._window.draw(self._scoretext_right) self._window.draw(self._scoretext_down) self._window.draw(self._scoretext) self._window.draw(self._nametext_left) self._window.draw(self._nametext_up) self._window.draw(self._nametext_right) self._window.draw(self._nametext_down) self._window.draw(self._nametext) self._window.draw(self._okbutton)
def __init__(self, pos, width, default_text, input): super().__init__(pos, "textbox", 1, 1, input) self.sprite.scale(sf.Vector2(width / self.sprite.texture.width, 1)) self.text_offset = sf.Vector2(7, 3) self.local_bounds = sf.Rectangle( pos, sf.Vector2(width, self.sprite.texture.height)) self.typing = False self.overlapping = False # if the text goes out the textbox self.default_text = default_text self.text = sf.Text(default_text, res.font_farmville, 20) self.text.position = self.local_bounds.position self.text.color = sf.Color.BLACK input.add_text_handler(self)
def main(args): window = sf.RenderWindow(sf.VideoMode(640, 480), 'InputStream example') window.framerate_limit = 60 running = True error_message = None texture_stream = Stream('python-logo.png') texture = sf.Texture.load_from_stream(texture_stream) sprite = sf.Sprite(texture) music = None music_stream = None if len(args) > 1: music_stream = Stream(args[1]) music = sf.Music.open_from_stream(music_stream) music.play() else: error_message = sf.Text( "Error: please provide an audio file as a command-line argument\n" "Example: ./soundstream.py music.ogg") error_message.color = sf.Color.BLACK error_message.character_size = 18 while running: for event in window.iter_events(): if event.type == sf.Event.CLOSED: running = False window.clear(sf.Color.WHITE) window.draw(sprite) if error_message is not None: window.draw(error_message) window.display() texture_stream.close() if music is not None: music.stop() if music_stream is not None: music_stream.close() window.close()
def main(): window = sf.RenderWindow(sf.VideoMode(640, 480), 'Title') window.framerate_limit = 60 text = sf.Text(u'éèà', sf.Font.DEFAULT_FONT, 100) text.color = sf.Color.BLACK text.style = sf.Text.UNDERLINED | sf.Text.BOLD | sf.Text.ITALIC text.x = window.width / 2.0 - text.global_bounds.width / 2.0 text.y = window.height / 2.0 - text.global_bounds.height / 2.0 running = True while running: for event in window.iter_events(): if event.type == sf.Event.CLOSED: running = False window.clear(sf.Color.WHITE) window.draw(text) window.display() window.close()
def __init__(self, position, size, bgcolor, fgcolor, linecolor, linesize, lable, font, fontsize): self._position = position self._size = size self._bgcolor = bgcolor self._fgcolor = fgcolor self._linecolor = linecolor self._linesize = linesize self._lable = lable self._font = font self._fontsize = fontsize #def RecShape(): #RectangleShape self._recShape = sf.RectangleShape() self._recShape.position = self._position self._recShape.size = self._size self._recShape.fill_color = self._bgcolor self._recShape.outline_color = self._linecolor self._recShape.outline_thickness = self._linesize #def Rec(): #Rectangle self._rec = sf.Rectangle(self._position, self._size) #def Text(): #text self._text = sf.Text() self._text.string = self._lable self._text.font = self._font self._text.character_size = self._fontsize self._text.style = sf.Text.BOLD textpos = sf.Vector2( (self._size[0] - self._text.local_bounds.size[0]) / 2, self._size[1] - self._fontsize) + self._position textpos = sf.Vector2( textpos[0], (textpos[1] - (self._text.local_bounds.size[1] / 5))) self._text.position = textpos self._text.color = self._fgcolor
def draw(self, surface): text = sf.Text(self.textContent) text.font = self.font text.character_size = 10 text.color = sf.Color.WHITE text.position = (self.x + 2, self.y + 2) self.outline.outline_color = self.currentColor self.outline.position = (self.x, self.y) surface.draw(text) surface.draw(self.outline) mouseX, mouseY = surface.map_pixel_to_coords( sf.Mouse.get_position(surface)) if self.canDisplayImage: self.hoverSprite.position = sf.Vector2(mouseX, self.y + 3) if type(self) is not Card: self.hoverSprite.position = sf.Vector2( mouseX, self.y - self.hoverSprite.texture.height) if self.check_hover(mouseX, mouseY) and self.canDisplayImage: aspectX = surface.width aspectY = surface.height self.hoverSprite.ratio = (1100 / aspectX, 850 / aspectY) surface.draw(self.hoverSprite)
def __init__(self, window, framebuffer, config): self.window = window self.framebuffer = framebuffer self.framebuffer_scale = float(config["window"]["framebuffer_scale"]) self.update_frequency = float(config["game"]["update_frequency"]) self.show_fps = du.strtobool(config["game"]["show_fps"]) self.should_run = True self.game_states = [] self.active_game_state = None self.mouse_previous_position = sf.Vector2() self.calculate_mouse_delta() self.mouse_delta = sf.Vector2() self.fps_counter = fps_counter.FpsCounter() self.fps_font = sf.Font.from_file( "data/fonts/dejavu-sans-mono-bold.ttf") self.fps_text = sf.Text("56", self.fps_font, 16) self.fps_text.position = (4, 2) self.fps_text.style = sf.Text.REGULAR self.fps_text.color = sf.Color(255, 255, 255, 255)
def __init__(self, window, game_menu): self._window = window self._game_menu = game_menu self._font = settings.defaultFont self._name = "" self._titletext = sf.Text("Help", self._font, 50) self._titletext_left = sf.Text("Help", self._font, 50) self._titletext_up = sf.Text("Help", self._font, 50) self._titletext_right = sf.Text("Help", self._font, 50) self._titletext_down = sf.Text("Help", self._font, 50) self._titletext.position = ( (self._window.size[0] - self._titletext.local_bounds.size[0]) / 2, 20) self._titletext_left.position = ( (self._window.size[0] - self._titletext.local_bounds.size[0]) / 2 - 1.5, 20) self._titletext_up.position = ( (self._window.size[0] - self._titletext.local_bounds.size[0]) / 2, 20 - 1.5) self._titletext_right.position = ( (self._window.size[0] - self._titletext.local_bounds.size[0]) / 2 + 1.5, 20) self._titletext_down.position = ( (self._window.size[0] - self._titletext.local_bounds.size[0]) / 2, 20 + 1.5) self._titletext_left.color = sf.Color.BLACK self._titletext_up.color = sf.Color.BLACK self._titletext_right.color = sf.Color.BLACK self._titletext_down.color = sf.Color.BLACK self._helptext = sf.Text( "The goal of this game is to light-off as many bulbs with your\nbacon as possible. The brighter/whiter the light is," + "the more\npoints you will get for lighting it off...\n" + "Occasionally some bread slices will appear on top of lighted\n" + "windows. Lighting off windows with these slices will give you\n" + "some extra points.\n" + "Additionally at the top corner you will find a points-counter\n" + "and a bar representing your remaining energy. Lighting off\n" + "windows will replenish some of your energy and those with a\n" + "bread slice will replenish even a bit more.\n" + "But beware! Every window is consuming some of your energy\n" + "and so the more windows are lighted, the faster your energy\n" + "will be consumed. The further you are into the game, the\n" + "faster new windows will be lighted.\n" "Once your energy reaches 0 it\'s \"Game Over\" for you.", self._font, 25) self._helptext_left = sf.Text( "The goal of this game is to light-off as many bulbs with your\nbacon as possible. The brighter/whiter the light is," + "the more\npoints you will get for lighting it off...\n" + "Occasionally some bread slices will appear on top of lighted\n" + "windows. Lighting off windows with these slices will give you\n" + "some extra points.\n" + "Additionally at the top corner you will find a points-counter\n" + "and a bar representing your remaining energy. Lighting off\n" + "windows will replenish some of your energy and those with a\n" + "bread slice will replenish even a bit more.\n" + "But beware! Every window is consuming some of your energy\n" + "and so the more windows are lighted, the faster your energy\n" + "will be consumed. The further you are into the game, the\n" + "faster new windows will be lighted.\n" "Once your energy reaches 0 it\'s \"Game Over\" for you.", self._font, 25) self._helptext_up = sf.Text( "The goal of this game is to light-off as many bulbs with your\nbacon as possible. The brighter/whiter the light is," + "the more\npoints you will get for lighting it off...\n" + "Occasionally some bread slices will appear on top of lighted\n" + "windows. Lighting off windows with these slices will give you\n" + "some extra points.\n" + "Additionally at the top corner you will find a points-counter\n" + "and a bar representing your remaining energy. Lighting off\n" + "windows will replenish some of your energy and those with a\n" + "bread slice will replenish even a bit more.\n" + "But beware! Every window is consuming some of your energy\n" + "and so the more windows are lighted, the faster your energy\n" + "will be consumed. The further you are into the game, the\n" + "faster new windows will be lighted.\n" "Once your energy reaches 0 it\'s \"Game Over\" for you.", self._font, 25) self._helptext_right = sf.Text( "The goal of this game is to light-off as many bulbs with your\nbacon as possible. The brighter/whiter the light is," + "the more\npoints you will get for lighting it off...\n" + "Occasionally some bread slices will appear on top of lighted\n" + "windows. Lighting off windows with these slices will give you\n" + "some extra points.\n" + "Additionally at the top corner you will find a points-counter\n" + "and a bar representing your remaining energy. Lighting off\n" + "windows will replenish some of your energy and those with a\n" + "bread slice will replenish even a bit more.\n" + "But beware! Every window is consuming some of your energy\n" + "and so the more windows are lighted, the faster your energy\n" + "will be consumed. The further you are into the game, the\n" + "faster new windows will be lighted.\n" "Once your energy reaches 0 it\'s \"Game Over\" for you.", self._font, 25) self._helptext_down = sf.Text( "The goal of this game is to light-off as many bulbs with your\nbacon as possible. The brighter/whiter the light is," + "the more\npoints you will get for lighting it off...\n" + "Occasionally some bread slices will appear on top of lighted\n" + "windows. Lighting off windows with these slices will give you\n" + "some extra points.\n" + "Additionally at the top corner you will find a points-counter\n" + "and a bar representing your remaining energy. Lighting off\n" + "windows will replenish some of your energy and those with a\n" + "bread slice will replenish even a bit more.\n" + "But beware! Every window is consuming some of your energy\n" + "and so the more windows are lighted, the faster your energy\n" + "will be consumed. The further you are into the game, the\n" + "faster new windows will be lighted.\n" "Once your energy reaches 0 it\'s \"Game Over\" for you.", self._font, 25) self._helptext.position = ( (self._window.size[0] - self._helptext.local_bounds.size[0]) / 2, 100) self._helptext_left.position = ( (self._window.size[0] - self._helptext.local_bounds.size[0]) / 2 - 1.5, 100) self._helptext_up.position = ( (self._window.size[0] - self._helptext.local_bounds.size[0]) / 2, 100 - 1.5) self._helptext_right.position = ( (self._window.size[0] - self._helptext.local_bounds.size[0]) / 2 + 1.5, 100) self._helptext_down.position = ( (self._window.size[0] - self._helptext.local_bounds.size[0]) / 2, 100 + 1.5) self._helptext_left.color = sf.Color.BLACK self._helptext_up.color = sf.Color.BLACK self._helptext_right.color = sf.Color.BLACK self._helptext_down.color = sf.Color.BLACK #backbutton self._backbutton = Button( sf.Vector2(20, (self._window.size[1] - 50)), #position sf.Vector2(150, 20), #size sf.Color.GREEN, #background color sf.Color.BLACK, #text color sf.Color.RED, #outline color 2, #outline thickness "Back", #lable self._font, #font 17) #text size
def run(): window = sf.RenderWindow(sf.VideoMode(WWIDTH, WHEIGHT), WTITLE) window.framerate_limit = 60 # Background bg_texture = sf.Texture.from_file("assets/images/background.png") background = sf.Sprite(bg_texture) # Ball b_texture = sf.Texture.from_file("assets/images/ball.png") ball = sf.CircleShape(35) ball.texture = b_texture ball.origin = 35, 35 ball.position = WWIDTH / 2.0, WHEIGHT / 2.0 speed = sf.Vector2(randint(-5, 5), randint(-5, 5)) * 50.0 # Paddle 1 paddle_1 = sf.RectangleShape((50, 175)) paddle_1.origin = 25.0, 82.5 paddle_1.position = 50, WHEIGHT / 2.0 # Paddle 2 paddle_2 = sf.RectangleShape((50, 175)) paddle_2.origin = 25.0, 82.5 paddle_2.position = WWIDTH - 50, WHEIGHT / 2.0 # Scores scored = False p1_score, p2_score = 0, 0 # Font font = sf.Font.from_file("assets/fonts/kenvector.ttf") # Texts p1_score_text = sf.Text(str(p1_score)) p1_score_text.font = font p1_score_text.character_size = 72 p1_score_text.color = sf.Color.WHITE p1_score_text.position = 170, 100 p2_score_text = sf.Text(str(p2_score)) p2_score_text.font = font p2_score_text.character_size = 72 p2_score_text.color = sf.Color.WHITE p2_score_text.position = 570, 100 # Sound s_buffer = sf.SoundBuffer.from_file("assets/sounds/tone1.ogg") sound = sf.Sound(s_buffer) # Clock clock = sf.Clock() while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() # Close if sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE): window.close() elapsed = clock.elapsed_time.seconds clock.restart() # Inputs if sf.Keyboard.is_key_pressed(sf.Keyboard.W): paddle_1.move(sf.Vector2(0, -PADDLE_SPEED)) if paddle_1.position.y < paddle_1.origin.y: paddle_1.position = sf.Vector2(paddle_1.position.x, paddle_1.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.S): paddle_1.move(sf.Vector2(0, PADDLE_SPEED)) if paddle_1.position.y > WHEIGHT - paddle_1.origin.y: paddle_1.position = sf.Vector2(paddle_1.position.x, WHEIGHT - paddle_1.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.UP): paddle_2.move(sf.Vector2(0, -PADDLE_SPEED)) if paddle_2.position.y < paddle_2.origin.y: paddle_2.position = sf.Vector2(paddle_2.position.x, paddle_2.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN): paddle_2.move(sf.Vector2(0, PADDLE_SPEED)) if paddle_2.position.y > WHEIGHT - paddle_2.origin.y: paddle_2.position = sf.Vector2(paddle_2.position.x, WHEIGHT - paddle_2.origin.y) if scored: speed = sf.Vector2(randint(-5, 5), randint(-5, 5)) * 50.0 ball.position = WWIDTH / 2.0, WHEIGHT / 2.0 paddle_1.position = 50, WHEIGHT / 2.0 paddle_2.position = WWIDTH - 50, WHEIGHT / 2.0 scored = False ball.move(speed * elapsed) if ball.position.x < ball.origin.x or ball.position.x > WWIDTH - ball.origin.x: scored = True if ball.position.x < ball.origin.x: p2_score += 1 else: p1_score += 1 p1_score_text.string = str(p1_score) p2_score_text.string = str(p2_score) if ball.position.y < ball.origin.y or ball.position.y > WHEIGHT - ball.origin.y: speed = sf.Vector2(speed.x, speed.y * -1.0) p1_col = ball.global_bounds.intersects(paddle_1.global_bounds) if p1_col: sound.play() if p1_col.top + p1_col.height / 2.0 > paddle_1.position.y: y = (-1.0, 1.0)[speed.y > 0] else: y = (1.0, -1.0)[speed.y > 0] x_factor = (1.0, 1.05)[-MAX_BALL_SPEED < speed.x < MAX_BALL_SPEED] speed = sf.Vector2(speed.x * -1.0 * x_factor, speed.y * y) p2_col = ball.global_bounds.intersects(paddle_2.global_bounds) if p2_col: sound.play() if p2_col.top + p2_col.height / 2.0 > paddle_2.position.y: y = (-1.0, 1.0)[speed.y > 0] else: y = (1.0, -1.0)[speed.y > 0] x_factor = (1.0, 1.05)[-MAX_BALL_SPEED < speed.x < MAX_BALL_SPEED] speed = sf.Vector2(speed.x * -1.0 * x_factor, speed.y * y) # Rendering window.clear(sf.Color.BLACK) window.draw(background) window.draw(ball) window.draw(paddle_1) window.draw(paddle_2) window.draw(p1_score_text) window.draw(p2_score_text) window.display()