Silverhawk11011
Still Fresh
- Joined
- Jul 27, 2011
- Messages
- 11
Hello everyone. I am currently working on an ARPG style game using some borrowed sprites . Currently I am working on my main character class. I have added the sprite, made him an animated sprite and added some control. He has a specific set animation for each direction he walks (north, east, south, and west). I have used a sprite sheet for the animations and bound the keys to his directional animation. What I am trying to do is have him actually move across the screen in the given direction and stop when the key is no longer held down. This is giving me some trouble. If anyone could give me some advice I would really appreciate it. Below is the code I have so far.
Code:
import sys, pygame, random
sys.path.append('./modules')
from Zone1 import *
pygame.init()
screen = pygame.display.set_mode((800, 600))
class ZeldaC(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.image = pygame.image.load("sprites\Dstnd.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.rect.center = (400, 300)
self.walkEast()
self.walkWest()
self.walkNorth()
self.walkSouth()
self.frame = 0
self.delay = 4
self.pause = 0
self.speed = 0
self.dir = 0
self.dx = 0
self.dy = 0
self.x = self.rect.centerx
self.y = self.rect.centery
def chkDir(self):
if self.dir == 0:
self.dx = 1
self.dy = 0
elif self.dir == 90:
self.dx = 0
self.dy = -1
elif self.dir == 180:
self.dx = -1
self.dy = 0
elif self.dir == 270:
self.dx = 0
self.dy = 1
self.dx *= self.speed
self.dy *= self.speed
def walkEast(self):
imgMaster = pygame.image.load("sprites\Sheet1.gif")
imgMaster = imgMaster.convert()
self.imgList = []
imgSize = (40, 46)
offset = ((400, 905), (441, 905), (493, 905), (538, 905), (584, 905), (633, 905), (677, 905), (720, 905))
for i in range(8):
tmpImg = pygame.Surface(imgSize)
tmpImg.blit(imgMaster, (0, 0), (offset[i], imgSize))
transColor = tmpImg.get_at((1, 1))
tmpImg.set_colorkey(transColor)
self.imgList.append(tmpImg)
self.dir = 0
def walkWest(self):
imgMaster = pygame.image.load("sprites\Sheet1.gif")
imgMaster = imgMaster.convert()
self.imgList = []
imgSize = (40, 46)
offset = ((30, 909), (81, 909), (125, 909), (169, 909), (216, 909), (265, 909), (314, 909), (359, 909))
for i in range(8):
tmpImg = pygame.Surface(imgSize)
tmpImg.blit(imgMaster, (0, 0), (offset[i], imgSize))
transColor = tmpImg.get_at((1, 1))
tmpImg.set_colorkey(transColor)
self.imgList.append(tmpImg)
self.dir = 180
def walkNorth(self):
imgMaster = pygame.image.load("sprites\Sheet1.gif")
imgMaster = imgMaster.convert()
self.imgList = []
imgSize = (40, 46)
offset = ((29, 973), (73, 973), (121, 973), (168, 973), (217, 973), (261, 973), (305, 973), (351, 973))
for i in range(8):
tmpImg = pygame.Surface(imgSize)
tmpImg.blit(imgMaster, (0, 0), (offset[i], imgSize))
transColor = tmpImg.get_at((1, 1))
tmpImg.set_colorkey(transColor)
self.imgList.append(tmpImg)
self.dir = 90
def walkSouth(self):
imgMaster = pygame.image.load("sprites\Sheet1.gif")
imgMaster = imgMaster.convert()
self.imgList = []
imgSize = (40, 46)
offset = ((412, 973), (455, 973), (498, 973), (541, 973), (587, 973), (631, 973), (676, 973), (715, 973))
for i in range(8):
tmpImg = pygame.Surface(imgSize)
tmpImg.blit(imgMaster, (0, 0), (offset[i], imgSize))
transColor = tmpImg.get_at((1, 1))
tmpImg.set_colorkey(transColor)
self.imgList.append(tmpImg)
self.dir = 270
def update(self):
oldCenter = self.rect.center
self.image = self.imgList[self.frame]
self.rect = self.image.get_rect()
self.rect.center = oldCenter
self.chkDir()
self.x += self.dx
self.y += self.dy
self.checkBound()
self.rect.centerx = self.x
self.rect.centery = self.y
self.pause += 1
if self.pause >= self.delay:
self.pause = 0
self.frame += 1
if self.frame >= len(self.imgList):
self.frame = 0
def checkBound(self):
screen = self.screen
if self.y > screen.get_height():
self.y = 0
if self.y < 0:
self.y = screen.get_height()
if self.x > screen.get_width():
self.x = 0
if self.x < 0:
self.x = screen.get_width()
def main():
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Zelda")
#tells pygame the size of the screen and what to use as a background color
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 255))
screen.blit(background, (0, 0))
#Creates the local variables from the specific class
zone1 = Zone1()
zelda = ZeldaC()
#Defines specific groups of sprites, this allows them to clear, update, draw together.
goodSprites = pygame.sprite.OrderedUpdates(zone1, zelda)
clock = pygame.time.Clock()
#Main game loop
keepGoing = True
while keepGoing:
clock.tick(30)
pygame.mouse.set_visible(False)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
zelda.walkWest()
elif event.key == pygame.K_RIGHT:
zelda.walkEast()
elif event.key == pygame.K_UP:
zelda.walkNorth()
elif event.key == pygame.K_DOWN:
zelda.walkSouth()
#Updates the sprites as per group
goodSprites.clear(screen, background)
goodSprites.update()
goodSprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()