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

[pygame] Yes I'm finally getting "Classes" with pygame.



:D I'm making progress, taught my self some what of how to move images using keyboard keys.  First I took some animated ball moving script ripped it apart tried understanding it and edited it a bit, was fun.  Then I rewrote it using classes :D with my basic Python Knowledge I've been studying for forever and I pulled it off.  So far so good.
 
First Code:
import sys, pygame
from pygame.locals import *
pygame.init()
size = width , height = 320, 240
speed = [0,0]
pace = 10
black = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption ("Demonics Ball Game[Rofl]")
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()
print ballrect[3]
wi = 10
hi = 10
x = 0
while True:
 for event in pygame.event.get():
  pygame.key.set_repeat()
  if event.type == pygame.QUIT: sys.exit()
  elif event.type == pygame.KEYDOWN:
   pygame.event.pump()
   key = pygame.key.get_pressed()
   ballrect = speed
   if key[K_DOWN] and speed[1] < height-wi:
    speed[1] += pace
   if key[K_UP] and speed[1] > 0:
    speed[1] -= pace
   if key[K_LEFT] and speed[0] > 0:
    speed[0] -= pace
   if key[K_RIGHT] and speed[0] < width-hi:
    speed[0] += pace
 #ballrect = ballrect.move(speed)
 #if ballrect.left < 0 or ballrect.right > width:
 # speed[0] = -speed[0]
 #if ballrect.top < 0 or ballrect.bottom > height:
 # speed[1] = -speed[1]
 screen.fill (black)
 screen.blit(ball, ballrect)
 pygame.display.update()

Second Version:
import sys,pygame
from pygame.locals import *
class MoveBall(object):
 def __init__(self,size,pace,background,caption="Game Practice"):
  pygame.init()
  self.size = self.width,self.height = size
  self.speed = [0,0]
  self.pace = pace
  self.color = background
  self.window = pygame.display.set_mode(self.size)
  pygame.display.set_caption(caption)
 def load_image(self,img):
  image = pygame.image.load (img)
  return image
 def update(self,img):
  ball = img
  rect_ = ball.get_rect()
  bw = rect_[2]
  bh = rect_[3]
  while True:
   ballrect = self.speed
   for event in pygame.event.get():
     pygame.key.set_repeat()
    if event.type == pygame.QUIT: sys.exit()
    elif event.type == pygame.KEYDOWN:
     pygame.event.pump()
     key = pygame.key.get_pressed()
     ballrect = self.speed
     if key[K_DOWN] and self.speed[1] < self.height-bw:
      self.speed[1] += self.pace
     if key[K_UP] and self.speed[1] > 0:
      self.speed[1] -= self.pace
     if key[K_LEFT] and self.speed[0] > 0:
      self.speed [0] -= self.pace
     if key[K_RIGHT] and self.speed[0] < self.width-bh:
      self.speed[0] += self.pace
   self.window.fill(self.color)
   self.window.blit(ball, ballrect)
   pygame.display.update()
Ball = MoveBall((320,240),2,(0,0,0))
image = Ball.load_image("ball.gif")
Ball.update(image)
 
(Gmail messed up my spacing -.-)
So quick question, How come when I used my class it doesn't load any faster then I thought it would?