[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] I can't seem to find any simple sprite animation game examples on the pygame.org site



Nvm I fixed it. :D

On 9/11/07, Lamonte Harris <pyth0nc0d3r@xxxxxxxxx> wrote:
Okay this is really complicated, it seem like its looping correctly but the images aren't switching.

On 9/11/07, Lamonte Harris < pyth0nc0d3r@xxxxxxxxx> wrote:
Okay thanks sounds like a plan.


On 9/11/07, Ian Mallett <geometrian@xxxxxxxxx > wrote:
Opps.  That's:

if frame_number%10 == 0: #frame_number is a multiple of ten
  #draw frame 1
elif frame_number%9 == 0: #frame_number ends in 9
  #draw frame 2
elif frame_number%8 == 0: #frame_number ends in 8
  #draw frame 3
elif frame_number%7 == 0: #frame_number ends in 7
  #draw frame 4
elif frame_number%6 == 0: #frame_number ends in 6
  #draw frame 5
elif frame_number%5 == 0: #frame_number ends in 5
  #draw frame 6
elif frame_number%4 == 0: #frame_number ends in 4
  #draw frame 7
elif frame_number%3 == 0: #frame_number ends in 3
  #draw frame 8
elif frame_number%2 == 0: #frame_number ends in 2
  #draw frame 9
else: #frame_number ends in 1
  #draw frame 10

Ian




#Lamonte
import pygame,sys,string
from pygame.locals import *
class Block(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
class SpriteSheet:
	def __init__(self,image):
		self.sprite = pygame.image.load(image).convert()
		self.rect = ''
		self.imag = ''
	def slice(self,rect,key):
		rect = Rect(rect)
		img = pygame.Surface(rect.size).convert()
		img.set_colorkey(key)
		img.blit(self.sprite,(0,0),rect)
		self.rect = rect
		self.imag = img
class map_(object):
	def __init__(self,block=100,wndow=(300,300)):
		self.BLOCK = block
		self.size = wndow
		self.window = pygame.display.set_mode(self.size)
		self.blocks = []
		self.map = []
		self.ANIMATION = []
		self.clock = pygame.time.Clock()
		self.frame = 0
		pygame.init()
	def hex_converter(self,hexcolorcode):
		hexcolorcode = hexcolorcode[1:]
		red = hexcolorcode[0:2]
		red = int(red,16)
		blue = hexcolorcode[2:4]
		blue = int(blue,16)
		green = hexcolorcode[4:6]
		green = int(green,16)
		return (red,green,blue)
	def create_box(self,size,color,rect):
		box = Block()
		box.image = pygame.Surface((size))
		pygame.draw.rect(box.image,color,box.image.get_rect())
		box.color = color
		box.point = rect
		self.blocks.append(box)
	def load_text_map(self,map):
		file = open(map,"r")
		links = file.readlines()
		file.close()
		x = 0
		total = len(links)
		k = 0
		while k<total:
			self.map.append([])
			k += 1
		for i in links:
			i = string.replace(i,"\n","")
			i = i.split(",")
			for e in i:
				self.map[x].append(e)
			x += 1
	def render_map(self,aray,dict):
		x=0
		for e in aray:	
			z = 0
			for y in e:
				aray[x][z] = dict[int(y)]
				z += 1
			x += 1
		return aray
	def main(self):
		z = 0
		x = 0
		y = 0
		m = 0
		while z < len(self.map):
			q = 0
			while q < len(self.map[z]):
				self.create_box(self.map[z][q][0],self.map[z][q][1],(x,y))
				x += self.BLOCK
				q += 1
			x = 0
			y += self.BLOCK
			z += 1
		self.draw_box()
	def draw_box(self):
		g1 = SpriteSheet("player.png")
		g2 = SpriteSheet("player.png")
		g1.slice((0,0,40,57),(255,255,255))
		g2.slice((80,114,40,57),(255,255,255))
		self.ANIMATION.append(g1)
		self.ANIMATION.append(g2)
		print self.ANIMATION
		while True:
			for event in pygame.event.get():
				if event.type == pygame.QUIT: sys.exit()
			e = 0
			while e < len(self.blocks):
				self.window.blit(self.blocks[e].image,self.blocks[e].point)
				e += 1
			u = 0
			if self.frame > 1:
				self.frame = 0
			print self.frame
			self.window.blit(self.ANIMATION[self.frame].imag,(400,400))
			#print self.ANIMATION[self.frame].imag
			pygame.display.update()
			pygame.display.flip()
			self.clock.tick(2)
			self.frame += 1
Map = map_(25,(700,600))
tiles = {1 : [(25,25),Map.hex_converter("#E0ECFF")], 2 : [(25,25),Map.hex_converter("#B5EDBC")], 3 : [(25,25),Map.hex_converter("#F3E807")] }
Map.load_text_map("map1.map")
Map.render_map(Map.map,tiles)
Map.main()