5 Replies - 770 Views - Last Post: 12 November 2013 - 12:41 AM Rate Topic: -----

#1 #comment  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-November 13

A problem with a pygame project

Posted 06 November 2013 - 07:04 PM

Hello forumers (or whatever this is called nowdays),

I'm doing a project in which I used pygame to design a rather simple snake game, but I've run into a small error:

When I threw the code in an event loop so it was able to be played again after you crashed your worm, the movement options no longer worked, I'm wondering if someone could help me with this problem...

This is what I used for the event loop
def game():
running = True
#Code Here
if worm.crashed:
restart = input("Play Again?(Y/N) ")
if restart == "Y"
game()
else:
print("Thank you for playing")
break


That works perfectly fine

Then we come to the keyboard events/motion, it worked before placing the event loop in but now..

def event(self, event):
#Handles keyboard events
if event.key == pygame.K_UP:
if self.vy == -1: return
self.vx = 0
self.vy = -1
elif event.key == pygame.K_DOWN:
if self.vy == -1: return
self.vx = 0
self.vy = 1
elif event.key == pygame.K_LEFT:
if self.vx == 1: return
self.vx = -1
self.vy = 0
elif event.key == pygame.K_RIGHT:
if self.vx == -1: return
self.vx = 1
self.vy = 0

def move(self):
#moving the worm
self.x+= self.vx
self.y += self.vy

if (self.x, self.y) in self.body:
self.crashed = True

self.body.insert(0, (self.x, self.y))

if (self.grow_to > self.length):
self.length += 1

if len(self.body) > self.length:
self.body.pop() 


Is This A Good Question/Topic? 0
  • +

Replies To: A problem with a pygame project

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: A problem with a pygame project

Posted 07 November 2013 - 07:16 AM

Few things. Recursion (having your game function call itself) is not how you want to accomplish looping your program. Next; you really shouldn't be trying to use the input builtin (or raw_input in python 2.x) with pygame.

If you could post your whole program (taking care that the indentation is preserved when you post it) I will try to take a look.

-Mek
Was This Post Helpful? 0
  • +
  • -

#3 witeboy724  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 21-June 12

Re: A problem with a pygame project

Posted 11 November 2013 - 11:42 AM

Mekire is right, that's not a great way to loop. You have a running=True var so make a game loop that uses
while running==True:

then inside the running loop you can handle the keypresses with something like
 
for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_UP:



(I think putting this pygame.event.get()in the game loop solves the main problem you have with keypresses not working with your current game loop.)

when the worm crashes, set running to False, ask the question using raw_input() (I've not heard a reason not to use this with pygame before), then if play again is the answer set running back to True and call any game variable resets you need and tell the loop run again.

I'd also be happy to look at the full code if that helps more.

This post has been edited by witeboy724: 11 November 2013 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: A problem with a pygame project

Posted 11 November 2013 - 07:43 PM

View Postwiteboy724, on 11 November 2013 - 06:42 PM, said:

ask the question using raw_input() (I've not heard a reason not to use this with pygame before)

Well now you have. You shouldn't do it because it is the wrong thing to do.

The best case scenario leaves the user forced to click over to the console window; give their input; and then click back over to the pygame window. The worst case scenario locks up your game because it has not been able to make calls to the event queue often enough.

The most legitimate reason, however, is the aformentioned it being the wrong thing to do. If the game should restart on a given input, render this message to the screen and add the necessary code to your pygame event loop to process it.

-Mek
Was This Post Helpful? 0
  • +
  • -

#5 witeboy724  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 21-June 12

Re: A problem with a pygame project

Posted 11 November 2013 - 11:07 PM

Alright you make a good point. Even when running pygame I like to print outputs to the console in the background so I wouldn't mind clicking over to type something, but it is pretty sloppy and may freeze something. So check out text rendering, it's pretty simple.
    screen.fill((250, 250, 250))

    font = pygame.font.Font(None, 36)
    text = font.render("Hello There", 1, (10, 10, 10))
    textpos = text.get_rect()
    centerx = screensize[0]/2 - textpos[2]/2
    centery = screensize[1]/2 - textpos[3]/2
    screen.blit(text, (centerx,centery,textpos[2],textpos[3]))


Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: A problem with a pygame project

Posted 12 November 2013 - 12:41 AM

View Postwiteboy724, on 12 November 2013 - 06:07 AM, said:

Alright you make a good point. Even when running pygame I like to print outputs to the console

Don't get me wrong. I have nothing against print statements being used in pygame for testing purposes. This works great and does no harm. From a user perspective however you shouldn't be expecting them to do something they wouldn't normally do. You shouldn't use print statements to substitute as your HUD (for more than just testing purposes); and you shouldn't expect a player to tab over to the console window to enter an input prompt.

-Mek
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1