reguarding character movement. Diagonal?

reguarding character movement. Diagonal?

Postby spamynator_1 » Wed Oct 16, 2013 6:59 pm

I am trying to make it so my character can move diagonally when you press w and a, w and d, s and a, and s and d. i cant seem to make it so it hits the loop so that when you hold two buttons it does (eg. -6x and -6y). The issue i am having is that event.pos can have 2 values. Any help would be appreciated thank you.

My code for those if statements are below.

Code: Select all
 #MOVEMENT
                #Up Left
                elif event.key == pg.K_w and event.key == pg.K_a:
                    self.locationy -= 6
                    self.locationx -= 6
                    self.char = 4
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Up Right
                elif event.key == pg.K_w and event.key == pg.K_d:
                    self.locationy -= 6
                    self.locationx += 6
                    self.char = 4
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Up
                elif event.key == pg.K_w:
                    self.locationy -= 6
                    self.char = 4
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Down Left
                elif event.key == pg.K_s and event.key == pg.K_a:
                    self.locationy += 6
                    self.locationx -= 6
                    self.char = 1
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Down Right
                elif event.key == pg.K_s and event.key == pg.K_d:
                    self.locationy += 6
                    self.locationx += 6
                    self.char = 1
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Down       
                elif event.key == pg.K_s:
                    self.locationy += 6
                    self.char = 1
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Left
                elif event.key == pg.K_a:
                    self.locationx -= 6
                    self.char = 2
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
                #Right 
                elif event.key == pg.K_d:
                    self.locationx += 6
                    self.char = 3
                    if self.chariable < 5:
                        self.charde = 1
                    elif self.chariable > 15:
                        self.charde = 3
                    elif self.chariable > 10:
                        self.charde = 2
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: reguarding character movement. Diagonal?

Postby Mekire » Wed Oct 16, 2013 11:24 pm

You actually need to handle those keys in a completely different way. You should be making use of
Code: Select all
pygame.keys.get_pressed()
rather than trying to catch those keys in your event loop. Using
Code: Select all
pygame.key.set_repeat
to handle continuous button presses can be useful in some cases, but in your case it is probably not what you want.

-Mek

Edit:
Take a look at the example here, and see if you can grasp the method of it:
https://github.com/Mekire/meks-pygame-samples/blob/master/eight_dir_move.py
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: reguarding character movement. Diagonal?

Postby spamynator_1 » Thu Oct 17, 2013 2:02 pm

ok i threw together some crap quickly that basically translated what i have into this better format.

Code: Select all
import sys
import os
import pygame as pg
import pygame.mixer
import json
from pygame.locals import *

SCREEN_SIZE = SCREEN_WIDTH,SCREEN_HEIGHT = (1024,768)

RED = (255,0,0)
ORANGE = (255,127,0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
INDIGO = (75,0,130)
VIOLET = (143,0,255)

Q = 1#OPEN VARIABLE

DIRECT_DICT = {pg.K_a : (-1, 0),
               pg.K_d : ( 1, 0),
               pg.K_w : ( 0,-1),
               pg.K_s : ( 0, 1)}

clock = pg.time.Clock()

class Player(object):
    def __init__(self,rect,speed):
        self.rect = pg.Rect(rect)
        self.speed = speed
        self.image = self.image()
        self.char
        self.chariable
        self.charde
        self.locationx = 200
        self.locationy = 200
        self.direction = "DOWN"

    def direction(self):
        #W
        if DIRECT_DICT[key][0,-1]:
            self.direction = "UP"
            self.chariable += 1
        #S
        elif DIRECT_DICT[key][0, 1]:
            self.direction = "DOWN"
            self.chariable += 1
        #A
        elif DIRECT_DICT[key][-1]:
            self.direction = "LEFT"
            self.chariable += 1
        #D
        elif DIRECT_DICT[key][1]:
            self.direction = "RIGHT"
            self.chariable += 1
       
    def image(self):
        if self.direction == "UP":
            self.char = 1
        elif self.direction == "DOWN":
            self.char = 2
        elif self.direction == "LEFT":
            self.char = 3
        elif self.direction == "RIGHT":
            self.char = 4

    def display_image(self):
        if self.char == 1:
            if self.chariable < 5:
                self.screen.blit(CHAR1,(self.rect.x,self.rect.y))
            elif self.chariable > 15:
                self.screen.blit(CHAR12,(self.rect.x,self.rect.y))
            elif self.chariable > 10:
                self.screen.blit(CHAR13,(self.rect.x,self.rect.y))
               
        elif self.char == 2:
            if self.chariable < 5:
                self.screen.blit(CHAR2,(self.rect.x,self.rect.y))
            elif self.chariable > 15:
                self.screen.blit(CHAR22,(self.rect.x,self.rect.y))
            elif self.chariable > 10:
                self.screen.blit(CHAR23,(self.rect.x,self.rect.y))
               
        elif self.char == 3:
            if self.chariable < 5:
                self.screen.blit(CHAR3,(self.rect.x,self.rect.y))
            elif self.chariable > 15:
                self.screen.blit(CHAR32,(self.rect.x,self.rect.y))
            elif self.chariable > 10:
                self.screen.blit(CHAR33,(self.rect.x,self.rect.y))
               
        elif self.char == 4:
            if self.chariable < 5:
                self.screen.blit(CHAR4,(self.rect.x,self.rect.y))
            elif self.chariable > 15:
                self.screen.blit(CHAR42,(self.rect.x,self.rect.y))
            elif self.chariable > 10:
                self.screen.blit(CHAR43,(self.rect.x,self.rect.y))
               
    def update(self,surface,keys):
        for key in DIRECT_DICT:
            if keys[key]:
                self.rect.x += DIRECT_DICT[key][0]*self.speed
                self.rect.y += DIRECT_DICT[key][1]*self.speed
            self.rec.clamp_ip(surface.get_rect())
            surface.blit(self.image,self.rect)
   
class Control(object):
    def __init__(self):
        self.screen = pg.display.get_surface()
        self.done = False
        self.state = "START"
        #Save File Shit
        self.mapid = (1)
        self.items = (0,1)
        self.surface
        self.clock = pg.time.Clock()
        self.fps = 60
        self.enter_coords = [0,400]
        self.keys = pg.key.get_pressed()
       
    def event_loop(self):
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

            elif self.state == "MENU":
                if event.type == pg.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if BUTTON_NEW_rect.collidepoint(event.pos):
                            self.state = "PLAY"                           
                        elif BUTTON_CONTINUE_rect.collidepoint(event.pos):
                            run_it.load

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN and self.state == "START":
                    self.state = "MENU"

    def surface(self):
        global START_PAGEq #NEEDS REVIEW
        START_PAGEq = pg.image.load(os.path.join('images', 'startpageq.png'))
        self.surface = pg.Surface.convert(START_PAGEq)

    def save(self):
        if self.state == "PLAY":
            print("Saving")
            savex = open('corn.dik','w+')
            savex.write(str(self.locationx))
           
            savey = open('stuffing.dik','w+')
            savey.write(str(self.locationy))
           
            savei = open('turkey.dik','w+')
            savei.write(str(self.items))
           
            savem = open('gravy.dik','w+')
            savem.write(str(self.mapid))

    def load(self):
        if self.state == "CONTINUE":
            print("Loading")
            osavex = open('corn.dik','r')
            self.locationx = osavex.read()

            osavey = open('stuffing.dik','r')
            self.locationy = osavey.read()

            osavei = open('turkey.dik','r')
            self.items = osavei.read()

            osavem = open('gravy.dik','r')
            self.mapid = osavem.read()
            self.state = "PLAY"
                       
    def main_loop(self):
        while not self.done:
            self.event_loop()
            self.update()
            pg.display.update()
            self.clock.tick(self.fps)
            Player()
            pg.display.update()
            self.clock.tick(self.fps)

    def update(self):
        global BUTTON_NEW
        global BUTTON_NEW_rect
        global BUTTON_CONTINUE
        global BUTTON_CONTINUE_rect
        if self.state == "START":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(PRESS_ENTER,self.enter_coords)
            self.enter_coords[0] = (self.enter_coords[0]+2)
           
        elif self.state == "MENU":
            self.screen.blit(START_PAGE,(0,0))
            self.screen.blit(BUTTON_NEW, BUTTON_NEW_rect)
            self.screen.blit(BUTTON_CONTINUE, BUTTON_CONTINUE_rect)
           
        elif self.state == "PLAY":
            run_it.save()
            self.screen.blit(self.surface,(0,0))
       
if __name__ == "__main__":
    os.environ["SDL_VIDEO_CENTERED"] = '1'
    pg.init()
    pg.display.set_mode(SCREEN_SIZE)
    pg.key.set_repeat(1,50)

    START_PAGE = pg.image.load(os.path.join('images', 'startpage.png'))
   
    #start of button shit
    BUTTON_NEW = pg.image.load(os.path.join('images', 'buttonnew.png'))
    BUTTON_NEW_rect = BUTTON_NEW.get_rect(center=(200,500))
    BUTTON_CONTINUE = pg.image.load(os.path.join('images', 'buttoncontinue.png'))
    BUTTON_CONTINUE_rect = BUTTON_CONTINUE.get_rect(center=(824,500))
    #end of button shit
   
    mx,my = pg.mouse.get_pos()

    #Character Images load(lol)
    CHAR1 = pg.image.load(os.path.join('images', 'char1.png'))
    CHAR12 = pg.image.load(os.path.join('images', 'char1-2.png'))
    CHAR13 = pg.image.load(os.path.join('images', 'char1-3.png'))
    CHAR2 = pg.image.load(os.path.join('images', 'char2.png'))
    CHAR22 = pg.image.load(os.path.join('images', 'char2-2.png'))
    CHAR23 = pg.image.load(os.path.join('images', 'char2-3.png'))
    CHAR3 = pg.image.load(os.path.join('images', 'char3.png'))
    CHAR32 = pg.image.load(os.path.join('images', 'char3-2.png'))
    CHAR33 = pg.image.load(os.path.join('images', 'char3-3.png'))
    CHAR4 = pg.image.load(os.path.join('images', 'char4.png'))
    CHAR42 = pg.image.load(os.path.join('images', 'char4-2.png'))
    CHAR43 = pg.image.load(os.path.join('images', 'char4-3.png'))
   
    PRESS_ENTER = pg.image.load(os.path.join('images', 'pressenter.png'))
    MENU_SOUND = pg.mixer.Sound(os.path.join('sounds', 'startsound.wav'))
    BUTTON_NOISE = pg.mixer.Sound(os.path.join('sounds', 'buttonnoise.wav'))
    MENU_SOUND.play
   
    run_it = Control()
    run_it.surface()
    run_it.main_loop()
    pg.quit()
    sys.exit()


this returns the error:
Code: Select all
Traceback (most recent call last):
  File "C:\Users\chris\Google Drive\Game\Game Files\The Quest of Bruce (Player class).py", line 243, in <module>
    run_it.main_loop()
  File "C:\Users\chris\Google Drive\Game\Game Files\The Quest of Bruce (Player class).py", line 182, in main_loop
    Player()
TypeError: __init__() takes exactly 3 arguments (1 given)



also i am unclear on weather or not i did some of this correctly, such as my use of self.rect.x and self.rect.y. Thankyou
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: reguarding character movement. Diagonal?

Postby Mekire » Thu Oct 17, 2013 2:59 pm

spamynator_1 wrote:ok i threw together some crap quickly that basically translated what i have into this better format.

I'm sorry, but effort begets effort. Go back and look at the sample code I linked to in detail. If you don't understand every single line of it, then you are not ready to "[throw] together some crap." The exception you show in your previous post is illustrating that there is something fairly fundamental about classes that you are misunderstanding.

I'm sure this sounds harsh, but really; go back through the sample. If there is something you can't understand, ask about that. Forget about making your character change image or animate until you understand basic movement.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: reguarding character movement. Diagonal?

Postby spamynator_1 » Thu Oct 17, 2013 5:45 pm

I already made the animations work perfectly, the reason this thread existed is because i wanted to know how to make the character go diagonally, you gave me this example which i am trying to interperate. I wouldnt be even as far as i am if i completely didnt understand classes. I post on here to get help, if i knew perfectly how to code, i wouldnt be on here.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: reguarding character movement. Diagonal?

Postby dboxall123 » Thu Oct 17, 2013 7:21 pm

I wouldnt be even as far as i am if i completely didnt understand classes

Apparantly not. Look at the traceback. It says that your Player() class takes 3 arguments. So when you write
Code: Select all
Player()

in the main loop, you're not passing it enough arguments, are you?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: reguarding character movement. Diagonal?

Postby Mekire » Thu Oct 17, 2013 10:57 pm

spamynator_1 wrote:you gave me this example which i am trying to interperate.

If you are trying to interpret it, then ask about the parts of it you currently don't understand. What it looks like you have done so far is just copy and paste bits of it into your program and hoped they work. The __init__ method of a class dictates what arguments a class instance needs to be passed when you create it; you aren't passing the right ones and therefore the exception. Aside from this, you shouldn't be trying to create the instance in your main loop to begin with. Right now you are attempting to recreate the Player every time through the loop. What we want is to create the Player once, and then update it every time through the loop.

Get rid of every feature and work on a version with JUST movement. Take out the animation; take out the saving and loading; get this working smoothly. Then add the features back once you understand.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests