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 6320857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:02:57+00:00 2026-05-24T16:02:57+00:00

This question has already given me the reason WHY this error is happening now

  • 0

This question has already given me the reason WHY this error is happening now I want to know how to solve this issue.

Here is the code for main.py

from twisted.internet import reactor
import pygame

from networking import run, construct_factory

class GameEngine(object):
    def __init__(self, client):
        pygame.init()
        self.screen = pygame.display.set_mode((640, 400))
        self.FPS = 60
        self.client = client.connectedProtocol
        reactor.callWhenRunning(self.grab_all_sprites)

    def grab_all_sprites(self):
        with open('sprites.txt') as sprites:
            for sprite in sprites:
                sprite_file = self.client.download_sprite(sprite)
                with open(r'resources\%s.png' % sprite, 'wb') as out:
                    out.write(sprite_file)


    def spin(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                reactor.stop()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    print "spacebar!"

        #update the player
        pygame.display.flip()
        reactor.callLater((1/self.FPS), self.spin)

if __name__ in '__main__':
    client = construct_factory()    
    game = GameEngine(client)
    run(game, client)

here is the code for networking.py

from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory
from twisted.protocols import amp
import sys

from ampcommands import TransferSprite

class GameClient(amp.AMP):
    def download_sprite(self, sprite):
        self.callRemote(TransferSprite, sprite)

class GameClientFactory(ClientFactory):
    protocol = GameClient

    def buildProtocol(self, address):
        proto = ClientFactory.buildProtocol(self, address)
        self.connectedProtocol = proto
        return proto

def construct_factory():
    return GameClientFactory()

def run(game, factory, verbose=True):

    if verbose:
        from twisted.python import log
        log.startLogging(sys.stdout)

    reactor.connectTCP("localhost", 1234, factory)
    reactor.callWhenRunning(game.spin)
    reactor.run()

I have no idea in the slightest how to get game.spin to be called AFTER the connection is made so that GameClientFactory.connectedProtocol. I’m getting confused and tired can anyone spot a better way?

  • 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-05-24T16:02:57+00:00Added an answer on May 24, 2026 at 4:02 pm

    This seems like a case where your question is your answer. Remove your existing GameEngine instantiation code and change your GameClientFactory to have a buildProtocol like this:

    def buildProtocol(self, address):
        proto = ClientFactory.buildProtocol(self, address)
        GameEngine(proto).spin()
        return proto
    

    Change GameEngine.__init__ to just accept the protocol, too, since it’s easier to just pass it in rather than make it an attribute of another object and then pass in that other object.

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

Sidebar

Related Questions

This is a homework question, binary search has already been introduced: Given two arrays,
I know this question has already been asked, but the solution doesn't work in
If this question has already been asked I appologies, please point me in the
Preamble So, this question has already been answered, but as it was my first
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
I know this specific question has been asked before , but I am not
I used the code from Why is this slideshow flickering? which peter has given
This question has already been posted before, however, the correct answer doesn't seem to
This question has been asked before ( link ) but I have slightly different

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.