Mục tiêu. Học lập trình hướng đối tượng – Modularity. Làm cách nào chúng tôi có thể biến một số phần mã của mình thành một thư viện để bất kỳ ai cũng có thể sử dụng nó để tham khảo trong tương lai. Tạo mã theo mô-đun sẽ cho phép khả năng sử dụng lại và giảm thiểu trùng lặp
phụ thuộc. pygame
Tóm lược. Chúng tôi sẽ tạo một trò chơi nhỏ [không thực sự là một trò chơi] mà chỉ là một môi trường và một số đối tượng trong đó. Chúng tôi sẽ cố gắng làm cho môi trường tĩnh và các đối tượng [các đốm màu trong trường hợp của chúng tôi] theo mô-đun. Chúng tôi sẽ sử dụng PyGame, vì nó mang lại cho chúng tôi một cách đơn giản để thực sự hình dung những gì chúng tôi đang làm và xây dựng, vì vậy chúng tôi có thể thấy các đối tượng của mình đang hoạt động. Những gì chúng ta sẽ làm là xây dựng Blob World, một thế giới bao gồm các diễn viên, được gọi là các đốm màu. Các đốm màu khác nhau có các thuộc tính khác nhau và các đốm màu cần hoạt động khác trong môi trường Thế giới đốm màu của chúng. Với ví dụ này, chúng ta sẽ có thể minh họa tính mô đun.
Chúng tôi đang chia quá trình học tập của chúng tôi thành hai giai đoạn.
- Tạo môi trường và các đốm màu
- Hiểu mô đun
Kho lưu trữ [Github]. nguồn
THẾ GIỚI BLOB [Mã Python]
con trăn
import
pygame
import
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...0
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...3
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...4_______0_______2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...7
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...9
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...0_______0_______2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...3
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...6_______10_______7
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...6
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...6
self.size = random.randrange[4,8]1
self.size = random.randrange[4,8]2_______0_______2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...5
self.size = random.randrange[4,8]5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
self.size = random.randrange[4,8]5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...6
self.size = random.randrange[4,8]1
class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8]]: self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.size = random.randrange[size_range[0],size_range[1]] self.color = color1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...6
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
self.size = random.randrange[4,8]5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
self.size = random.randrange[4,8]5
self.size = random.randrange[4,8]1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary0_______0_______2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary3
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
self.size = random.randrange[4,8]1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6_______0_______2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary8
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary9
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary0
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1_______50_______2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary3
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary5
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary8
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import
0self.size = random.randrange[4,8]5
import
2import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import
5import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import
0self.size = random.randrange[4,8]5
import
9import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
pygame
2import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import
0pygame
5def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
pygame
7self.size = random.randrange[4,8]1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import
1import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import
3
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary2
import
6import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import
8import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...01
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import
0import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...04
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...05
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...07
self.size = random.randrange[4,8]1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...11_______0_______2
import
0import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...04
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...05
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...7
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...07
self.size = random.randrange[4,8]1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary8
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...22
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...01
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import
5import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...22
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...11
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...34
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...36_______21_______5
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...38
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary8
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
self.size = random.randrange[4,8]5
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...44
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...46_______50_______4
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary8
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...7
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...34
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...55_______21_______5
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...38
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import
5import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
self.size = random.randrange[4,8]5
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...44
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary4
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...65_______50_______4
import
5import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...0
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...71
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...73
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...75
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...76
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...77
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...78
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...75
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...81
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...77
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...83
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...84
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...85
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...87
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...84
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...89
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...84
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...91
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...93
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...96
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...98
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...00
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...02
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...03
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...75
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...05
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...77
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...07
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...08
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...10
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...00_______10_______5
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...02
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...15
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...75
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...05
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...77
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...07
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...20
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...22
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...23
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...38
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...75
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...27
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...77
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...29
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...84
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...34
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...32
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...33
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...36
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...37
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...38
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...37
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...40
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...42
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary6
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...44
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...45
self.size = random.randrange[4,8]1
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...34
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...48
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...2
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...51
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...38
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y def check_bounds[self]: if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary1
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...54
đầu ra.
https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/uploads/bobWorld. mp4
PHẦN[1/2]. Thế giới đốm màu Trong phần này, chúng ta đang tạo một môi trường trò chơi đơn giản và một số đối tượng trong đó vì trực quan hóa những gì chúng ta đã tạo ra là một cách đặc biệt để học lập trình. Lời giải thích cho việc tạo ra thế giới blob [ i. e, môi trường và các đối tượng của nó] sử dụng pygame được giải thích tại đây. Tất cả những gì chúng ta cần hiểu là làm thế nào để làm cho mã của chúng ta theo mô-đun
PHẦN[2/2]. Tính mô đun Trong phần thứ hai này, chúng ta sẽ hiểu một tính năng thiết yếu của Lập trình hướng đối tượng, đó là. e Tính mô đun. Cho đến nay, chúng tôi chưa giới thiệu bất kỳ điều gì khiến mã này [mã BLOB WORLD] trở nên quá khó để duy trì hoặc mở rộng quy mô theo thời gian, ít nhất là trong phạm vi những gì chúng tôi có thể làm với PyGame. Điều gì về làm cho nó mô-đun? .
Để làm điều này, hãy có hai tệp. Hãy sao chép lớp Blob và ngẫu nhiên, đồng thời tạo một tệp mới. bãi. py
import random class Blob: def __init__[self, color]: self.x = random.randrange[0, WIDTH] self.y = random.randrange[0, HEIGHT] self.size = random.randrange[4,8] self.color = color def move[self]: self.move_x = random.randrange[-1,2] self.move_y = random.randrange[-1,2] self.x += self.move_x self.y += self.move_y if self.x WIDTH: self.x = WIDTH if self.y HEIGHT: self.y = HEIGHT
Quay lại tệp gốc của chúng tôi, hãy xóa lớp Blob, sau đó nhập Blob từ blob. py.
import pygame import random from blob import Blob STARTING_BLUE_BLOBS = 10 ...
Ngay lập tức, chúng tôi nhận được một lỗi trong đốm màu. py, liên quan đến lớp Blob của chúng tôi, nơi chúng tôi có một số biến không xác định. Đây chắc chắn là một vấn đề với việc viết các lớp, chúng ta nên cố gắng tránh sử dụng các hằng hoặc biến bên ngoài lớp. Hãy thêm các giá trị này vào phương thức __init__, sau đó sửa đổi tất cả các phần mà chúng ta đã sử dụng hằng số.
Vì vậy, đây là tệp lớp Blob mới của chúng tôi. bãi. py
Tiếp theo, trong tệp gốc của chúng tôi, khi chúng tôi gọi lớp Blob, nó mong đợi một số giá trị cho các đối số đó, vì vậy bạn sẽ thêm chúng vào hàm chính.
def main[]: blue_blobs = dict[enumerate[[Blob[BLUE,WIDTH,HEIGHT] for i in range[STARTING_BLUE_BLOBS]]]] red_blobs = dict[enumerate[[Blob[RED,WIDTH,HEIGHT] for i in range[STARTING_RED_BLOBS]]]] while True: ...
Tuyệt vời, vì vậy bây giờ lớp Blob của chúng ta ít nhất có thể được nhập vào, vì vậy bản chất của nó là mô-đun rồi. Một ý tưởng hay khác là cố gắng cung cấp cho nhà phát triển đang sử dụng mã của bạn nhiều quyền lực nhất có thể và làm cho lớp của bạn có khả năng khái quát hóa nhất có thể. Ít nhất một ví dụ mà chúng tôi chắc chắn có thể cung cấp thêm cho lập trình viên sử dụng lớp này là trong định nghĩa về kích thước của đốm màu.
self.size = random.randrange[4,8]
Có lý do nào khiến chúng tôi không muốn cung cấp cho lập trình viên một cách dễ dàng để thay đổi những điều này không? . Tuy nhiên, không giống như x_boundary và y_boundary, chúng tôi không nhất thiết phải *cần* lập trình viên cung cấp cho chúng tôi giá trị về kích thước, vì ít nhất chúng tôi có thể sử dụng giá trị mặc định bắt đầu hợp lý. Vì vậy, chúng ta có thể làm một cái gì đó như.
class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8]]: self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.size = random.randrange[size_range[0],size_range[1]] self.color = color
Bây giờ, nếu lập trình viên muốn thay đổi kích thước, họ có thể, nếu không thì họ không phải. Chúng tôi cũng có thể muốn cho phép lập trình viên sửa đổi tốc độ của đốm màu nếu họ muốn.
import random class Blob: def __init__[self, color, x_boundary, y_boundary, size_range=[4,8], movement_range=[-1,2]]: self.size = random.randrange[size_range[0],size_range[1]] self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange[0, self.x_boundary] self.y = random.randrange[0, self.y_boundary] self.movement_range = movement_range def move[self]: self.move_x = random.randrange[self.movement_range[0],self.movement_range[1]] self.move_y = random.randrange[self.movement_range[0],self.movement_range[1]] self.x += self.move_x self.y += self.move_y if self.x self.x_boundary: self.x = self.x_boundary if self.y self.y_boundary: self.y = self.y_boundary
Bây giờ chúng tôi đã mở lớp khá nhiều. Có bất cứ điều gì khác nhảy ra khỏi chúng tôi? . Có thể có những ví dụ mà chúng tôi muốn các đốm màu có thể tự do đi lang thang ngoài tầm nhìn? . Mã giới hạn này có hữu ích không? . tuy nhiên, sẽ hợp lý hơn nếu hoàn toàn không có mã hoặc cung cấp cho nó phương thức riêng, như vậy.