From 6bedffa54a429bfa789a450e123a6ec74033c2ba Mon Sep 17 00:00:00 2001 From: CharlesPikachu <1159254961@qq.com> Date: Tue, 10 Dec 2019 15:16:25 +0800 Subject: [PATCH] Unified comment --- Game1/Game1.py | 1 - Game10/Game10.py | 35 ++++++++++++++++---------------- Game11/Game11.py | 1 + Game12/Game12.py | 34 ++++++++++--------------------- Game13/Game13.py | 6 ++---- Game14/Game14.py | 1 + Game2/Game2.py | 8 ++++---- Game2/classes/block.py | 1 - Game2/classes/pikachu.py | 9 ++++----- Game4/Game4.py | 40 ++++++++++++++++++------------------- Game6/Bird.py | 10 +++++----- Game6/Game6.py | 3 ++- Game6/Pipe.py | 10 +++++----- Game7/Dinosaur.py | 10 +++++----- Game7/Game7.py | 7 ++++--- Game7/Obstacle.py | 16 +++++++-------- Game7/Scene.py | 8 ++++---- Game8/Game8.py | 3 ++- Game8/interface/CHOICE.py | 22 ++++++++++---------- Game8/interface/END.py | 10 +++++----- Game8/interface/GAMING.py | 42 +++++++++++++++++++-------------------- Game8/interface/PAUSE.py | 10 +++++----- Game8/interface/START.py | 16 +++++++-------- Game8/sprites/Arrow.py | 6 +++--- Game8/sprites/Enemy.py | 4 ++-- Game8/sprites/Turret.py | 6 +++--- Game9/Game9.py | 23 ++++++++++----------- 27 files changed, 163 insertions(+), 179 deletions(-) diff --git a/Game1/Game1.py b/Game1/Game1.py index add3325..cc515a2 100644 --- a/Game1/Game1.py +++ b/Game1/Game1.py @@ -1,4 +1,3 @@ -# coding: utf-8 ''' Function: Bunnies and Badgers diff --git a/Game10/Game10.py b/Game10/Game10.py index 5bd6c41..e2e109c 100644 --- a/Game10/Game10.py +++ b/Game10/Game10.py @@ -16,7 +16,7 @@ HEIGHT = 560 -# 定义按钮 +'''定义按钮''' def BUTTON(screen, position, text): bwidth = 310 bheight = 65 @@ -31,7 +31,7 @@ def BUTTON(screen, position, text): return screen.blit(text_render, (left+50, top+10)) -# 开始界面 +'''开始界面''' def start_interface(screen): clock = pygame.time.Clock() while True: @@ -50,7 +50,7 @@ def start_interface(screen): pygame.display.update() -# 子弹 +'''子弹''' class Bullet(pygame.sprite.Sprite): def __init__(self, idx, position): pygame.sprite.Sprite.__init__(self) @@ -65,16 +65,16 @@ def __init__(self, idx, position): self.speed = 8 # 玩家编号 self.playerIdx = idx - # 移动子弹 + '''移动子弹''' def move(self): self.position = self.position[0], self.position[1] - self.speed self.rect.left, self.rect.top = self.position - # 画子弹 + '''画子弹''' def draw(self, screen): screen.blit(self.image, self.rect) -# 小行星 +'''小行星''' class Asteroid(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) @@ -89,11 +89,11 @@ def __init__(self): self.angle = 0 self.angular_velocity = random.randrange(1, 5) self.rotate_ticks = 3 - # 移动小行星 + '''移动小行星''' def move(self): self.position = self.position[0], self.position[1] + self.speed self.rect.left, self.rect.top = self.position - # 转动小行星 + '''转动小行星''' def rotate(self): self.rotate_ticks -= 1 if self.rotate_ticks == 0: @@ -105,12 +105,12 @@ def rotate(self): rot_image = rot_image.subsurface(rot_rect).copy() self.image = rot_image self.rotate_ticks = 3 - # 画小行星 + '''画小行星''' def draw(self, screen): screen.blit(self.image, self.rect) -# 飞船 +'''飞船''' class Ship(pygame.sprite.Sprite): def __init__(self, idx): pygame.sprite.Sprite.__init__(self) @@ -129,12 +129,12 @@ def __init__(self, idx): self.cooling_time = 0 # 爆炸用 self.explode_step = 0 - # 飞船爆炸 + '''飞船爆炸''' def explode(self, screen): img = self.explode_img.subsurface((48*(self.explode_step-1), 0), (48, 48)) screen.blit(img, (self.position['x'], self.position['y'])) self.explode_step += 1 - # 移动飞船 + '''移动飞船''' def move(self, direction): if direction == 'left': self.position['x'] = max(-self.speed['x']+self.position['x'], -10) @@ -145,15 +145,15 @@ def move(self, direction): elif direction == 'down': self.position['y'] = min(self.speed['y']+self.position['y'], 520) self.rect.left, self.rect.top = self.position['x'], self.position['y'] - # 画飞船 + '''画飞船''' def draw(self, screen): screen.blit(self.image, self.rect) - # 射击 + '''射击''' def shot(self): return Bullet(self.playerIdx, (self.rect.center[0] - 5, self.position['y'] - 5)) -# 游戏界面 +'''游戏界面''' def GameDemo(num_player, screen): pygame.mixer.music.load(("./resources/sounds/Cool Space Music.mp3")) pygame.mixer.music.set_volume(0.4) @@ -284,7 +284,7 @@ def GameDemo(num_player, screen): clock.tick(60) -# 结束界面 +'''结束界面''' def end_interface(screen): clock = pygame.time.Clock() while True: @@ -304,7 +304,7 @@ def end_interface(screen): pygame.display.update() -# 主函数 +'''主函数''' def main(): pygame.init() pygame.font.init() @@ -322,5 +322,6 @@ def main(): end_interface(screen) +'''run''' if __name__ == '__main__': main() \ No newline at end of file diff --git a/Game11/Game11.py b/Game11/Game11.py index af9e5aa..afc0a4d 100644 --- a/Game11/Game11.py +++ b/Game11/Game11.py @@ -114,6 +114,7 @@ def keyPressEvent(self, event): self.updateWindow() +'''run''' if __name__ == '__main__': app = QApplication([]) tetris = TetrisGame() diff --git a/Game12/Game12.py b/Game12/Game12.py index b60aa8b..d8b9e11 100644 --- a/Game12/Game12.py +++ b/Game12/Game12.py @@ -14,19 +14,13 @@ from itertools import chain -''' -Function: - 退出游戏 -''' +'''退出游戏''' def quitGame(): pygame.quit() sys.exit(0) -''' -Function: - 游戏地图 -''' +'''游戏地图''' class gameMap(): def __init__(self, num_cols, num_rows): self.walls = [] @@ -78,10 +72,7 @@ def getBox(self, col, row): return None -''' -Function: - 游戏界面 -''' +'''游戏界面''' class gameInterface(): def __init__(self, screen): self.screen = screen @@ -139,10 +130,7 @@ def scroll(self): self.scroll_y += 2 -''' -Function: - 某一关卡的游戏主循环 -''' +'''某一关卡的游戏主循环''' def runGame(screen, game_level): clock = pygame.time.Clock() game_interface = gameInterface(screen) @@ -215,7 +203,7 @@ def runGame(screen, game_level): clock.tick(100) -# 定义按钮 +'''定义按钮''' def BUTTON(screen, position, text): bwidth = 310 bheight = 65 @@ -231,7 +219,7 @@ def BUTTON(screen, position, text): return screen.blit(text_render, (left+50, top+10)) -# 开始界面 +'''开始界面''' def startInterface(screen): screen.fill(Config.get('bg_color')) clock = pygame.time.Clock() @@ -251,7 +239,7 @@ def startInterface(screen): pygame.display.update() -# 关卡切换界面 +'''关卡切换界面''' def switchInterface(screen): screen.fill(Config.get('bg_color')) clock = pygame.time.Clock() @@ -271,7 +259,7 @@ def switchInterface(screen): pygame.display.update() -# 结束界面 +'''结束界面''' def endInterface(screen): screen.fill(Config.get('bg_color')) clock = pygame.time.Clock() @@ -289,10 +277,7 @@ def endInterface(screen): pygame.display.update() -''' -Function: - 主函数 -''' +'''主函数''' def main(): pygame.init() pygame.mixer.init() @@ -311,5 +296,6 @@ def main(): endInterface(screen) +'''run''' if __name__ == '__main__': main() \ No newline at end of file diff --git a/Game13/Game13.py b/Game13/Game13.py index 202ac84..1f8d74c 100644 --- a/Game13/Game13.py +++ b/Game13/Game13.py @@ -23,10 +23,7 @@ FPS = 60 -''' -Function: - 开始游戏 -''' +'''开始游戏''' def startGame(screen): clock = pygame.time.Clock() # 加载字体 @@ -236,5 +233,6 @@ def main(): endInterface(screen, BLACK, is_win) +'''run''' if __name__ == '__main__': main() \ No newline at end of file diff --git a/Game14/Game14.py b/Game14/Game14.py index a74a02c..0bd8351 100644 --- a/Game14/Game14.py +++ b/Game14/Game14.py @@ -184,5 +184,6 @@ def main(screen): showText(screen, font_big, is_clearance) +'''test''' if __name__ == '__main__': main(initialize()) \ No newline at end of file diff --git a/Game2/Game2.py b/Game2/Game2.py index b576596..66391b3 100644 --- a/Game2/Game2.py +++ b/Game2/Game2.py @@ -1,4 +1,3 @@ -# coding: utf-8 ''' Function: 仿八分音符的声控小游戏 @@ -50,7 +49,7 @@ def __init__(self): input=True, frames_per_buffer=self.numSamples) self.schedule(self.update) - # 碰撞检测 + '''碰撞检测''' def collide(self): diffx = self.pikachu.x - self.floor.x for b in self.floor.get_children(): @@ -58,7 +57,7 @@ def collide(self): if self.pikachu.y < b.height: self.pikachu.land(b.height) break - # 定义游戏规则 + '''定义游戏规则''' def update(self, dt): # 获取每帧的音量 audio_data = self.stream.read(self.numSamples) @@ -69,11 +68,12 @@ def update(self, dt): if k > 8000: self.pikachu.jump((k - 8000) / 1000.0) self.collide() - # 重置 + '''重置''' def reset(self): self.floor.x = 0 +'''run''' if __name__ == '__main__': cocos.director.director.init(caption="Pikachu~~~") cocos.director.director.run(cocos.scene.Scene(VCGame())) \ No newline at end of file diff --git a/Game2/classes/block.py b/Game2/classes/block.py index 16a0ccf..fe410c9 100644 --- a/Game2/classes/block.py +++ b/Game2/classes/block.py @@ -1,4 +1,3 @@ -# coding: utf-8 ''' Function: 定义block类,用于随机生成地面块 diff --git a/Game2/classes/pikachu.py b/Game2/classes/pikachu.py index b36968a..ebb9dd6 100644 --- a/Game2/classes/pikachu.py +++ b/Game2/classes/pikachu.py @@ -1,4 +1,3 @@ -# coding: utf-8 ''' Function: 定义皮卡丘类(Define Pikachu Class) @@ -23,25 +22,25 @@ def __init__(self): # 皮卡丘的位置 self.position = 80, 280 self.schedule(self.update) - # 声控跳跃 + '''声控跳跃''' def jump(self, h): if self.able_jump: self.y += 1 self.speed -= max(min(h, 10), 7) self.able_jump = False - # 着陆后静止 + '''着陆后静止''' def land(self, y): if self.y > y - 25: self.able_jump = True self.speed = 0 self.y = y - # 更新(重力下降) + '''更新(重力下降)''' def update(self, dt): self.speed += 10 * dt self.y -= self.speed if self.y < -85: self.reset() - # 重置 + '''重置''' def reset(self): self.parent.reset() self.able_jump = False diff --git a/Game4/Game4.py b/Game4/Game4.py index 518ed68..4f7c545 100644 --- a/Game4/Game4.py +++ b/Game4/Game4.py @@ -1,4 +1,3 @@ -# coding: utf-8 ''' Function: 滑雪小游戏 @@ -13,7 +12,7 @@ from pygame.locals import * -# 滑雪者类 +'''滑雪者类''' class SkierClass(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) @@ -24,8 +23,7 @@ def __init__(self): self.rect = self.person.get_rect() self.rect.center = [320, 100] self.speed = [self.direction, 6-abs(self.direction)*2] - # 改变滑雪者的朝向 - # 负数为向左,正数为向右,0为向前 + '''改变滑雪者的朝向. 负数为向左,正数为向右,0为向前''' def turn(self, num): self.direction += num self.direction = max(-2, self.direction) @@ -36,18 +34,21 @@ def turn(self, num): self.rect.center = center self.speed = [self.direction, 6-abs(self.direction)*2] return self.speed - # 移动滑雪者 + '''移动滑雪者''' def move(self): self.rect.centerx += self.speed[0] self.rect.centerx = max(20, self.rect.centerx) self.rect.centerx = min(620, self.rect.centerx) -# 障碍物类 -# Input: -# -img_path: 障碍物图片路径 -# -location: 障碍物位置 -# -attribute: 障碍物类别属性 +''' +Function: + 障碍物类 +Input: + -img_path: 障碍物图片路径 + -location: 障碍物位置 + -attribute: 障碍物类别属性 +''' class ObstacleClass(pygame.sprite.Sprite): def __init__(self, img_path, location, attribute): pygame.sprite.Sprite.__init__(self) @@ -58,12 +59,12 @@ def __init__(self, img_path, location, attribute): self.rect.center = self.location self.attribute = attribute self.passed = False - # 移动 + '''移动''' def move(self, num): self.rect.centery = self.location[1] - num -# 创建障碍物 +'''创建障碍物''' def create_obstacles(s, e, num=10): obstacles = pygame.sprite.Group() locations = [] @@ -80,7 +81,7 @@ def create_obstacles(s, e, num=10): return obstacles -# 合并障碍物 +'''合并障碍物''' def AddObstacles(obstacles0, obstacles1): obstacles = pygame.sprite.Group() for obstacle in obstacles0: @@ -90,7 +91,7 @@ def AddObstacles(obstacles0, obstacles1): return obstacles -# 显示游戏开始界面 +'''显示游戏开始界面''' def Show_Start_Interface(Demo, width, height): Demo.fill((255, 255, 255)) tfont = pygame.font.Font('./font/simkai.ttf', width//4) @@ -112,11 +113,9 @@ def Show_Start_Interface(Demo, width, height): return -# 主程序 +'''主程序''' def main(): - ''' - 初始化 - ''' + # 初始化 pygame.init() # 声音 pygame.mixer.init() @@ -144,9 +143,6 @@ def main(): # 速度 speed = [0, 6] Show_Start_Interface(screen, 640, 640) - ''' - 主循环 - ''' # 更新屏幕 def update(): screen.fill([255, 255, 255]) @@ -154,6 +150,7 @@ def update(): screen.blit(skier.person, skier.rect) screen.blit(score_text, [10, 10]) pygame.display.flip() + # 主循环 while True: # 左右键控制人物方向 for event in pygame.event.get(): @@ -201,5 +198,6 @@ def update(): clock.tick(40) +'''run''' if __name__ == '__main__': main() \ No newline at end of file diff --git a/Game6/Bird.py b/Game6/Bird.py index ec02c7e..eaad0dd 100644 --- a/Game6/Bird.py +++ b/Game6/Bird.py @@ -9,7 +9,7 @@ import pygame -# Bird类 +'''Bird类''' class Bird(pygame.sprite.Sprite): def __init__(self, HEIGHT, WIDTH): pygame.sprite.Sprite.__init__(self) @@ -38,19 +38,19 @@ def __init__(self, HEIGHT, WIDTH): self.x = 150 self.y = (self.HEIGHT - self.ori_bird.get_height()) / 2 self.set_bird() - # 设置小鸟的位置 + '''设置小鸟的位置''' def set_bird(self): self.rotated_bird = pygame.transform.rotate(self.ori_bird, self.angle) delta_width = (self.rotated_bird.get_rect().width - self.ori_bird.get_rect().width) / 2 delta_height = (self.rotated_bird.get_rect().width - self.ori_bird.get_rect().height) / 2 self.rect.left, self.rect.top = self.x - delta_width, self.y - delta_height - # 判断小鸟是否死亡 + '''判断小鸟是否死亡''' def is_dead(self): if self.y >= self.HEIGHT: return True else: return False - # 更新小鸟 + '''更新小鸟''' def update(self, time_passed): if self.is_jump: if self.angle < self.max_angle: @@ -70,7 +70,7 @@ def update(self, time_passed): return self.y += self.down_speed * time_passed self.set_bird() - # 重置 + '''重置''' def reset(self): self.angle = 0 self.cur_jump_height = 0 diff --git a/Game6/Game6.py b/Game6/Game6.py index 705a4db..d068bea 100644 --- a/Game6/Game6.py +++ b/Game6/Game6.py @@ -16,7 +16,7 @@ WIDTH, HEIGHT = 640, 480 -# 主函数 +'''主函数''' def main(): # 初始化 pygame.init() @@ -103,5 +103,6 @@ def main(): exit(0) +'''run''' if __name__ == "__main__": main() \ No newline at end of file diff --git a/Game6/Pipe.py b/Game6/Pipe.py index 14389fd..d1299f5 100644 --- a/Game6/Pipe.py +++ b/Game6/Pipe.py @@ -10,7 +10,7 @@ import random -# 管道头 +'''管道头''' class pipeHead(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) @@ -21,7 +21,7 @@ def __init__(self): self.width = self.pipe_head.get_width() -# 管道体 +'''管道体''' class pipeBody(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) @@ -32,7 +32,7 @@ def __init__(self): self.width = self.pipe_body.get_width() -# 管道类 +'''管道类''' class Pipe(): def __init__(self, HEIGHT, WIDTH): # 游戏界面宽高 @@ -53,7 +53,7 @@ def __init__(self, HEIGHT, WIDTH): # 小鸟通过该Pipe后变为True,防止重复加分 self.add_score = False self.construct_pipe() - # 用管道体和管道头构建管道 + '''用管道体和管道头构建管道''' def construct_pipe(self): # 管道 self.pipe = pygame.sprite.Group() @@ -73,7 +73,7 @@ def construct_pipe(self): pipe_head = pipeHead() pipe_head.rect.left, pipe_head.rect.top = self.x - (pipeHead().width - pipeBody().width) / 2, self.HEIGHT - self.n_down_pipe_body * pipeBody().height - pipeHead().height self.pipe.add(pipe_head) - # 更新管道 + '''更新管道''' def update(self, time_passed): self.x -= time_passed * self.speed self.construct_pipe() \ No newline at end of file diff --git a/Game7/Dinosaur.py b/Game7/Dinosaur.py index 567d2ff..5efefa3 100644 --- a/Game7/Dinosaur.py +++ b/Game7/Dinosaur.py @@ -9,7 +9,7 @@ import pygame -# 恐龙类 +'''恐龙类''' class Dinosaur(pygame.sprite.Sprite): def __init__(self, WIDTH=640, HEIGHT=500): pygame.sprite.Sprite.__init__(self) @@ -18,7 +18,7 @@ def __init__(self, WIDTH=640, HEIGHT=500): # self.imgs = ['./images/dinosaur/wait.png', './images/dinosaur/afraid.png', './images/dinosaur/running.png', './images/dinosaur/flying.png'] self.imgs = ['./images/dinosaur/dino.png', './images/dinosaur/dino_ducking.png'] self.reset() - # 跳跃 + '''跳跃''' def jump(self, time_passed): # time_passed很小时,可近似为匀速运动 if self.is_jumping_up: @@ -33,10 +33,10 @@ def jump(self, time_passed): self.is_jumping = False self.is_jumping_up = True self.jump_v = self.jump_v0 - # 跳跃时变为感到恐惧的表情 + '''跳跃时变为感到恐惧的表情''' def be_afraid(self): self.dinosaur = self.dinosaurs.subsurface((352, 0), (88, 95)) - # 把自己画到屏幕上去 + '''把自己画到屏幕上去''' def draw(self, screen): if self.is_running and not self.is_jumping: self.running_count += 1 @@ -48,7 +48,7 @@ def draw(self, screen): else: self.dinosaur = self.dinosaurs.subsurface((264, 0), (88, 95)) screen.blit(self.dinosaur, self.rect) - # 重置 + '''重置''' def reset(self): # 恐龙是否在奔跑 self.is_running = False diff --git a/Game7/Game7.py b/Game7/Game7.py index 637d916..637f7cc 100644 --- a/Game7/Game7.py +++ b/Game7/Game7.py @@ -23,7 +23,7 @@ HEIGHT = 400 -# 显示Gameover界面 +'''显示Gameover界面''' def show_gameover(screen): screen.fill(BACKGROUND) gameover_img = pygame.image.load('./images/others/gameover.png').convert_alpha() @@ -47,13 +47,13 @@ def show_gameover(screen): return True -# 将Score转为生成障碍物的概率 +'''将Score转为生成障碍物的概率''' def sigmoid(score): probability = 1 / (1 + math.exp(-score)) return min(probability, 0.6) -# 主函数 +'''主函数''' def main(): # 初始化 pygame.init() @@ -158,6 +158,7 @@ def main(): return res +'''run''' if __name__ == '__main__': res = True while res: diff --git a/Game7/Obstacle.py b/Game7/Obstacle.py index 21ac7e2..34763f0 100644 --- a/Game7/Obstacle.py +++ b/Game7/Obstacle.py @@ -10,7 +10,7 @@ import pygame -# 植物 +'''植物''' class Plant(pygame.sprite.Sprite): def __init__(self, WIDTH=640, HEIGHT=500): pygame.sprite.Sprite.__init__(self) @@ -22,7 +22,7 @@ def __init__(self, WIDTH=640, HEIGHT=500): # self.imgs = ['./images/obstacles/plant1.png', './images/obstacles/plant2.png', './images/obstacles/plant3.png', './images/obstacles/plant4.png'] self.imgs = ['./images/obstacles/plant_big.png', './images/obstacles/plant_small.png'] self.generate_random() - # 随机生成障碍物 + '''随机生成障碍物''' def generate_random(self): idx = random.randint(0, 1) temp = pygame.image.load(self.imgs[idx]).convert_alpha() @@ -32,15 +32,15 @@ def generate_random(self): self.plant = temp.subsurface((68*random.randint(0, 2), 0), (68, 70)) self.rect = self.plant.get_rect() self.rect.left, self.rect.top = self.WIDTH+60, int(self.HEIGHT/2) - # 不停往左移动 + '''不停往左移动''' def move(self): self.rect.left = self.rect.left-self.speed - # 把自己画到屏幕上去 + '''把自己画到屏幕上去''' def draw(self, screen): screen.blit(self.plant, self.rect) -# 飞龙 +'''飞龙''' class Ptera(pygame.sprite.Sprite): def __init__(self, WIDTH=640, HEIGHT=500): pygame.sprite.Sprite.__init__(self) @@ -55,17 +55,17 @@ def __init__(self, WIDTH=640, HEIGHT=500): # 统计分数时用的 self.speed = 7 self.generate() - # 生成飞龙 + '''生成飞龙''' def generate(self): self.ptera = pygame.image.load(self.imgs[0]).convert_alpha() self.ptera_0 = self.ptera.subsurface((0, 0), (92, 81)) self.ptera_1 = self.ptera.subsurface((92, 0), (92, 81)) self.rect = self.ptera_0.get_rect() self.rect.left, self.rect.top = self.WIDTH+30, int(self.HEIGHT/20) - # 不停往左移动 + '''不停往左移动''' def move(self): self.rect.left = self.rect.left-self.speed - # 把自己画到屏幕上去 + '''把自己画到屏幕上去''' def draw(self, screen): self.flying_count += 1 if self.flying_count % 6 == 0: diff --git a/Game7/Scene.py b/Game7/Scene.py index b5c849d..b5f9e58 100644 --- a/Game7/Scene.py +++ b/Game7/Scene.py @@ -10,7 +10,7 @@ import random -# 场景类 +'''场景类''' class Scene(pygame.sprite.Sprite): def __init__(self, WIDTH=640, HEIGHT=500): pygame.sprite.Sprite.__init__(self) @@ -19,10 +19,10 @@ def __init__(self, WIDTH=640, HEIGHT=500): self.speed = 5 self.imgs = ['./images/bg/bg1.png', './images/bg/bg2.png', './images/bg/bg3.png'] self.reset() - # 不停向左移动 + '''不停向左移动''' def move(self): self.x = self.x - self.speed - # 把自己画到屏幕上去 + '''把自己画到屏幕上去''' def draw(self, screen): if self.bg1_rect.right < 0: self.x = 0 @@ -38,7 +38,7 @@ def draw(self, screen): screen.blit(self.bg1, self.bg1_rect) screen.blit(self.bg2, self.bg2_rect) screen.blit(self.bg3, self.bg3_rect) - # 重置 + '''重置''' def reset(self): self.x = 0 self.bg1 = pygame.image.load(self.imgs[0]).convert_alpha() diff --git a/Game8/Game8.py b/Game8/Game8.py index d6b0dab..131cd13 100644 --- a/Game8/Game8.py +++ b/Game8/Game8.py @@ -15,7 +15,7 @@ HEIGHT = 600 -# 主函数 +'''主函数''' def main(): pygame.init() pygame.mixer.init() @@ -40,5 +40,6 @@ def main(): end_interface.update(screen) +'''run''' if __name__ == '__main__': main() \ No newline at end of file diff --git a/Game8/interface/CHOICE.py b/Game8/interface/CHOICE.py index e413d0b..cde6e91 100644 --- a/Game8/interface/CHOICE.py +++ b/Game8/interface/CHOICE.py @@ -10,7 +10,7 @@ import pygame -# 游戏地图选择界面 +'''游戏地图选择界面''' class MapChoiceInterface(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -18,12 +18,12 @@ def __init__(self, WIDTH, HEIGHT): self.image = pygame.image.load(self.imgs[0]).convert() self.rect = self.image.get_rect() self.rect.topleft = (0, 0) - # just pass + '''just pass''' def update(self): pass -# 地图1 +'''地图1''' class MapButton1(pygame.sprite.Sprite): def __init__(self, position=(175, 240)): pygame.sprite.Sprite.__init__(self) @@ -41,7 +41,7 @@ def update(self): self.image = self.img_1 -# 地图2 +'''地图2''' class MapButton2(pygame.sprite.Sprite): def __init__(self, position=(400, 240)): pygame.sprite.Sprite.__init__(self) @@ -59,7 +59,7 @@ def update(self): self.image = self.img_1 -# 地图3 +'''地图3''' class MapButton3(pygame.sprite.Sprite): def __init__(self, position=(625, 240)): pygame.sprite.Sprite.__init__(self) @@ -77,7 +77,7 @@ def update(self): self.image = self.img_1 -# 信息显示框 +'''信息显示框''' class InfoBox(pygame.sprite.Sprite): def __init__(self, position=(400, 475)): pygame.sprite.Sprite.__init__(self) @@ -98,7 +98,7 @@ def update(self, MBs): break -# 简单按钮 +'''简单按钮''' class EasyButton(pygame.sprite.Sprite): def __init__(self, position=(400, 150)): pygame.sprite.Sprite.__init__(self) @@ -128,7 +128,7 @@ def update(self): self.image = self.img_1 -# 中等难度按钮 +'''中等难度按钮''' class MediumButton(pygame.sprite.Sprite): def __init__(self, position=(400, 300)): pygame.sprite.Sprite.__init__(self) @@ -158,7 +158,7 @@ def update(self): self.image = self.img_1 -# 困难难度按钮 +'''困难难度按钮''' class HardButton(pygame.sprite.Sprite): def __init__(self, position=(400, 450)): pygame.sprite.Sprite.__init__(self) @@ -188,7 +188,7 @@ def update(self): self.image = self.img_1 -# 游戏模式选择界面 +'''游戏模式选择界面''' class CHOICE(): def __init__(self, WIDTH, HEIGHT): # part1 @@ -201,7 +201,7 @@ def __init__(self, WIDTH, HEIGHT): self.EB = EasyButton() self.MB = MediumButton() self.HB = HardButton() - # 外部调用 + '''外部调用''' def update(self, screen): clock = pygame.time.Clock() # part1 diff --git a/Game8/interface/END.py b/Game8/interface/END.py index 04c7c9a..848f8a3 100644 --- a/Game8/interface/END.py +++ b/Game8/interface/END.py @@ -10,7 +10,7 @@ import pygame -# 游戏结束界面 +'''游戏结束界面''' class EndInterface(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -18,12 +18,12 @@ def __init__(self, WIDTH, HEIGHT): self.image = pygame.image.load(self.imgs[0]).convert() self.rect = self.image.get_rect() self.rect.center = (WIDTH/2, HEIGHT/2) - # just pass + '''just pass''' def update(self): pass -# 继续游戏按钮 +'''继续游戏按钮''' class ContinueButton(pygame.sprite.Sprite): def __init__(self, position=(400, 409)): pygame.sprite.Sprite.__init__(self) @@ -41,13 +41,13 @@ def update(self): self.image = self.img_1 -# 游戏结束类 +'''游戏结束类''' class END(): def __init__(self, WIDTH, HEIGHT): self.EI = EndInterface(WIDTH, HEIGHT) self.CB = ContinueButton() self.components = pygame.sprite.LayeredUpdates(self.EI, self.CB) - # 外部调用 + '''外部调用''' def update(self, screen): clock = pygame.time.Clock() background = pygame.Surface(screen.get_size()) diff --git a/Game8/interface/GAMING.py b/Game8/interface/GAMING.py index c331c07..5500ca9 100644 --- a/Game8/interface/GAMING.py +++ b/Game8/interface/GAMING.py @@ -32,7 +32,7 @@ button_color2 = (0, 100, 0) -# 游戏进行中界面 +'''游戏进行中界面''' class GAMING(): def __init__(self, WIDTH=800, HEIGHT=600): self.WIDTH = WIDTH @@ -109,7 +109,7 @@ def __init__(self, WIDTH=800, HEIGHT=600): Button(pygame.Rect((info_w+gap*5+button_w*4), button_y, button_w, button_h), 'Pause', self.pauseGame), Button(pygame.Rect((info_w+gap*6+button_w*5), button_y, button_w, button_h), 'Quit', self.quitGame) ] - # 开始游戏 + '''开始游戏''' def start(self, screen, map_path=None, difficulty_path=None): # 读取游戏难度对应的参数 with open(difficulty_path, 'r') as f: @@ -234,7 +234,7 @@ def start(self, screen, map_path=None, difficulty_path=None): del arrow break self.draw(screen, map_path) - # 将场景画到游戏界面上 + '''将场景画到游戏界面上''' def draw(self, screen, map_path): self.drawToolbar(screen) self.loadMap(screen, map_path) @@ -243,11 +243,11 @@ def draw(self, screen, map_path): self.drawEnemies(screen) self.drawArrows(screen) pygame.display.flip() - # 画出所有射出的箭 + '''画出所有射出的箭''' def drawArrows(self, screen): for arrow in self.arrowsGroup: screen.blit(arrow.image, arrow.rect) - # 画敌人 + '''画敌人''' def drawEnemies(self, screen): for enemy in self.EnemiesGroup: if enemy.life_value <= 0: @@ -275,11 +275,11 @@ def drawEnemies(self, screen): if greenLen < self.elementSize: pygame.draw.line(screen, red, (enemy.position[0]+greenLen, enemy.position[1]), (enemy.position[0]+self.elementSize, enemy.position[1]), 1) screen.blit(enemy.image, enemy.rect) - # 画已经建造好的炮塔 + '''画已经建造好的炮塔''' def drawBuiltTurret(self, screen): for turret in self.builtTurretGroup: screen.blit(turret.image, turret.rect) - # 画鼠标携带物 + '''画鼠标携带物''' def drawMouseCarried(self, screen): if self.mouseCarried: position = pygame.mouse.get_pos() @@ -294,7 +294,7 @@ def drawMouseCarried(self, screen): self.mouseCarried[1].rect.left, self.mouseCarried[1].rect.top = position else: screen.blit(self.mouseCarried[1], position) - # 画工具栏 + '''画工具栏''' def drawToolbar(self, screen): # 信息显示框 # 左 @@ -323,7 +323,7 @@ def drawToolbar(self, screen): buttonText_rect = buttonText.get_rect() buttonText_rect.center = (button.rect.centerx, button.rect.centery) screen.blit(buttonText, buttonText_rect) - # 显示被鼠标选中按钮的作用信息 + '''显示被鼠标选中按钮的作用信息''' def showSelectedInfo(self, screen, button): if button.text == 'T1': T1 = Turret.Turret(0) @@ -358,7 +358,7 @@ def showSelectedInfo(self, screen, button): elif button.text == 'Quit': selectedInfo = self.info_font.render('Quit game', True, white) screen.blit(selectedInfo, (self.rightinfo_rect.left+5, self.rightinfo_rect.top+35)) - # 出售炮塔(半价) + '''出售炮塔(半价)''' def sellTurret(self, position): coord = self.pos2coord(position) for turret in self.builtTurretGroup: @@ -367,7 +367,7 @@ def sellTurret(self, position): self.money += int(turret.price * 0.5) del turret break - # 建造炮塔 + '''建造炮塔''' def buildTurret(self, position): turret = self.mouseCarried[1] coord = self.pos2coord(position) @@ -388,26 +388,26 @@ def buildTurret(self, position): elif self.mouseCarried[1].turret_type == 2: self.mouseCarried = [] self.takeT3() - # 拿炮塔1 + '''拿炮塔1''' def takeT1(self): T1 = Turret.Turret(0) if self.money >= T1.price: self.mouseCarried = ['turret', T1] - # 拿炮塔2 + '''拿炮塔2''' def takeT2(self): T2 = Turret.Turret(1) if self.money >= T2.price: self.mouseCarried = ['turret', T2] - # 拿炮塔3 + '''拿炮塔3''' def takeT3(self): T3 = Turret.Turret(2) if self.money >= T3.price: self.mouseCarried = ['turret', T3] - # 出售炮塔 + '''出售炮塔''' def takeXXX(self): XXX = pygame.image.load('./resource/imgs/game/x.png') self.mouseCarried = ['XXX', XXX] - # 找下一个路径单元 + '''找下一个路径单元''' def find_next_path(self, enemy): x, y = enemy.coord # 优先级: 下右左上 @@ -416,13 +416,13 @@ def find_next_path(self, enemy): if (neighbour in self.path_list) and (neighbour not in enemy.reached_path): return neighbour return None - # 将真实坐标转为地图坐标, 20个单位长度的真实坐标=地图坐标 + '''将真实坐标转为地图坐标, 20个单位长度的真实坐标=地图坐标''' def pos2coord(self, position): return (position[0]//self.elementSize, position[1]//self.elementSize) - # 将地图坐标转为真实坐标, 20个单位长度的真实坐标=地图坐标 + '''将地图坐标转为真实坐标, 20个单位长度的真实坐标=地图坐标''' def coord2pos(self, coord): return (coord[0]*self.elementSize, coord[1]*self.elementSize) - # 导入地图 + '''导入地图''' def loadMap(self, screen, map_path): map_file = open(map_path, 'r') idx_j = -1 @@ -458,11 +458,11 @@ def loadMap(self, screen, map_path): pygame.draw.line(self.map_surface, red, (740+greenLen, 400), (740+nexus_width, 400), 3) screen.blit(self.map_surface, (0, 0)) map_file.close() - # 暂停游戏 + '''暂停游戏''' def pauseGame(self, screen): pause_interface = PAUSE.PAUSE(self.WIDTH, self.HEIGHT) pause_interface.update(screen) - # 退出游戏 + '''退出游戏''' def quitGame(self): sys.exit(0) pygame.quit() \ No newline at end of file diff --git a/Game8/interface/PAUSE.py b/Game8/interface/PAUSE.py index 9cc4713..432a899 100644 --- a/Game8/interface/PAUSE.py +++ b/Game8/interface/PAUSE.py @@ -10,7 +10,7 @@ import pygame -# 游戏暂停界面 +'''游戏暂停界面''' class PauseInterface(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -18,12 +18,12 @@ def __init__(self, WIDTH, HEIGHT): self.image = pygame.image.load(self.imgs[0]).convert() self.rect = self.image.get_rect() self.rect.center = (WIDTH/2, HEIGHT/2) - # just pass + '''just pass''' def update(self): pass -# 恢复游戏按钮 +'''恢复游戏按钮''' class ResumeButton(pygame.sprite.Sprite): def __init__(self, position=(391, 380)): pygame.sprite.Sprite.__init__(self) @@ -41,13 +41,13 @@ def update(self): self.image = self.img_1 -# 游戏暂停类 +'''游戏暂停类''' class PAUSE(): def __init__(self, WIDTH, HEIGHT): self.PI = PauseInterface(WIDTH, HEIGHT) self.RB = ResumeButton() self.components = pygame.sprite.LayeredUpdates(self.PI, self.RB) - # 外部调用 + '''外部调用''' def update(self, screen): clock = pygame.time.Clock() background = pygame.Surface(screen.get_size()) diff --git a/Game8/interface/START.py b/Game8/interface/START.py index 1d022b0..04f0813 100644 --- a/Game8/interface/START.py +++ b/Game8/interface/START.py @@ -10,7 +10,7 @@ import pygame -# 游戏开始界面 +'''游戏开始界面''' class StartInterface(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -18,12 +18,12 @@ def __init__(self, WIDTH, HEIGHT): self.image = pygame.image.load(self.imgs[0]).convert() self.rect = self.image.get_rect() self.rect.center = WIDTH/2, HEIGHT/2 - # just pass + '''just pass''' def update(self): pass -# 开始游戏按钮 +'''开始游戏按钮''' class PlayButton(pygame.sprite.Sprite): def __init__(self, position=(220, 415)): pygame.sprite.Sprite.__init__(self) @@ -33,7 +33,7 @@ def __init__(self, position=(220, 415)): self.image = self.img_1 self.rect = self.image.get_rect() self.rect.center = position - # 不断地更新检测鼠标是否在按钮上 + '''不断地更新检测鼠标是否在按钮上''' def update(self): mouse_pos = pygame.mouse.get_pos() if self.rect.collidepoint(mouse_pos): @@ -42,7 +42,7 @@ def update(self): self.image = self.img_1 -# 结束游戏按钮 +'''结束游戏按钮''' class QuitButton(pygame.sprite.Sprite): def __init__(self, position=(580, 415)): pygame.sprite.Sprite.__init__(self) @@ -52,7 +52,7 @@ def __init__(self, position=(580, 415)): self.image = self.img_1 self.rect = self.image.get_rect() self.rect.center = position - # 不断地更新检测鼠标是否在按钮上 + '''不断地更新检测鼠标是否在按钮上''' def update(self): mouse_pos = pygame.mouse.get_pos() if self.rect.collidepoint(mouse_pos): @@ -61,14 +61,14 @@ def update(self): self.image = self.img_1 -# 游戏开始类 +'''游戏开始类''' class START(): def __init__(self, WIDTH, HEIGHT): self.SI = StartInterface(WIDTH, HEIGHT) self.PB = PlayButton() self.QB = QuitButton() self.components = pygame.sprite.LayeredUpdates(self.SI, self.PB, self.QB) - # 外部调用 + '''外部调用''' def update(self, screen): clock = pygame.time.Clock() while True: diff --git a/Game8/sprites/Arrow.py b/Game8/sprites/Arrow.py index fcdcb95..49f904e 100644 --- a/Game8/sprites/Arrow.py +++ b/Game8/sprites/Arrow.py @@ -11,7 +11,7 @@ import pygame -# 箭类 +'''箭类''' class Arrow(pygame.sprite.Sprite): def __init__(self, arrow_type): assert arrow_type in range(3) @@ -35,11 +35,11 @@ def __init__(self, arrow_type): elif arrow_type == 2: self.speed = 7 self.attack_power = 15 - # 不停移动 + '''不停移动''' def move(self): self.position = self.position[0] - self.speed * math.cos(self.angle), self.position[1] - self.speed * math.sin(self.angle) self.rect.left, self.rect.top = self.position - # 重置箭的位置 + '''重置箭的位置''' def reset(self, position, angle=None): if angle is None: angle = random.random() * math.pi * 2 diff --git a/Game8/sprites/Enemy.py b/Game8/sprites/Enemy.py index ff77749..8dcebb2 100644 --- a/Game8/sprites/Enemy.py +++ b/Game8/sprites/Enemy.py @@ -9,7 +9,7 @@ import pygame -# 敌方类 +'''敌方类''' class Enemy(pygame.sprite.Sprite): def __init__(self, enemy_type): assert enemy_type in range(4) @@ -55,7 +55,7 @@ def __init__(self, enemy_type): self.speed = 0.2 self.reward = 500 self.damage = 4 - # 不停地移动 + '''不停地移动''' def move(self, cellLen): is_next_cell = False self.cell_move_dis += self.speed diff --git a/Game8/sprites/Turret.py b/Game8/sprites/Turret.py index fd59028..000c857 100644 --- a/Game8/sprites/Turret.py +++ b/Game8/sprites/Turret.py @@ -10,7 +10,7 @@ from sprites import Arrow -# 炮塔类 +'''炮塔类''' class Turret(pygame.sprite.Sprite): def __init__(self, turret_type): assert turret_type in range(3) @@ -26,7 +26,7 @@ def __init__(self, turret_type): self.position = 0, 0 self.rect.left, self.rect.top = self.position self.reset() - # 射击 + '''射击''' def shot(self, position, angle=None): arrow = None if not self.is_cooling: @@ -38,7 +38,7 @@ def shot(self, position, angle=None): if self.coolTime == 0: self.reset() return arrow - # 重置 + '''重置''' def reset(self): if self.turret_type == 0: # 价格 diff --git a/Game9/Game9.py b/Game9/Game9.py index 7d3b41a..315e804 100644 --- a/Game9/Game9.py +++ b/Game9/Game9.py @@ -12,7 +12,7 @@ import pygame -# 获取历史最高分 +'''获取历史最高分''' def getScore(): if os.path.isfile('score'): with open('score', 'r') as f: @@ -24,13 +24,13 @@ def getScore(): return score -# 保存分数(仅当超过历史最高分时) +'''保存分数(仅当超过历史最高分时)''' def saveScore(score): with open('score', 'w') as f: f.write(score) -# 定义接苹果/金币的人类精灵 +'''定义接苹果/金币的人类精灵''' class FarmerSprite(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -58,7 +58,7 @@ def __init__(self, WIDTH, HEIGHT): self.WIDTH, self.HEIGHT = WIDTH, HEIGHT # 激活一下 self.move() - # 移动 + '''移动''' def move(self, direction='left'): if direction != self.direction: self.direction = direction @@ -77,12 +77,12 @@ def move(self, direction='left'): self.rect.left = 0 if self.rect.left < 0 else self.rect.left self.rect.top = 0 if self.rect.top < 0 else self.rect.top self.rect.bottom = self.HEIGHT if self.rect.bottom > self.HEIGHT else self.rect.bottom - # 画上去 + '''画上去''' def draw(self, screen): screen.blit(self.image, self.rect) -# 掉落的食物类 +'''掉落的食物类''' class foodSprite(pygame.sprite.Sprite): def __init__(self, WIDTH, HEIGHT): pygame.sprite.Sprite.__init__(self) @@ -98,16 +98,16 @@ def __init__(self, WIDTH, HEIGHT): self.x = random.randint(0, WIDTH-self.rect.width) self.y = -50 self.rect.left, self.rect.top = self.x, self.y - # 移动 + '''移动''' def move(self): self.y += self.speed self.rect.top = self.y - # 画到屏幕上 + '''画到屏幕上''' def draw(self, screen): screen.blit(self.image, self.rect) -# 显示游戏结束界面 +'''显示游戏结束界面''' def GameOver(screen, width, height, score, highest): screen.fill((255, 255, 255)) tfont = pygame.font.Font('./font/simkai.ttf', width//10) @@ -129,7 +129,7 @@ def GameOver(screen, width, height, score, highest): return -# 主函数 +'''主函数''' def main(): # 初始化 pygame.init() @@ -213,7 +213,6 @@ def main(): clock.tick(60) - - +'''run''' if __name__ == '__main__': main() \ No newline at end of file