pygame.key.set_repeat(1,50)?

pygame.key.set_repeat(1,50)?

Postby spamynator_1 » Fri Aug 02, 2013 8:03 pm

im trying to design some sort of system of making it so a character slides across a screen when you hold a key.
the problem i am having is i cannot get the pg.key.set_repeat() to work at all, i have googled everywhere and there is nothing useful in two full pages of google links. butts.
my code is
Code: Select all
import sys
import os
import pygame as pg
from pygame.locals import *

size = width, height = 640,480
screen = pg.display.set_mode(size)

map1 = pg.image.load('map.png')
character = pg.image.load('character.png')

x,y = (200,200)
dong = 0
pg.key.set_repeat(1,50)
while dong == 0:
    for event in pg.event.get():
        key_pressed = pg.key.get_pressed()
        screen.blit(map1, (0,0))
        screen.blit(character, (x,y))
        pg.display.update()
        pg.key.set_repeat(1, 10)
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_w:
                y -= 3
            elif event.key == pg.K_s:
                y += 3
            elif event.key == pg.K_a:
                x -= 3
            elif event.key == pg.K_d:
                x += 3

spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: pygame.key.set_repeat(1,50)?

Postby Mekire » Sat Aug 03, 2013 3:29 am

This can be done with key.set_repeat, but it isn't usually the right tool for the job. The main problem with your code is that you are trying to put all the processing inside the event loop. The only things that should be in your event loop are things that pertain to processing events.

This works using set_repeat:
Code: Select all
import sys
import os
import pygame as pg

size = width, height = 640,480
screen = pg.display.set_mode(size)
clock = pg.time.Clock()

character = pg.Surface((50,50)).convert()
character.fill((255,0,0))

x,y = (200,200)
done = False
pg.key.set_repeat(1,50)
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_w:
                y -= 3
            elif event.key == pg.K_s:
                y += 3
            elif event.key == pg.K_a:
                x -= 3
            elif event.key == pg.K_d:
                x += 3
    screen.fill(0)
    screen.blit(character, (x,y))
    pg.display.update()
    clock.tick(60)
pg.quit()
sys.exit()

I however suggest you reexamine your approach and do the way you can see here:
https://github.com/Mekire/meks-pygame-samples/blob/master/eight_dir_move.py

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

Re: pygame.key.set_repeat(1,50)?

Postby spamynator_1 » Sat Aug 03, 2013 4:20 am

Thank you, and the way i write code at this point is to write it in simple complete innefficent way than to convert it to respectable oop format after i know what i am doing. This code worked thank you
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: pygame.key.set_repeat(1,50)?

Postby Mekire » Sat Aug 03, 2013 4:32 am

I wasn't referring to OOP vs non-OOP in this case. set_repeat() just isn't appropriate for a lot of circumstances unless you want every key in your game to behave that way. Better to use pygame.key.get_pressed() usually coupled with a dictionary to get the effect in a more generalizable way.

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


Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 1 guest