Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:22:49+00:00 2026-06-11T15:22:49+00:00

I’m running a pygames program with twisted and I’m having problems when it comes

  • 0

I’m running a pygames program with twisted and I’m having problems when it comes to sending data from within a pygame event.

First here is the server:

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import simplejson

class Game_Data(LineReceiver):
    def __init__(self, players, clients):
        self.players = players
        self.clients = clients

## the connectionMade method of LineReceiver is being used to create ##
## protocol instances of each client so it can send back any data it gets ##

    def connectionMade(self):
        new_player = 'player_' + str(len(self.players) + 1)
        self.clients.append(self)
        self.players.append(new_player)
        self.players = simplejson.dumps(self.players)
        for client in self.clients:
            client.sendLine(self.players)

## what ever data the server receives it sends right back to any clients ##

    def lineReceived(self,line):
        self.line = line
        print self.line
        for client in self.clients:
            client.sendLine(self.line)


class BlackFactory(Factory):
    def __init__(self):
        self.players = []
        self.clients = []

    def buildProtocol(self, addr):
        return Game_Data(self.players, self.clients)


reactor.listenTCP(6000, BlackFactory())

Now for the client:

import pygame
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet.task import LoopingCall
from twisted.internet import reactor

class BlackClientProtocol(LineReceiver):
    def __init__(self, recv, host):
        self.recv = recv
        self.host = host

    def lineReceived(self, line):
        self.recv(line)
        print line ## prints out as expected ##

    def connectionMade(self):
        self.host = self
        print self.host ## prints out as expected ##

class BlackClient(ClientFactory):
    def __init__(self, recv, host):
        self.recv = recv
        self.host = host

    def buildProtocol(self, addr):
        return BlackClientProtocol(self.recv, self.host)

class Client(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((800, 600))
        pygame.display.flip()
        reactor.callLater(0.1, self.tick)

    def new_line(self, line):
        self.line = line

## this trial_func was to see if I could pass the BlackClient another argument ##
## to return an instance of the protocol to be called later in this class ##     

    def trial_func(self, host):
        self.host = host

    def tick(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                print self.line  ## prints out as expected##
                print self.host  ## does not print out ???##


if __name__ == '__main__':
    c = Client()
    lc = LoopingCall(c.tick)
    lc.start(0.1)
    reactor.connectTCP('192.168.1.2', 6000, BlackClient(c.new_line, c.trial))
    reactor.run()

Edit: This is a more explicit example of whats going on. With the comments being the hang-ups. As long as you have pygames installed this will run, with the pygame event being a simple KEYDOWN (escape key) event as the trigger for the client to send data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:22:50+00:00Added an answer on June 11, 2026 at 3:22 pm

    I looked at your code and this seems to be the relevant part:

    def tick(self):
        flag = 0
        for event in pygame.event.get():
        ...
    ## call an instance of the protocol and sendLine ##
                        BlackClient(self.new_line(allhands)) ## this line does not work ##
    ## send allhands data ##
        ...
    

    You want so send data back to the server, but here you’re just creating a new instance of BlackClient.

    You probably want to use sendLine to send the line, but you need a reference to the current protocoll instance (or at least to this method).


    This is an example how you could achieve this:

    class Client(object):
        ...
        # this method is going to be replaced
        def sendLine(self, line):
            pass
    
        def tick(self):
            flag = 0
            for event in pygame.event.get():
                ...
                ## call sendLine, which is replaced by the sendLine method from the protocol ##
                self.sendLine(yourStuff)        
    
    class BlackClient(ClientFactory):
        def __init__(self, client):
            # keep instance of client
            self.client = client
    
        def buildProtocol(self, addr):
            # give client.new_line method to protocol
            proto = BlackClientProtocol(self.client.new_line)
            # replace client's sendLine with proto.sendLine
            self.client.sendLine = proto.sendLine
            return proto
    
    if __name__ == '__main__':
        c = Client()
        lc = LoopingCall(c.tick)
        lc.start(0.1)
        protocoll = 
        reactor.connectTCP('192.168.1.2', 6000, BlackClient(c))
        reactor.run()
    

    This is just an example; you could probably clean up the code a bit. Maybe you want to use some kind of dispatcher (like pydispatch)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.