LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2003, 03:20 PM   #1
jarin scott
Member
 
Registered: May 2003
Distribution: LFS 6.0
Posts: 67

Rep: Reputation: 15
Help. Attacked by a Python


I am attempting to write a simple program to move a image across a tile background. I got it working by making everything it's own class, but it was too much typing. I read and reread the moveit program and the docs for it and tried to splice it into my own. I get either a background and no player or i get a background with an error:

Traceback (most recent call last):
File "F:\PYGA\backgroundworks.pyw", line 78, in ?
if __name__ == '__main__': main()
File "F:\PYGA\backgroundworks.pyw", line 75, in main
heros.draw(screen)
File "D:\PROGRA~1\PYTHON~1.2\Lib\site-packages\pygame\sprite.py", line 459, in draw
surface_blit(s.image, s.rect)
TypeError: invalid destination position for blit

Here's the program.

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

global playerpos
playerpos = 2

#Taken from moveit.py, using it because it works
def load_image(name):
path = os.path.join('data', name)
return pygame.image.load(path).convert()

#my player class
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = self.rect = load_image('player.bmp')
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.playerpos = 2
playerpos = self.playerpos
#self.playerpos = 2
self.topleft = 0 * 32, 0

def update(self):
self.topleft = 0 * 32, 0

def left(self):
if self.playerpos >= 1:
self.playerpos = self.playerpos - 1
self.update()

def right(self):
if self.playerpos <= 2:
self.playerpos = self.playerpos + 1
self.update()

def main():
pygame.init()
screen = pygame.display.set_mode((4 * 32, 32))
pygame.display.set_caption('Hero')
#My images to load for background
Terr1 = load_image('terr1.bmp')
Terr2 = load_image('terr2.bmp')
Terr3 = load_image('terr3.bmp')
#Create an array for the background, easy loading
background = [Terr1, Terr2, Terr3]
for i in range(3):
screen.blit(background[i], (i*32, 0))

pygame.display.flip()

clock = pygame.time.Clock()
hero = Player()
heros = pygame.sprite.RenderPlain((hero))
pygame.display.flip()


while 1:
clock.tick(60)
#event sequence
for event in pygame.event.get():

if event.type == QUIT: #quit prog
return
elif event.type == KEYDOWN and event.key == K_ESCAPE: #quit prog
return
elif event.type == KEYDOWN and event.key == K_LEFT:
hero.left()
elif event.type == KEYDOWN and event.key == K_RIGHT:
hero.right()

hero.update()
screen.blit(background[playerpos], (playerpos*32,0))
heros.draw(screen)
pygame.display.flip()

if __name__ == '__main__': main()

Thanks for the help,
Jarin Scott
 
Old 06-20-2003, 12:16 PM   #2
jarin scott
Member
 
Registered: May 2003
Distribution: LFS 6.0
Posts: 67

Original Poster
Rep: Reputation: 15
I removed the Player class and everything that dealt with it and was able to get the background. I slowly added the Player class and ran the program with no errors, and then added the heor.update() and heros.draw(screen) after blitting the screen. I get the following error:
Traceback (most recent call last):
File "D:\WINDOWS.000\Desktop\pygame short\background_w_player.py", line 71, in ?
if __name__ == '__main__': main()
File "D:\WINDOWS.000\Desktop\pygame short\background_w_player.py", line 67, in main
hero.update()
File "D:\PROGRA~1\PYTHON~1.2\Lib\site-packages\pygame\sprite.py", line 323, in update
s.update()
File "D:\WINDOWS.000\Desktop\pygame short\background_w_player.py", line 28, in update
self.rect.topleft = 32, 0
TypeError: 'Surface' object has only read-only attributes (assign to .topleft)

Removing the hero.update() gives me the error posted in my first post. One more thing, I am using whitespace it just wasn't posted.

Jarin Scott
 
Old 06-21-2003, 10:31 AM   #3
jarin scott
Member
 
Registered: May 2003
Distribution: LFS 6.0
Posts: 67

Original Poster
Rep: Reputation: 15
Does anyone have any idea about what is wrong with the above. If it will help I can post the orignal program that works but it is long (about 114 lines). If you know any sites to visit for information it would be appreicated. I read the tutorial sfor pugame and python and am understanding the stuff but I need to use them to grasp them hence this program. It may not be much but it's a start to learning something new and different.

Jarin Scott
 
Old 06-21-2003, 05:56 PM   #4
m32
Member
 
Registered: Feb 2003
Location: Cedar Park, TX
Distribution: Slackware 9.0, RH 9
Posts: 39

Rep: Reputation: 15
Howdy,

Don't know the answer to your question, but this is a good place that I've gotten help with my fledgling python programs:

Comp.lang.python

It takes a while to see your post, and for the replies to come in, but the literal experts on the language post there frequently, and there's plenty of help to be found.

I haven't done anything serious with classes yet, or with graphics, so sorry I'm not much help. Hopefully, that link will help, though.


Python Language Website also has plenty of links for "newbies" such as us.

hth.

Last edited by m32; 06-21-2003 at 05:57 PM.
 
Old 06-22-2003, 11:07 AM   #5
jarin scott
Member
 
Registered: May 2003
Distribution: LFS 6.0
Posts: 67

Original Poster
Rep: Reputation: 15
Thanks for the reply. I got the program working but I still have no idea why it doesn't work. I added another def to load images using a short form of the orignal format and used it to load the background.

Jarin Scott
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
netstat - Am I being attacked? techrolla Linux - Security 4 08-02-2007 06:58 AM
Virus Attacked! matchgirl Linux - Security 7 03-06-2006 07:39 AM
I think I've been attacked! smacky Linux - Security 7 10-21-2003 02:39 AM
Have I been attacked? tangle Linux - Security 6 08-03-2003 08:33 PM
Being Attacked? andy18 Linux - Security 1 05-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration