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 - import pygame
-
from pygame.locals import *
-
pygame.init()
-
screen=pygame.display.set_mode((900,900))
-
-
-
class Hero(pygame.sprite.Sprite):
-
def __init__(self):
-
pygame.sprite.Sprite.__init__(self)
-
self.image=pygame.image.load('C:/Python24/r3.PNG')
-
self.image=self.image.convert()
-
self.rect=self.image.get_rect()
-
self.rect.topleft=0,500
-
self.a=screen.get_rect()
-
def forward(self):
-
a=1
-
def back(self):
-
a=2
-
def original(self):
-
self.a.topleft=0,500
-
def moveup(self):
-
a=3
-
def movedown(self):
-
a=4
-
def update(self):
-
if a==0:
-
self.original()
-
if a==1:
-
if self.rect.left<=self.a.right :
-
self.rect.move_ip(5,0)
-
else:
-
self.original()
-
if a==2:
-
if self.rect.left!=0:
-
self.rect.move_ip(-5,0)
-
else:
-
self.original()
-
if a==3:
-
if self.rect.top!=self.a.top:
-
self.rect.move_ip(0,5)
-
else:
-
self.rect.move_ip(0,-5)
-
if a==4:
-
if self.rect.bottom!=0:
-
self.rect.move_ip(0,-5)
-
else:
-
self.rect.move_ip(0,5)
-
class Villian(pygame.sprite.Sprite):
-
def __init__(self):
-
pygame.sprite.Sprite.__init__(self)
-
self.image=pygame.image.load('C:/Python24/toba-pyweek3/toba-pyweek3/data/cards/1.PNG')
-
self.rect=self.image.convert()
-
self.rect=self.image.get_rect()
-
self.rect.topleft=400,500
-
a=0
-
def forward(self):
-
self.rect.move_ip(0,5)
-
-
def update(self):
-
if self.rect.top!=900:
-
self.forward()
-
else:
-
self.original()
-
def original(self):
-
self.rect.topleft=400,500
-
-
imig=pygame.image.load('C:/Python24/Blue hills.GIF')
-
background=pygame.Surface(screen.get_size())
-
background=background.convert()
-
background.blit(imig,(0,0))
-
screen.blit(background,(0,0))
-
hero=Hero()
-
villian=Villian()
-
allsprites=pygame.sprite.RenderPlain((hero,villian))
-
allsprites.draw(screen)
-
pygame.display.flip()
-
-
clock=pygame.time.Clock()
-
while True:
-
clock.tick(60)
-
for event in pygame.event.get():
-
if event.type==KEYDOWN and event.key==K_UP:
-
hero.moveup()
-
if event.type==KEYDOWN and event.key==K_DOWN:
-
hero.movedown()
-
if event.type==KEYDOWN and event.key==K_RIGHT:
-
hero.forward()
-
if event.type==KEYDOWN and event.key==K_LEFT:
-
hero.back()
-
if event.type==QUIT:
-
pygame.quit()
-
raise SystemExit()
-
allsprites.update()
-
screen.blit(background,(0,0))
-
allsprites.draw(screen)
-
pygame.display.flip()
11 3583
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 -
clock=pygame.time.Clock()
-
while True:
-
clock.tick(60)
-
for event in pygame.event.get():
-
if event.type==KEYDOWN and event.key==K_UP:
-
hero.moveup()
-
if event.type==KEYDOWN and event.key==K_DOWN:
-
hero.movedown()
-
if event.type==KEYDOWN and event.key==K_RIGHT:
-
hero.forward()
-
if event.type==KEYDOWN and event.key==K_LEFT:
-
hero.back()
-
if event.type==QUIT:
-
pygame.quit()
-
raise SystemExit()
-
allsprites.update()
-
screen.blit(background,(0,0))
-
allsprites.draw(screen)
-
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: -
while True:
-
clock.tick(60)
-
for event in pygame.event.get():
-
if event.type==KEYDOWN and event.key==K_UP:
-
hero.moveup()
-
if event.type==KEYDOWN and event.key==K_DOWN:
-
hero.movedown()
-
if event.type==KEYDOWN and event.key==K_RIGHT:
-
hero.forward()
-
if event.type==KEYDOWN and event.key==K_LEFT:
-
hero.back()
-
if event.type==QUIT:
-
pygame.quit()
-
raise SystemExit()
-
allsprites.update()
-
screen.blit(background,(0,0))
-
allsprites.draw(screen)
-
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: -
while True:
-
clock.tick(60)
-
for event in pygame.event.get():
-
if event.type==KEYDOWN and event.key==K_UP:
-
hero.moveup()
-
if event.type==KEYDOWN and event.key==K_DOWN:
-
hero.movedown()
-
if event.type==KEYDOWN and event.key==K_RIGHT:
-
hero.forward()
-
if event.type==KEYDOWN and event.key==K_LEFT:
-
hero.back()
-
if event.type==QUIT:
-
pygame.quit()
-
raise SystemExit()
-
allsprites.update()
-
screen.blit(background,(0,0))
-
allsprites.draw(screen)
-
pygame.display.flip()
-
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?
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?
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
I did not think of that thats actually brilliant and a lot easier the creating little seperate images.Thanks
No problem. (stupid limit)
No problem. (stupid limit)
what do you mean by stupid limit.
what do you mean by stupid limit.
Lol, you need to enter at least 20 characters in a message. Used to be 10.
Lol, you need to enter at least 20 characters in a message. Used to be 10.
Oh,lol.Thanks again.
Lol, you need to enter at least 20 characters in a message. Used to be 10.
Or !
Sign in to post your reply or Sign up for a free account.
Similar topics
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",...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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)
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
| |