Vous êtes sur la page 1sur 3

#

#
#
#

Crazy Breakout
Defeat the blocks! Where is the ball going to go? No one knows!
Scott Warnert
12/16/14

from livewires import games, color


import random
from tkinter import *
games.init(screen_width = 640, screen_height = 480, fps = 60)
class Application(Frame):
""" GUI application which can save a high score. """
def __init__(self, master):
""" Initialize the frame. """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create button, text, and entry widgets. """
# create instruction label
self.inst_lbl = Label(self, text = "Enter your name to save your highscore!"
)
self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
# create label for name
self.name_lbl = Label(self, text = "Name: ")
self.name_lbl.grid(row = 1, column = 0, sticky = W)
# create entry widget to enter name
self.name_ent = Entry(self)
self.name_ent.grid(row = 1, column = 1, sticky = W)
# create submit button
self.save_bttn = Button(self, text = "Save", command = self.save)
self.save_bttn.grid(row = 2, column = 0, sticky = W)
def save(self):
""" Display message based on password. """
name = self.name_ent.get() + "\n"
high_score = open("high_score.text", "w")
lines = [name, str(Paddle.score.value)]
high_score.writelines(lines)
high_score.close()
class Ball(games.Sprite):
""" A bouncing ball. """
def update(self):
""" Reverse a velocity component if edge of screen reached. """
if self.right > games.screen.width or self.left < 0:
self.dx = -self.dx
if self.top < 0:
self.dy = -self.dy
if self.bottom > games.screen.height:
self.end_game()
def handle_paddle_collide(self):

self.dy = -self.dy
self.dx = random.random() * 8 - 4
def handle_block_collision(self):
self.dy = random.random() * 8 - 4
self.dx = random.random() * 8 - 4
def end_game(self):
""" End the game"""
games.music.stop()
high_score = open("high_score.text", "r")
high_score.readline()
recordedScore = int(high_score.readline())
high_score.close()
if Paddle.score.value > recordedScore:
root = Tk()
root.title("Enter your name")
root.geometry("300x100")
app = Application(root)
root.mainloop()
end_message = games.Message(value = "Game Over",
size = 90,
color = color.white,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 5 * games.screen.fps,
after_death = games.screen.quit())
games.screen.add(end_message)
class Paddle(games.Sprite):
""" A paddle controlled by the mouse. """
image = games.load_image("pongpaddle.bmp")
score = games.Text(value = 0,
size = 25,
color = color.white,
top = 5,
right = games.screen.width - 10)
def __init__(self):
"""Initialize Paddle Object and create Text Object for score"""
super(Paddle, self).__init__(image = Paddle.image,
x = games.mouse.x,
y = games.screen.height - 20)
games.screen.add(Paddle.score)
def update(self):
""" Move to mouse x-coordinate and check for collision. """
self.x = games.mouse.x
self.check_collide()
def check_collide(self):
""" Check for collision with ball. """
for ball in self.overlapping_sprites:
ball.handle_paddle_collide()
class Block(games.Sprite):

"""A block that needs breaking"""


def update(self):
self.check_collide()
def check_collide(self):
for ball in self.overlapping_sprites:
ball.handle_block_collision()
Paddle.score.value +=1
Paddle.score.right = games.screen.width - 10
self.destroy()
def main():
ball_image = games.load_image("pongball.bmp")
the_ball = Ball(image = ball_image,
x = games.screen.width/2,
y = 450,
dx = 1,
dy = -1)
games.screen.add(the_ball)
block_image = games.load_image("block.bmp")
the_blocks = [["" for x in range(15)] for y in range(11)]
for i in range(11):
for j in range(15):
the_blocks[i][j] = Block(image = block_image,
x = 23 + j * 40,
y = 13 + i * 20)
for i in range(11):
for j in range(15):
games.screen.add(the_blocks[i][j])
the_paddle = Paddle()
games.screen.add(the_paddle)
games.screen.event_grab = True
games.mouse.is_visible = False
games.music.load("town.mid")
games.music.play(-1)
games.screen.mainloop()
# kick it off!
main()

Vous aimerez peut-être aussi