Conditions | 16 |
Total Lines | 70 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like game.Game.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import pygame |
||
67 | def main(self): |
||
68 | self.setup() |
||
69 | |||
70 | while self.run: |
||
71 | self.screen.fill((121, 201, 249), ((0, 0), (1280, 360))) |
||
72 | self.screen.fill((127, 173, 113), ((0, 593), (1280, 720))) |
||
73 | for background in self.backgrounds: |
||
74 | background.update() |
||
75 | self.screen.blit( |
||
76 | background.texture, |
||
77 | (background.rect.x, 350), |
||
78 | (0, 0, 1280, 360) |
||
79 | ) |
||
80 | |||
81 | for platform in self.platforms: |
||
82 | platform.check_hit_box(self) |
||
83 | platform.move(self) |
||
84 | self.screen.blit(platform.texture, platform.rect) |
||
85 | |||
86 | self.player.move() |
||
87 | |||
88 | self.player.apply_gravity() |
||
89 | |||
90 | if self.player.above(720): |
||
91 | self.over = True |
||
92 | |||
93 | self.screen.blit(self.player.texture, self.player.rect) |
||
94 | |||
95 | for bullet in self.bullets: |
||
96 | bullet.move(self) |
||
97 | self.screen.blit(bullet.texture, bullet.rect) |
||
98 | |||
99 | if bullet.check_hit_box(self.player): |
||
100 | self.over = True |
||
101 | break |
||
102 | |||
103 | if len(self.bullets) < 3: |
||
104 | self.bullets.append(Bullet()) |
||
105 | |||
106 | pygame.draw.rect( |
||
107 | self.screen, (255, 255, 255), ((5, 5), (1270, 710)), 3 |
||
108 | ) |
||
109 | |||
110 | for event in pygame.event.get(): |
||
111 | if event.type == pygame.KEYDOWN: |
||
112 | if event.key in self.events: |
||
113 | self.events[event.key]() |
||
114 | |||
115 | elif event.type == pygame.KEYUP: |
||
116 | if event.key == pygame.K_SPACE: |
||
117 | self.player.jump_available = True |
||
118 | |||
119 | elif event.key in [pygame.K_a, pygame.K_d]: |
||
120 | self.player.walk() |
||
121 | |||
122 | elif event.type == pygame.QUIT: |
||
123 | self.run = False |
||
124 | |||
125 | if self.over: |
||
126 | self.over_screen() |
||
127 | |||
128 | else: |
||
129 | pygame.display.set_caption( |
||
130 | f"Duck Jump 2 | score : {self.score}" |
||
131 | + f" | {self.clock.get_fps()} " * self.show_fps |
||
132 | ) |
||
133 | |||
134 | pygame.display.update() |
||
135 | self.score += int(self.player.ax) |
||
136 | self.clock.tick(self.cap_fps) |
||
137 | |||
235 |