472,973 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

pygame problems

51
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a bit long but it's straightforward.Please help
Expand|Select|Wrap|Line Numbers
  1. import pygame
  2. from pygame.locals import *
  3. pygame.init()
  4. screen=pygame.display.set_mode((900,900))
  5.  
  6.  
  7. class Hero(pygame.sprite.Sprite):
  8.     def __init__(self):
  9.         pygame.sprite.Sprite.__init__(self)
  10.         self.image=pygame.image.load('C:/Python24/r3.PNG')
  11.         self.image=self.image.convert()
  12.         self.rect=self.image.get_rect()
  13.         self.rect.topleft=0,500
  14.         self.a=screen.get_rect()
  15.     def forward(self):
  16.         a=1
  17.     def back(self):
  18.         a=2
  19.     def original(self):
  20.         self.a.topleft=0,500
  21.     def moveup(self):
  22.         a=3  
  23.     def movedown(self):
  24.         a=4    
  25.     def update(self):
  26.         if a==0:
  27.             self.original()
  28.         if a==1:
  29.             if self.rect.left<=self.a.right :
  30.                 self.rect.move_ip(5,0)
  31.             else:
  32.                 self.original()
  33.         if a==2:
  34.             if self.rect.left!=0:
  35.                 self.rect.move_ip(-5,0)
  36.             else:
  37.                 self.original()
  38.         if a==3:
  39.             if self.rect.top!=self.a.top:
  40.                 self.rect.move_ip(0,5)
  41.             else:
  42.                 self.rect.move_ip(0,-5)
  43.         if a==4:
  44.             if self.rect.bottom!=0:
  45.                 self.rect.move_ip(0,-5)
  46.             else:
  47.                 self.rect.move_ip(0,5)
  48. class Villian(pygame.sprite.Sprite):
  49.     def __init__(self):
  50.         pygame.sprite.Sprite.__init__(self)
  51.         self.image=pygame.image.load('C:/Python24/toba-pyweek3/toba-pyweek3/data/cards/1.PNG')
  52.         self.rect=self.image.convert()
  53.         self.rect=self.image.get_rect()
  54.         self.rect.topleft=400,500
  55.         a=0
  56.     def forward(self):
  57.         self.rect.move_ip(0,5)
  58.  
  59.     def update(self):
  60.         if self.rect.top!=900:
  61.             self.forward()
  62.         else:
  63.             self.original()
  64.     def original(self):
  65.         self.rect.topleft=400,500
  66.  
  67. imig=pygame.image.load('C:/Python24/Blue hills.GIF')
  68. background=pygame.Surface(screen.get_size())
  69. background=background.convert()
  70. background.blit(imig,(0,0))
  71. screen.blit(background,(0,0))
  72. hero=Hero()
  73. villian=Villian()
  74. allsprites=pygame.sprite.RenderPlain((hero,villian))
  75. allsprites.draw(screen)
  76. pygame.display.flip()
  77.  
  78. clock=pygame.time.Clock()
  79. while True:
  80.     clock.tick(60)
  81.     for event in pygame.event.get():
  82.         if event.type==KEYDOWN and event.key==K_UP:
  83.             hero.moveup()
  84.         if event.type==KEYDOWN and event.key==K_DOWN:
  85.             hero.movedown()
  86.         if event.type==KEYDOWN and event.key==K_RIGHT:
  87.             hero.forward()
  88.         if event.type==KEYDOWN and event.key==K_LEFT:
  89.             hero.back()
  90.         if event.type==QUIT:
  91.             pygame.quit()
  92.             raise SystemExit()
  93. allsprites.update()
  94. screen.blit(background,(0,0))
  95. allsprites.draw(screen)
  96. pygame.display.flip()
May 20 '07 #1
11 3583
ilikepython
844 Expert 512MB
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a bit long but it's straightforward.Please help
Expand|Select|Wrap|Line Numbers
  1. clock=pygame.time.Clock()
  2. while True:
  3.     clock.tick(60)
  4.     for event in pygame.event.get():
  5.         if event.type==KEYDOWN and event.key==K_UP:
  6.             hero.moveup()
  7.         if event.type==KEYDOWN and event.key==K_DOWN:
  8.             hero.movedown()
  9.         if event.type==KEYDOWN and event.key==K_RIGHT:
  10.             hero.forward()
  11.         if event.type==KEYDOWN and event.key==K_LEFT:
  12.             hero.back()
  13.         if event.type==QUIT:
  14.             pygame.quit()
  15.             raise SystemExit()
  16. allsprites.update()
  17. screen.blit(background,(0,0))
  18. allsprites.draw(screen)
  19. pygame.display.flip()
Hi, I'm not sure you're indenting is correct. In your while loop you don't draw anything or update the screen. You should put the updating, drawing, and blitting in every frame. The way you have it, it is only going to do something when you close the window and only once.Your while loop should look like this:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     clock.tick(60)
  3.     for event in pygame.event.get():
  4.         if event.type==KEYDOWN and event.key==K_UP:
  5.             hero.moveup()
  6.         if event.type==KEYDOWN and event.key==K_DOWN:
  7.             hero.movedown()
  8.         if event.type==KEYDOWN and event.key==K_RIGHT:
  9.             hero.forward()
  10.         if event.type==KEYDOWN and event.key==K_LEFT:
  11.             hero.back()
  12.         if event.type==QUIT:
  13.             pygame.quit()
  14.             raise SystemExit()
  15.     allsprites.update()
  16.     screen.blit(background,(0,0))
  17.     allsprites.draw(screen)
  18.     pygame.display.flip()
  19.  
May 20 '07 #2
dynamo
51
Hi, I'm not sure you're indenting is correct. In your while loop you don't draw anything or update the screen. You should put the updating, drawing, and blitting in every frame. The way you have it, it is only going to do something when you close the window and only once.Your while loop should look like this:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     clock.tick(60)
  3.     for event in pygame.event.get():
  4.         if event.type==KEYDOWN and event.key==K_UP:
  5.             hero.moveup()
  6.         if event.type==KEYDOWN and event.key==K_DOWN:
  7.             hero.movedown()
  8.         if event.type==KEYDOWN and event.key==K_RIGHT:
  9.             hero.forward()
  10.         if event.type==KEYDOWN and event.key==K_LEFT:
  11.             hero.back()
  12.         if event.type==QUIT:
  13.             pygame.quit()
  14.             raise SystemExit()
  15.     allsprites.update()
  16.     screen.blit(background,(0,0))
  17.     allsprites.draw(screen)
  18.     pygame.display.flip()
  19.  
Thanks for replying.I think what your saying is i should include the blitting and drawing into the while loop,makes sense.How did you get to this level in pygame,any good tutorials?
May 21 '07 #3
ilikepython
844 Expert 512MB
Thanks for replying.I think what your saying is i should include the blitting and drawing into the while loop,makes sense.How did you get to this level in pygame,any good tutorials?
The pygame website has some good tutorials and is a good reference.
Here are some links to help you out:
http://www.pygame.org/docs/
http://www.pygame.org/wiki/tutorials
http://www.pygame.org/docs/tut/chimp...ineByLine.html
Also, you could download some sample games from the website so you can see how they are written and learn from them.
May 21 '07 #4
dynamo
51
The pygame website has some good tutorials and is a good reference.
Here are some links to help you out:
http://www.pygame.org/docs/
http://www.pygame.org/wiki/tutorials
http://www.pygame.org/docs/tut/chimp...ineByLine.html
Also, you could download some sample games from the website so you can see how they are written and learn from them.
Thanks for the links.Do you you know how to make a part of your image move.For instance to make your hero kick or to shoot.
May 22 '07 #5
ilikepython
844 Expert 512MB
Thanks for the links.Do you you know how to make a part of your image move.For instance to make your hero kick or to shoot.
Do you mean animation? You can't make part of an image move, you need to make seperate pygame.Surfaces. If you want to do animation like walking, running, or shooting you can have a sprite sheet all in one BMP file showing all the possible frames of animation of a charatcer. You can blit the part of the image that you need for the current frame to represent an animation. Is that what you need?
May 23 '07 #6
dynamo
51
Do you mean animation? You can't make part of an image move, you need to make seperate pygame.Surfaces. If you want to do animation like walking, running, or shooting you can have a sprite sheet all in one BMP file showing all the possible frames of animation of a charatcer. You can blit the part of the image that you need for the current frame to represent an animation. Is that what you need?
I did not think of that thats actually brilliant and a lot easier the creating little seperate images.Thanks
May 26 '07 #7
ilikepython
844 Expert 512MB
I did not think of that thats actually brilliant and a lot easier the creating little seperate images.Thanks
No problem. (stupid limit)
May 28 '07 #8
dynamo
51
No problem. (stupid limit)
what do you mean by stupid limit.
May 30 '07 #9
ilikepython
844 Expert 512MB
what do you mean by stupid limit.
Lol, you need to enter at least 20 characters in a message. Used to be 10.
May 30 '07 #10
dynamo
51
Lol, you need to enter at least 20 characters in a message. Used to be 10.
Oh,lol.Thanks again.
May 31 '07 #11
bartonc
6,596 Expert 4TB
Lol, you need to enter at least 20 characters in a message. Used to be 10.
Or !
May 31 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Marian Aldenhövel | last post by:
Hi, I am trying to make pygame play music on windows. This simple program: import pygame,time pygame.init() print "Mixer settings", pygame.mixer.get_init() print "Mixer channels",...
3
by: Tim Knauf | last post by:
Hi everyone, I'm glad to have found this list. I've written a small script for my own use which, amongst other things, captures mouse click information from a window containing an image. I used...
2
by: Brent W. Hughes | last post by:
I'm just starting to learn pygame. I write what I think is just about the simplest program that should display a window and then quit. #----------------------------------------------- import sys...
1
by: kjm | last post by:
Hi everyone, I have recently acquired a Logitech Rumble pad to use as an input device. I have been having trouble getting the event que to respond that a button or hat arrow has been pressed. ...
2
by: DK | last post by:
I'm somewhat new to Python but not to programming. I just want to know if it's possible to have a SINGLE wxPython frame containing two sections where one will contain widgets and the other will...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
1
by: joshuabraham | last post by:
os version =windows xp hi guys i'm having problems using pygame the following code just does not seem to work. clock=pygame.time.Clock() while True: sound1.play() clock.tick(60) ...
3
by: globalrev | last post by:
im doing this : http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/ and when closing the program the window stays up and doesnt respond. i tried adding...
11
by: globalrev | last post by:
http://www.pygame.org/docs/ref/mixer.html import pygame #pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=3072) //it complained abiout words= so i guess its only the nbrs...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.