\$\begingroup\$
\$\endgroup\$
0
I tried to make this clone of brick_breaker on my own in one day. The source code and assets .
However I have some concerns about the code so far:
- It uses simple edge detection code (as if ball goes out of screen <600 or <462).
- It uses built-in collision detection from pygame.
- Simple array for appending blocks.
import pygame,sys,random,time
from pygame.locals import *
pygame.init()
blk=pygame.image.load('block.bmp') #load blocks image
img=pygame.image.load('back.bmp') #load background image
thung=pygame.image.load('thung.bmp') #load player image
gameover=pygame.image.load('go.bmp')
mas=pygame.image.load('masthead.bmp')
soundObj = pygame.mixer.Sound('wall.wav')
back=pygame.mixer.music.load('background.mid')
global windowSurface
windowSurface = pygame.display.set_mode((600, 462), 0, 32)
pygame.display.set_caption('Simple breakout game by piyush')
class pool():
x=1
y=1
dirx=''
diry=''
trig=0
block=[]
pigger=0
def trigger(self):
self.trig=1
def _init_(self):
self.x=250
self.y=250
dirx='left'
diry=''
block=[]
def movex(self):
if self.trig==1:
if self.dirx=='left':
self.x-=1
if self.dirx=='right':
self.x+=1
if self.x<=0:
self.dirx='right'
soundObj.play()
if self.x==600:
self.dirx='left'
soundObj.play()
def movey(self):
if self.trig==1:
if self.diry=='up':
self.y-=1
if self.diry=='down':
self.y+=1
if self.y==0:
soundObj.play()
self.diry='down'
if self.y>=410:
self.diry='up'
self.x=250
self.y=250
def drawball(self,surface):
pygame.draw.circle(surface,(200,100,230),(self.x,self.y),13,0)
def rev(self):
if self.diry=='down':
self.diry='up'
def drawblock(self,surface):
for blocks in self.block:
surface.blit(blk,blocks)
def play():
pygame.mixer.music.play(-1, 0.0)
ball1=pool()
pygame.init()
for i in range(10):
size=i*75
ball1.block.append(pygame.Rect(size,20,40,40))
for i in range(10):
size=i*75
ball1.block.append(pygame.Rect(size,60,40,40))
for i in range(10):
size=i*75
ball1.block.append(pygame.Rect(size,100,40,40
ball1.x=400
ball1.y=200
ball1.dirx='left'
ball1.diry='up'
ball1.pigger=1
a=300
b=340
while True:
windowSurface.fill((25,25,25))
pos = pygame.mouse.get_pos()
a=pos[0]
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYUP:
if event.key==K_LEFT:
a-=30
if event.key==K_RIGHT:
a+=30
if event.key==ord('a'):
pigger=0
thunge=pygame.Rect(a, b, 120, 10)
ball=pygame.Rect(ball1.x, ball1.y, 10, 10)
windowSurface.blit(img,(0,0))
windowSurface.blit(thung,thunge)
ball1.drawball(windowSurface)
ball1.movex()
ball1.movey()
ball1.trig=1
ball1.drawblock(windowSurface)
windowSurface.blit(mas,(-20,410))
if thunge.colliderect(ball):
print("collision detecte")
ball1.rev()
soundObj.play()
for blok in ball1.block:
if ball.colliderect(blok):
ball1.block.remove(blok)
pygame.display.update()
play()
piyush_devpiyush_dev
1 Answer 1
\$\begingroup\$
\$\endgroup\$
0
- First off, according to this online tool, you have
~119
PEP8 violations. So, for my first tip, I'm going to reccomend reading Python's official style guide, PEP8. The style guide essentially tells you how to format your code, so I'm not going to give you any tips on that in this answer. - Secondly, your
pool
class seems weird, partially because it seems like you don't necessarily need it, like it's just a static class with variables, and functions that modify those variables. In that case, I'd recommend abandoning thepool
class completely, and just having global variables, with functions that modify those variables. - On your
pool
class again, you've misspelled__init__
and put_init_
instead. - Sorry if this sounds rude, but some of the names that you've given your variables and functions are awful. I mean, I have no idea what the variable
pigger
, orthung
does just by looking at it. You also shouldn't abbreviate already short variable names. For example,block
, does not need to becomeblk
. - Avoid importing multiple files on one line. This helps weed out certain errors if one of the imported files isn't working correctly. You should import modules like this.
import pygame
import sys
import time
import random
- Your
play
function could be separated into multiple functions, like acreate_blocks
function, orcheck_for_quit
. - You should define
SCREEN_WIDTH
andSCREEN_HEIGHT
variables. This gets rid of "magic numbers", and helps with overall readability. - Finally any variables with values that don't change should be constant. For example,
gameover
should beGAME_OVER_IMAGE
.
Anyways, I hope this helped!
answered May 26, 2015 at 22:17
lang-py