lavinog
July 23rd, 2009, 05:37 PM
I converted the tabs to spaces, to make the code a little easier for everyone to read:
# -*- coding: utf-8 -*-
balli = "data/ball.png"
bati = "data/bat.png"
import pygame
from pygame.locals import *
from sys import exit
from random import randint
from sys import argv
class ballc:
def __init__(self, ball):
self.ball = ball
self.x = 400
self.y = 400
self.rx = randint(2, 4)
self.ry = randint(2, 4)
self.move_x = -self.rx
self.move_y = -self.ry
self.speed = 70
def move(self, screen, time):
self.rx = randint(2, 4)
self.ry = randint(2, 4)
self.x += self.move_x * self.speed * time
self.y += self.move_y * self.speed * time
screen.blit(self.ball, (self.x, self.y))
if self.x >= (600 - self.ball.get_width()):
self.move_x = -self.rx
if self.x <= 0:
self.move_x = self.rx
if self.y <= 0:
self.move_y = self.ry
if self.y >= 480:
return False
return True
def coll(self, x, y, image):
if (self.x >= x and self.x - self.ball.get_width() <= x + image.get_width()) and (self.y >= 470 - bat.get_height()):
if self.move_x == 4:
self.move_x = -4
else:
self.move_x = 4
self.move_y = -4
class block:
def __init__(self, block, x, y,name="",mustc=1):
self.block = block
self.x = x
self.y = y
self.name = name
self.mustc = mustc
self.c = 0
def move(self, screen, time):
screen.blit(self.block, (self.x, self.y))
def coll(self, x, y, image, score, ball):
if self.x <= x:
if x <= self.x + self.block.get_width():
if self.y <= y:
if y <= self.y + self.block.get_height():
self.c += 1
if self.c >= self.mustc:
if self.name == "b":
score += 1
elif self.name == "r":
score += 2
elif self.name == "2":
score += 5
elif self.name == "g":
score += 3
return True, score
elif self.name == "2":
self.block = pygame.image.load("data/22block.png")
elif self.name.startswith('t'):
if self.name.endswith('r'):
self.name = 'r'
self.block = pygame.image.load("data/rblock.png")
elif self.name.endswith('b'):
self.name = 'b'
self.block = pygame.image.load("data/bblock.png")
elif self.name.endswith('g'):
self.name = 'g'
self.block = pygame.image.load("data/gblock.png")
elif self.name.endswith('2'):
self.name = '2'
self.block = pygame.image.load("data/2block.png")
ball.move_x = -ball.move_x
ball.move_y = 4
return False, score
class sblock:
def __init__(self, block, x, y, name=""):
self.block = block
self.x = x
self.y = y
self.name = name
def move(self, screen, time):
screen.blit(self.block, (self.x, self.y))
def coll(self, x, y, image, score, ball):
if self.x <= x:
if x <= self.x + self.block.get_width():
if self.y <= y:
if y <= self.y + self.block.get_height():
ball.move_x = -ball.move_x
ball.move_y = -ball.move_y
return False, score
if len(argv) > 1:
barray = []
f = open(argv[1], "r")
lines = f.readlines()
for line in lines:
for char in line:
barray.append(char.strip())
else:
barray = ["r","r","r","r","r","r","r","r","r","r","r","b","b","b","b","b","b","b","b","b","b","b", "!","_","r","b","r","b","r","b","r","_","!", "","","_","b","b","b","b","b","_","","","","","","_","r","r","r","_","","","","","","","","_","b","_","","","","", "2","2","2","2","2","2","2","2","2","2","2", "g","g","g","g","g","g","g","g","g","g","g", "!", "£", "$", "%", "!", "£","$","%","!","£","$"]
screen = pygame.display.set_mode((605,480), 0, 32)
clock = pygame.time.Clock()
ball = pygame.image.load(balli)
bat = pygame.image.load(bati)
i = 0
lastn = 0
lastn2 = 0
rblock = pygame.image.load("data/rblock.png")
bblock = pygame.image.load("data/bblock.png")
sblocki = pygame.image.load("data/_block.png")
twoblock = pygame.image.load("data/2block.png")
gblock = pygame.image.load("data/gblock.png")
tblock = pygame.image.load("data/tblock.png")
blocks = []
score = 0
try:
for y in xrange(0, 250, rblock.get_height()):
for x in xrange(0, 605, rblock.get_width()):
if barray[i] == 'r':
blocks.append(block(rblock, x, y, 'r'))
elif barray[i] == 'b':
blocks.append(block(bblock, x, y, 'b'))
elif barray[i] == '_':
blocks.append(sblock(sblocki, x, y))
elif barray[i] == '2':
blocks.append(block(twoblock, x, y, "2", 2))
elif barray[i] == 'g':
blocks.append(block(gblock, x, y, 'g'))
elif barray[i] == '!':
blocks.append(block(tblock, x, y, 'tr', 2))
elif barray[i] == '£':
blocks.append(block(tblock, x, y, 'tb', 2))
elif barray[i] == '$':
blocks.append(block(tblock, x, y, 'tg', 2))
elif barray[i] == '%':
blocks.append(block(tblock, x, y, 't2', 3))
i += 1
except IndexError:
pass
y = 470 - bat.get_height()
yd = 0
balls = ballc(ball)
while True:
time_passed = clock.tick(30) / 1000.0
screen.fill((255,255,255))
for event in pygame.event.get():
if event.type == QUIT:
exit()
for i in blocks:
i.move(screen, time_passed)
coll, score = i.coll(balls.x,balls.y,ball,score,balls)
if coll:
blocks.remove(i)
x,yd = pygame.mouse.get_pos()
x -= bat.get_width()/2
screen.blit(bat, (x,y))
balls.coll(x, y, ball)
draw = balls.move(screen, time_passed)
if not draw or len(blocks) == 0:
print score
exit()
pygame.display.update()
I would suggest breaking your main code up into functions to make it easier to debug, and give feedback on areas that can use improvement.
I have never used pygame before, so I am learning alot from this thread too.
I think if you should be able to make a generic block image, and have the program color it for you. This would let you easily add any color block without having to make a new image.
Also like mentioned earlier you should set the screen resolution to match the block array
for the block array, break it up so it is readable and understandable:
barray = [ "r","r","r","r","r","r","r","r","r","r","r",
"b","b","b","b","b","b","b","b","b","b","b",
"!","_","r","b","r","b","r","b","r","_","!",
"" ,"" ,"_","b","b","b","b","b","_","" ,"" ,
"" ,"" ,"" ,"_","r","r","r","_","" ,"" ,"" ,
"" ,"" ,"" ,"" ,"_","b","_","" ,"" ,"" ,"" ,
"2","2","2","2","2","2","2","2","2","2","2",
"g","g","g","g","g","g","g","g","g","g","g",
"!","£","$","%","!","£","$","%","!","£","$"]
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.