kommihe
kommihe

Reputation: 571

Variables has the same value, pygame

I'm trying to make a game in pygame, but for some reason actor and inp has the same value. I tried to have them as arrays instead of classes, but it didn't solve the problem.

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()

Why can't the things I make work properly? :(

Upvotes: 0

Views: 102

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

To put it simply, you're doing classes completely wrong.

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

actor = xy()
inp = xy()

Upvotes: 3

Related Questions