[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Yes I'm finally getting "Classes" with pygame.
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Yes I'm finally getting "Classes" with pygame.
- From: "Lamonte Harris" <pyth0nc0d3r@xxxxxxxxx>
- Date: Sat, 8 Sep 2007 22:30:19 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 08 Sep 2007 23:30:27 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=CxJHahaiZ9hfkSWHXsCBiJV61cWAfGzL4wTyLqKiBC4=; b=XwTIPPcjPvUjrIf77UUhNAJd23VQyqLRwqc+OKKRVJSwljVKKKdWHrGdgZdoFnLZYE4QzYgROyr9Di3RcB/JJnmRcIkuKA3Du++FQj+JOOJXdH3nS5BpgW67A4Lbej32trMJ1HrxMpIYDsqhTV2dwvDruVgfaRw6MpDEPx6gicE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type; b=CoQMuF+kfTafKhGi881p+w6kJBK3YbSOPkkD7ufl9xswT3WWdPpu7uKhP0qn9KDYW/z5nLCfJ8TXrH4/AZv/7KtzU0LcfHUSnmd1RJjD7udiiZ5oUQAgDDl+AkqGwikjeXs8/ExaEr32nED3ACKl4N6QNNo4lKPEulwvjXRPEeo=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
: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?