[Tkinter] Breakout game help?

This is the place for queries that don't fit in any of the other categories.

[Tkinter] Breakout game help?

Postby dannyxy » Thu Apr 25, 2013 12:52 pm

Hi i am trying to clone thw breakout game in Tkinter but i seem to be getting some errors:
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: [Tkinter] Breakout game help?

Postby dannyxy » Thu Apr 25, 2013 12:57 pm

Code: Select all
#!C:\Python27\python.exe

from Tkinter import *
from PIL import Image, ImageTk

class BreakOut(Canvas):
 
 p_x=145
 p_y=395
 b_x=145
 b_y=385
 dx=15
 b_dx=1
 hasHitRightSide=False
 hasHitLeftSide=False
 hasHitMiddle=True
 hasHitBricks=False
 hasHitBottom=False
 tag_name=""
 b_dy=-15
 HIGH_SCORE=0
 COUNT=15

 def __init__(self,parent):
  Canvas.__init__(self,width=307,height=400,bg="#AAAAAA")
  self.parent=parent
  self.hasStarted=False
  self.paused=False
  self.Game()

 def Game(self):
  self.parent.title("Breakout 2D")
  self.parent.geometry("307x400")
  self.create_text(10,10,anchor=NW,text="Highscore: %d"%self.HIGH_SCORE,tag="highscore")
  self.player=Image.open(r"C:\Users\user\Downloads\images(2)\breakout\paddle.png")
  self.playerimg=ImageTk.PhotoImage(self.player)
  self.create_image(self.p_x,self.p_y,image=self.playerimg,anchor=S,tag="paddle"
  self.ball=Image.open(r"C:\Users\user\Downloads\images(2)\breakout\ball.png")
  self.ballimg=ImageTk.PhotoImage(self.ball)
  self.create_image(self.b_x,self.b_y,image=self.ballimg,anchor=S,tag="ball")
  self.brick=Image.open(r"C:\Users\user\Downloads\images(2)\breakout\brickie.png")
  self.brickimg=ImageTk.PhotoImage(self.brick)
  self.create_image(70,80,image=self.brickimg,anchor=N,tag="brick1")
  self.create_image(70+40,80,image=self.brickimg,anchor=N,tag="brick2")
  self.create_image(70+(40*2),80,image=self.brickimg,anchor=N,tag="brick3")
  self.create_image(70,80+10,image=self.brickimg,anchor=N,tag="brick4")
  self.create_image(70+40,80+10,image=self.brickimg,anchor=N,tag="brick5")
  self.create_image(70+(40*2),80+10,image=self.brickimg,anchor=N,tag="brick6")
  self.bind_all("<Key>",self.onKeyPressed)
  self.pack()
 
 def onKeyPressed(self,e):
  self.key=e.keysym
  x,y=self.coords("paddle")
  if self.key=="Return":
   self.Timer()
   self.hasStarted=True
  if self.hasStarted==False:
   if self.key=="Left":
    if x==280:
     self.dx=15
    if x==25:
     self.dx=0
    self.move("paddle",-self.dx,0)
    self.move("ball",-self.dx,0)
   if self.key=="Right":
    if x==25:
     self.dx=15
    if x==280:
     self.dx=0
    self.move("ball",self.dx,0)
    self.move("paddle",self.dx,0)
  if self.hasStarted==True:
   if self.key=="p":
    if self.paused==False:
     self.paused=True
    elif self.paused==True:
     self.paused=False
     self.Timer()
   if self.key=="Left":
    if x==280:
     self.dx=15
    if x==25:
     self.dx=0
    self.move("paddle",-self.dx,0)
   if self.key=="Right":
    if x==25:
     self.dx=15
    if x==280:
     self.dx=0
    self.move("paddle",self.dx,0)

 def moveball(self):
  x,y=self.coords("ball")
  self.move("ball",self.b_dx,self.b_dy)
  if self.hasHitRightSide==True and self.hasHitLeftSide==False and self.hasHitMiddle==False:
   if x>=280:
    self.b_dx=-10
    self.b_dy=-15
   if y<=27:
    self.b_dx=-10
    self.b_dy=15
   if x<=27:
    self.b_dx=10
    self.b_dy=15
   if y>400:
    self.hasHitBottom=True
  elif self.hasHitLeftSide==True and self.hasHitRightSide==False and self.hasHitMiddle==False:
   if x>=280:
    self.b_dx=-10
    self.b_dy=15
   elif y<=27:
    self.b_dx=10
    self.b_dy=15
   elif x<=27:
    self.b_dx=10
    self.b_dy=-15
   elif y>400:
    self.hasHitBottom=True
  elif self.hasHitMiddle==True and self.hasHitRightSide==False and self.hasHitLeftSide==False:
   if y<=27:
    self.b_dx=2
    self.b_dy=15
   if x>=280:
    self.b_dx=-10
    self.b_dy=15
   elif y>400:
    self.hasHitBottom=True
   
 def checkCollision(self):
  x1,y1,x2,y2=self.bbox("paddle")
  x,y=self.coords("ball")
  if (y==y1) and x in range(x1+10,x2-10):
   self.hasHitLeftSide=False
   self.hasHitRightSide=False
   self.hasHitMiddle=True
   self.b_dy=-15
   self.b_dx=2
  elif (y==y1) and x in range(x1,x2-30):
   self.hasHitLeftSide=True
   self.hasHitRightSide=False
   self.hasHitMiddle=False
   self.b_dy=-15
   self.b_dx=-10
 
  #If the ball hits the right side of the top of the paddle
  elif (y==y1) and x in range(x1-30,x2):
   self.hasHitLeftSide=False
   self.hasHitRightSide=True
   self.hasHitMiddle=False
   self.b_dy=-15
   self.b_dx=10
 
 def checkBricks(self):
  x,y=self.coords("ball")
  br1_x1,br1_y1,br1_x2,br1_y2=self.bbox("brick1")
  br2_x1,br2_y1,br2_x2,br2_y2=self.bbox("brick2")
  br3_x1,br3_y1,br3_x2,br3_y2=self.bbox("brick3")
  br4_x1,br4_y1,br4_x2,br4_y2=self.bbox("brick4")
  br5_x1,br5_y1,br5_x2,br5_y2=self.bbox("brick5")
  br6_x1,br6_y1,br6_x2,br6_y2=self.bbox("brick6")
  if (y==br4_y2) and (x in range(br4_x1,br4_x2)):
   self.b_dx=self.b_dx
   self.b_dy=-15
   self.delete("brick4")
   self.HIGH_SCORE+=self.COUNT
  if (y==br5_y2) and (x in range(br5_x1,br5_x2)):
   self.b_dx=self.b_dx
   self.b_dy=-15
   self.delete("brick5")
   self.HIGH_SCORE+=self.COUNT
  if (y==br6_y2) and (x in range(br6_x1,br6_x2)):
   self.b_dx=self.b_dx
   self.b_dy=-15
   self.delete("brick6")
   self.HIGH_SCORE+=self.COUNT
  if (y==br1_y1) and (x in range(br1_x1,br1_x2)):
   print "hit top"
   self.b_dx=self.b_dx
   self.b_dy=15
   self.delete("brick1")
   self.HIGH_SCORE+=self.COUNT
  if (y==br2_y1) and (x in range(br2_x1,br2_x2)):
   print "hit top"
   self.b_dx=self.b_dx
   self.b_dy=15
   self.delete("brick2")
   self.HIGH_SCORE+=self.COUNT
  if (y==br3_y1) and (x in range(br3_x1,br3_x2)):
   print "hit top"
   self.b_dx=self.b_dx
   self.b_dy=15
   self.delete("brick3")
   self.HIGH_SCORE+=self.COUNT
  self.itemconfigure("highscore",text="Highscore: %d"%self.HIGH_SCORE)
 
 def Timer(self):
  if self.paused==False:
   self.moveball()
   
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: [Tkinter] Breakout game help?

Postby dannyxy » Thu Apr 25, 2013 1:09 pm

The code continues:
Code: Select all
   self.checkBricks()
   self.GameOver()
   self.checkCollision()
   self.after(70,self.Timer)
   
 def GameOver(self):
  if self.hasHitBottom==True:
   self.delete(ALL)
   font=("Tahoma",18)
   self.create_text(150,170,font=font,text="Game Over",anchor=N)
   self.create_text(150,200,anchor=N,text="Highscore: %d"%self.HIGH_SCORE)

root=Tk()
BO=BreakOut(root)
root.mainloop()
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am

Re: [Tkinter] Breakout game help?

Postby dannyxy » Thu Apr 25, 2013 1:17 pm

Here are the problems i am having?
1. The ball does not rebound when it hits the bricks instead it passes through it
2. When the ball hits the brick it produces a TypeError: `NoneType` object is not iterable
3. when the ball hits the bottom of the board it produces a TypeError: `NoneType` object is iterable
4. The speed of the ball increases each time i press the Enter key
dannyxy
 
Posts: 18
Joined: Fri Mar 29, 2013 11:26 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 4 guests