2012-02-25 101 views
0

我试图在pygame中制作游戏,但出于某种原因,actor和inp具有相同的值。 我试图让他们作为数组而不是类,但它没有解决问题。变量具有相同的值,pygame

import pygame, sys 
from pygame.locals import * 

pygame.init() 
screen=pygame.display.set_mode((640,360),0,32) 

a=pygame.image.load('a.png') 

class xy: 
    x=0 
    y=0 
    jump=0 

actor=xy 
inp=xy 

def events(): 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
      return 
     elif event.type == KEYDOWN: 
      if event.key==K_LEFT: 
       inp.x=-1 
      elif event.key==K_RIGHT: 
       inp.x=1 
      elif event.key==K_UP: 
       inp.y=-1 
      elif event.key==K_DOWN: 
       inp.y=1 
      elif event.key==K_SPACE: 
       inp.jump=True 
     elif event.type==KEYUP: 
      if event.key==K_LEFT: 
       inp.x=0 
      elif event.key==K_RIGHT: 
       inp.x=0 
      elif event.key==K_UP: 
       inp.y=0 
      elif event.key==K_DOWN: 
       inp.y=0 
      elif event.key==K_SPACE: 
       inp.jump=False 
    return 

def controller(): 
    if inp.x<0: 
     actor.x-=1 
    elif inp.x>0: 
     actor.x+=1 
    if inp.y<0: 
     actor.y-=1 
    elif inp.y>0: 
     actor.y+=1 

## actor.x+=inp.x 
## actor.y+=inp.y 
    return 

def screen_update(): 
    pygame.draw.rect(screen, 0x006633, ((0,0),(640,360)),0) 
    screen.blit(a, (actor.x,actor.y)) 
    pygame.display.update() 

if __name__ == '__main__': 
    while True: 
     events() 
     print 'inp='+str(inp.x)+','+str(inp.y) 
     print 'actor='+str(actor.x)+','+str(inp.y) 
     controller() 
     screen_update() 

为什么我的工作不能正常工作? :(

回答

3

简单地说,你正在做的类完全错误。

class xy: 
    def __init__(self): 
    self.x = 0 
    self.y = 0 
    self.jump = 0 

actor = xy() 
inp = xy() 
相关问题