Hello,
I know this is a very simple problem, but I am about to give up... Why couldn't I get the control keys do their job? Maybe someone whose eyes are more rested than mine can tell me. I will appreciate any help.
I am dealing with pygame, and I have two images loaded on the screen when running the program. However, what I want the first image in the first sprite to do is moving around with controlled keys by me, such as, move up, left, down, and right.
By the way, needless to say, I am new to pygame. It actually is so much fun, especially when things are smooth.
Here is my code:
#Coding starts here
"""The elder moves when pressed the control keys & when collides
with the scared guy, makes him disappear
Modified on September 20th, 2009"""
import pygame
pygame.init()
screen = pygame.display.set_mode((960, 720)) #Resolution has been changed by me
class Elderly(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("ELDERLY1.png")
self.rect = self.image.get_rect()
self.rect.centerx = 100
self.rect.centery = 150
def update(self):
self.rect.center = pygame.image.load()
#if self.rect.right > screen.get_width():
# self.rect.left = 0
class Ohno(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("OHNO2.png")
self.rect = self.image.get_rect()
self.rect.centerx = 800
self.rect.centery = 450
def update(self):
self.rect.center = pygame.image.load()
#if self.rect.right > screen.get_width():
# self.rect.left = 0
def main():
#Entities for the background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((238, 232, 170)) #
screen.blit(background, (0, 0))
elderly = Elderly()
ohno = Ohno()
allSprites = pygame.sprite.Group(elderly, ohno)
clock =pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
#Starting from here, modifying the control keys
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
elderly.moveup()
if event.key == pygame.K_DOWN:
elderly.movedown()
if event.key == pygame.K_RIGHT:
elderly.moveright()
if event.key == pygame.K.LEFT:
elderly.moveleft()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
elderly.movepos = [0,0]
elderly.state = "still"
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
#Coding ends here
I have been up for hours and hours and tried some searching. I also modified the code a little bit. When I hit the control keys, it used to shut itself down. At least, it doesn't do it anymore, but I am getting some attribute errors.
Thank you very much in advance.