Pygame Help


zRichi

Member
Joined
Mar 28, 2009
Messages
122
http://rene.f0o.com/mywiki/LectureThree#head-d4e5cda1e9eb26a049b47d429144faba112006dc

Going through this.
Just doing the exercises and trying to figure out what I need to add to my current code to quit on any key press

import pygame, sys,os
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Monkey Fever')
screen = pygame.display.get_surface()

monkey_head_file_name = os.path.join("data","chimp.bmp")

monkey_surface = pygame.image.load(monkey_head_file_name)

screen.blit(monkey_surface, (0,0))
pygame.display.flip()

def input(events):
for event in events:
if event.type == QUIT:
sys.exit(0)
else:
print event

while True:
input(pygame.event.get())
 
MAnaged to get it to quit with this code

while 1:
for event in pygame.event.get():
print "event"
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
sys.exit()

Still not sure how to quit on any key though.
 
Well, look at this line:
Code:
if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
You test first to see if the event is a keypress. Then you test to see what key is being pressed. All you have to do is remove that second test, as such:
Code:
if event.type == pygame.KEYDOWN:
Therefore, your statement will be true for any keypress.

PS: code tags are your friend, especially with Python.
 
I don't understand where I'm going wrong. I get a line indent error on line 15 and wherever I move it spaces or tabs it won't go away which is causing my sprite class to not work.

Any help is greatly appreciated

Code:
import pygame, sys,os
from pygame.locals import * 

class StickMan(pygame.sprite.Sprite):

   def __init__(self, position):

      pygame.sprite.Sprite.__init__(self)

      # Save a copy of the screen's rectangle
      self.screen = pygame.display.get_surface().get_rect()

      # Create a variable to store the previous position of the
sprite
    self.old = (0, 0, 0, 0)

      self.image = pygame.image.load('stickMan.gif').convert()
      self.rect = self.image.get_rect()
      self.rect.x = position[0]
      self.rect.y = position[1]

   def update(self, amount):

      # Make a copy of the current rectangle for use in erasing
      self.old = self.rect

      # Move the rectangle by the specified amount
      self.rect = self.rect.move(amount)

      # Check to see if we are off the screen
      if self.rect.x < 0:
         self.rect.x = 0
      elif self.rect.x > (self.screen.width - self.rect.width):
         self.rect.x = self.screen.width - self.rect.width
      if self.rect.y < 0:
         self.rect.y = 0
      elif self.rect.y > (self.screen.height - self.rect.height):
         self.rect.y = self.screen.height - self.rect.height
def input(events): 
   for event in events: 
      if event.type == QUIT: 
         sys.exit(0) 
      else: 
         print event 

pygame.init() 
 
screen = pygame.display.set_mode((800, 600)) 
pygame.display.set_caption('Super Ninja Knight Hadouken Quest') 
screen.fill((159, 182, 205))
character = StickMan((screen.get_rect().x, screen.get_rect().y))
screen.blit(character.image, character.rect)
screen = pygame.display.get_surface() 
blank = pygame.Surface((character.rect.width, character.rect.height))
blank.fill((159, 182, 205 ))

 
while True:
            for event in pygame.event.get():
                print "event"
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE :
                    sys.exit
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                     character.update([-10, 0])
                    elif event.key == pygame.K_UP:
                     character.update([0, -10])
                    elif event.key == pygame.K_RIGHT:
                     character.update([10, 0])
                    elif event.key == pygame.K_DOWN:
                     character.update([0, 10])
    # Erase the old position by putting our blank Surface on i
                    screen.blit(blank, character.old)

     # Draw the new position
                    screen.blit(character.image, character.rect)

    # Update ONLY the modified areas of the screen
                    pygame.display.update([character.old, character.rect])
 
Hi,

Code:
      self.screen = pygame.display.get_surface().get_rect()

      # Create a variable to store the previous position of the
sprite
    self.old = (0, 0, 0, 0)

This bit is odd. After copy/pasting your code in my editor, I just needed to fix this. It should be like this:

Code:
      # Save a copy of the screen's rectangle
      self.screen = pygame.display.get_surface().get_rect()

      # Create a variable to store the previous position of the sprite
      self.old = (0, 0, 0, 0)

Apparently, your code is indented with 6 spaces and the "self.old" line is indented with 4.
 
Clop is right. It looks like your comment somehow got split on to two lines, causing the second line to be unindented. Since Python sees that as being the end of the definition of the __init__ function, it gets confused when it sees the next line indented. Also, the line with self.old does not seem to be indented as much as the surrounding block.

As a general tip, if Python has an error on line 15, it's sometimes caused by a mistake made on an earlier line. All your code interacts, and Python doesn't always know what you intended to do; it just gives the error it sees.
 
Back
Top