8000 Merge pull request #20 from kpatell/collision · kpatell/basic-python@4cacffa · GitHub
[go: up one dir, main page]

Skip to content

Commit 4cacffa

Browse files
authored
Merge pull request #20 from kpatell/collision
updated bank_account.py and created class to demonstrate collisions
2 parents 57287ae + 1908777 commit 4cacffa

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

hello-world-book-programs/bank_account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def __init__(self, name, account_number):
1515
self.balance = 0.0
1616

1717
def __str__(self):
18-
bank_account_string = self.name + "\nAccount Number: %s \nBalance: %s" % (self.account_number, round(self.balance, 2))
19-
return bank_account_string
18+
return self.name + "\nAccount Number: %s \nBalance: %s" % \
19+
(self.account_number, round(self.balance, 2))
2020

2121
def display_balance(self):
2222
"""Displays the balance of the bank account"""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Krishan Patel
2+
3+
"""Chaper 18: Sprites and Collision Detection
4+
From Hello World! Computer Programming for Kids and Beginners
5+
Copyright Warren and Carter Sande, 2009-2013
6+
"""
7+
8+
# 17.1 Using sprites to put multiple ball images on the screen
9+
import sys
10+
import pygame
11+
12+
# Defines ball subclass
13+
class Ball(pygame.sprite.Sprite):
14+
"""Defines Ball subclass"""
15+
def __init__(self, image_file, loc):
16+
pygame.sprite.Sprite.__init__(self)
17+
self.image = pygame.image.load(image_file)
18+
self.rect = self.image.get_rect()
19+
self.rect.left, self.rect.top = loc
20+
21+
# Sets window size
22+
size = width, height = 640, 480
23+
screen = pygame.display.set_mode(size)
24+
screen.fill([255, 255, 255])
25+
IMG_FILE = "images/beach_ball.png"
26+
balls = []
27+
for row in range(0, 3):
28+
for column in range(0, 3):
29+
location = [column * 180 + 10, row * 180 + 10]
30+
ball = Ball(IMG_FILE, location)
31+
balls.append(ball) # Adds balls to a list
32+
33+
for ball in balls:
34+
screen.blit(ball.image, ball.rect)
35+
pygame.display.flip()
36+
37+
RUNNING = True
38+
while RUNNING:
39+
for event in pygame.event.get():
40+
if event.type == pygame.QUIT:
41+
RUNNING = False
42+
sys.exit()
43+
pygame.quit()

0 commit comments

Comments
 (0)
0