patternpythonMinor
Block_breaker clone in pygame with simple edge detection
Viewed 0 times
block_breakersimplewithedgedetectionpygameclone
Problem
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:
```
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=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))
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
- 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=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))
Solution
- First off, according to this online tool, you have
~119PEP8 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
poolclass 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 thepoolclass completely, and just having global variables, with functions that modify those variables.
- On your
poolclass 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, orthungdoes 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
playfunction could be separated into multiple functions, like acreate_blocksfunction, orcheck_for_quit.
- You should define
SCREEN_WIDTHandSCREEN_HEIGHTvariables. 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,
gameovershould beGAME_OVER_IMAGE.
Anyways, I hope this helped!
Code Snippets
import pygame
import sys
import time
import randomContext
StackExchange Code Review Q#80424, answer score: 8
Revisions (0)
No revisions yet.