Beispiel #1
0
def rungame():
  global gsh
  global tickinterval
  global playerlist
  global chatitems

  tickinterval = 50

  try:
    mode = argv[1]
    if mode == "s":
      port = argv[2]
      if len(argv) > 3:
        tickinterval = int(argv[3])
    elif mode == "c":
      addr = argv[2]
      port = argv[3]
  except:
    print "usage:"
    print argv[0], "s port [ticksize=50]"
    print argv[0], "c addr port"
    print "s is for server mode, c is for client mode."
    sys.exit()

  if mode == "c":
    init()
  
  if mode == "s": # in server mode
    gs = network.initServer(int(port))
  elif mode == "c": # in client mode
    gs = network.initClient(addr,int(port)) 
  else:
    print "specify either 's' or 'c' as mode."
    sys.exit()
  
  # sets some stuff for the network sockets.
  network.setupConn()

  # this is important for the simulation.
  gs.tickinterval = tickinterval

  # in order to be reactive, the state history, which is currently server-side
  # only, has to incorporate input events at the time they actually happened,
  # rather than when they were received. Thus, a number of old gamestates is
  # preserved.
  #
  # on the client, the history simply deals with python-object changes that
  # update proxy objects.
  gsh = StateHistory(gs)

  # since the server is dedicated and headless now, we don't need a ship for it
  if mode == "c":
    # this is used to fix the camera on the ship and display information about
    # his ship to the player.
    myshipid = network.clients[None].shipid
    # a proxy is an object, that will automatically be updated by the gamestate
    # history object to always reflect the current object. This is accomplished
    # with black magic.
    localplayer = gs.getById(myshipid).getProxy(gsh)

  # yay! play the game!
  
  # inits pygame and opengl.
  running = True
  timer.wantedFrameTime = tickinterval * 0.001
  timer.startTiming()

  timer.gameSpeed = 1

  # this variable is used to determine, when it would be wise to skip
  # displaying a single gamestate, to catch up with the time the data
  # from the server is received.
  catchUpAccum = 0

  if mode == "c":
    playerlist = []
    makePlayerList()
    # used for chat.
    sentence = ""
    textthing = Text(sentence)

    def updateTextThing():
      glEnable(GL_TEXTURE_2D)
      textthing.renderText(sentence)
      glDisable(GL_TEXTURE_2D)

  while running:
    timer.startFrame()
    if mode == "c":
      for event in pygame.event.get():
        if event.type == QUIT:
          running = False

        if event.type == KEYDOWN:
          if event.key == K_ESCAPE:
            running = False

          # chat stuff
          elif K_a <= event.key <= K_z:
            sentence += chr(ord('a') + event.key - K_a)
            updateTextThing()
          elif event.key == K_SPACE:
            sentence += " "
            updateTextThing()
          elif event.key == K_BACKSPACE:
            sentence = sentence[:-1]
            updateTextThing()
          elif event.key == K_RETURN:
            if sentence:
              network.sendChat(sentence)
            sentence = ""
            updateTextThing()
          # end chat stuff

      # player control stuff
      kp = pygame.key.get_pressed()
      if kp[K_LEFT]:
        sendCmd("l")
      if kp[K_RIGHT]:
        sendCmd("r")
      if kp[K_UP]:
        sendCmd("t")
      if kp[K_SPACE]:
        sendCmd("f")
      # end player control stuff

      catchUpAccum += timer.catchUp
      # only if the catchUp time is below our patience or we run the server,
      # the gamestate should be rendered and calculated.
      if catchUpAccum < 2:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        with glIdentityMatrix():
          # put the player in the middle of the screen
          glTranslatef(25 - localplayer.position[0], 18.5 - localplayer.position[1], 0)
          # render everything
          renderers.renderGameGrid(localplayer)
          renderers.renderWholeState(gsh[-1])

        with glIdentityMatrix():
          glScalef(1./32, 1./32, 1)
          # do gui stuff here
          with glMatrix():
            glScale(3, 3, 1)
            for pli in playerlist:
              pli.draw()
              glTranslate(0, 16, 0)
          with glMatrix():
            glScale(2, 2, 1)
            glTranslate(24, 540, 0)
            for msg in [textthing] + chatitems[::-1]:
              msg.draw()
              glTranslate(0, -17, 0)
          glDisable(GL_TEXTURE_2D)

        pygame.display.flip()

    # for the server, this lets new players in, distributes chat messages and
    # reacts to player inputs.
    network.pumpEvents()

    if mode == "c":
      if catchUpAccum > 2:
        print "catchUpAccum is", catchUpAccum
        catchUpAccum = 0

    timer.endFrame()

  # exit pygame
  if mode == "c":
    pygame.quit()
Beispiel #2
0
def rungame():
  # init all stuff
  init()

  # yay! play the game!
  running = True
  timer.startTiming()

  lvl = level.load("rageons3")
  lvl.scroller.w = screensize[0] / 32 + 1
  lvl.scroller.h = screensize[1] / 32 + 1

  plr = sprite.Sprite("player")
  plr.w = 1.0
  plr.h = 0.9
  plr.x, plr.y = lvl.plrstartx, lvl.plrstarty
  plr.health = 100

  possiblepositions = [(1, 1), (1.5, 1), (2.5, 1), (3, 1), (3.5, 1), (4, 1), (4.5, 1),
                       (1, 8)]

  possibleenemys = ["enemy", "enemy2", "enemy3", "enemy4"]

  #possiblepositions = []
  #for y in range(lvl.h):
  #  for x in range(lvl.w):
  #    if lvl.collision[lvl.level[y][x]] == 0:
  #      possiblepositions.append((x, y))
  #      print lvl.level[y][x], (x, y)
  # TODO: find out why the mabla this doesn't work!
  
  enemies = lvl.lvlenemies
  pain = []
  ppain = []

  for ne in enemies:
    ne.vx = 0.5
    ppain.append(damageArea.RectDamage(ne.x, ne.y, ne.w, ne.h, ne.vx, ne.vy, -1, 10))
    ne.damager = ppain[-1] 

  timer.gameSpeed = 1

  lasthithp = 0

  sentence = ""
  sentencestrength = 1
  mode = 0

  textthing = font.Text("")

  while running:
    timer.startFrame()
    for event in pygame.event.get():
      if event.type == QUIT:
        running = False

      if mode == 1:
        if event.type == KEYDOWN:
          if K_a <= event.key <= K_z:
            if event.key == K_h:
              sentence += "'"
            else:
              sentence += chr(ord('a') + event.key - K_a)
            textthing.renderText(sentence + "_")
            textthing.rgba = (1, 1, 1, 1)
          elif event.key == K_SPACE:
            sentence += " "
            textthing.renderText(sentence + "_")
            textthing.rgba = (1, 1, 1, 1)
          elif event.key == K_BACKSPACE:
            sentence = sentence[:-1]
            textthing.renderText(sentence + "_")
            textthing.rgba = (1, 1, 1, 1)
            if len(sentence) == 0:
              mode = 0
              textthing.renderText(sentence)
          elif event.key == K_RETURN:
            mode = 0
            sentencestrength = 1
            textthing.renderText(sentence)
      else:
        if event.type == KEYDOWN:
          if event.key == K_s:
            mode = 1
            sentence = ""
            textthing.renderText(sentence + "_")
            textthing.rgba = (1, 1, 1, 1)
          if event.key == K_SPACE:
            if plr.vx > 0:
              x  = plr.x + plr.w
              sa = pi / -2
              ea = pi / 2
            else:
              x  = plr.x
              sa = pi / 2
              ea = pi / 2 * 3
            if plr.physics == 'standing':
              plr.vy -= 2
            np = damageArea.ArcDamage(x, plr.y + plr.h / 2.0, plr.vx, plr.vy, 0.75, sa, ea, 0.25, 10 + len(sentence) * sentencestrength * 3)
            pain.append(np)
            sentencestrength *= 0.75
            textthing.rgba = (1, 1, 1, sentencestrength)

    if mode == 0:
      if pygame.key.get_pressed()[K_UP]:
        if plr.physics == 'standing':
          plr.vy = -6.0
      elif plr.physics == 'falling' and plr.vy < 0:
        plr.vy *= 1.0 - timer.curspd

      if pygame.key.get_pressed()[K_LEFT]:
        if plr.state != 'ouch':
          if plr.physics == 'standing':
            plr.vx = max(-3, plr.vx - 10 * timer.curspd)
          else:
            plr.vx = max(-3, plr.vx - 5 * timer.curspd)

      elif pygame.key.get_pressed()[K_RIGHT]:
        if plr.state != 'ouch':
          if plr.physics == 'standing':
            plr.vx = min(3, plr.vx + 10 * timer.curspd)
          else:
            plr.vx = min(3, plr.vx + 5 * timer.curspd)

      else:
        if plr.vx != 0:
          plr.vx *= 0.99

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()

    plr.move()
    for p in ppain:
      p.move()
      if p.lifetime == 0:
        ppain.remove(p)
      if p.check(plr):
        p.hit(plr)
    for p in pain:
      p.move()
      if p.lifetime == 0:
        pain.remove(p)
    for en in enemies:
      en.move()
      for p in pain:
        if p.check(en):
          p.hit(en)
          lasthithp = en.health
      if en.state == 'dead':
        enemies.remove(en)

    lvl.scroller.centerOn(plr.x, plr.y, timer.curspd / 2)

    glPushMatrix()
    # do stuff
    lvl.scroller.scroll()
    lvl.draw()
    glPushMatrix()
    #lvl.scroller.scroll()
    plr.draw()
    for en in enemies:
      en.draw()
    for p in pain:
      p.draw()

    glPopMatrix()
    glPopMatrix()

    glPushMatrix()
    glLoadIdentity()

    glTranslatef(5, 5, 0)
    glScalef(1./32, 1./32, 1)
    textthing.draw()

    glPopMatrix()

    # enemy HP
    glDisable(GL_TEXTURE_2D)
    glColor4f(1.0, 1.0, 1.0, 1.0)
    glBegin(GL_QUADS)
    glVertex2f(0, 0)
    glVertex2f(2, 0)
    glVertex2f(2, 0.2)
    glVertex2f(0, 0.2)

    glColor4f(1.0, 0.0, 0.0, 1.0)
    glVertex2f(0.05, 0.05)
    glVertex2f(0.05 + lasthithp / 100.0 * 1.9, 0.05)
    glVertex2f(0.05 + lasthithp / 100.0 * 1.9, 0.15)
    glVertex2f(0, 0.15)
    glEnd()
    
    # player HP
    glColor4f(1.0, 1.0, 1.0, 1.0)
    glBegin(GL_QUADS)
    glVertex2f(3, 0)
    glVertex2f(5, 0)
    glVertex2f(5, 0.2)
    glVertex2f(3, 0.2)

    glColor4f(1.0, 0.0, 0.0, 1.0)
    glVertex2f(3.05, 0.05)
    glVertex2f(3.05 + plr.health / 100.0 * 1.9, 0.05)
    glVertex2f(3.05 + plr.health / 100.0 * 1.9, 0.15)
    glVertex2f(3, 0.15)
    glEnd()

    pygame.display.flip()
    timer.endFrame()

  # exit pygame
  pygame.quit()