Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Code Reviews A simple game with pygame

Overall the structure seems sensible. Style It's Python style to have two blank lines after a function or method, not just one. You seem to be trying to line wrap before 80 characters. Maybe you...

posted 3y ago by Peter Taylor‭

Answer
#1: Initial revision by user avatar Peter Taylor‭ · 2021-09-20T22:36:36Z (about 3 years ago)
Overall the structure seems sensible.

### Style

It's Python style to have two blank lines after a function or method, not just one.

You seem to be trying to line wrap before 80 characters. Maybe you have a reason for that, but I don't think that's a sensible width in general any more. In fact, I think that PEP8 is far too conservative with the suggestion that a team might reach consensus to stretch to 99 chars. I wouldn't even consider wrapping a line before about 120 chars.


### Inelegant code

>             self.ship.draw_on(self.screen)
>     
>             for obj in self.moving_objects():
>                 obj.draw_on(self.screen)

This draws the ship twice.

----

>         def spawn_submarine(self):
>             "Possibly spawn a new submarine"
>             if random.uniform(0,1) < self.p_spawn:
>                 ship_speed = self.ship.speed

`ship_speed` isn't used here.

----

>             while self.running:
>                 self.draw()
>                 self.clock.tick(60)

Should that be `self.clock.tick(self.fps)`?


### State representation

There are two things which feel slightly clunky to me. The bigger one is the movement code in `MovingObject`. From a numerical analysis perspective it's preferable to use linear interpolation rather than increments of the base position, but physics engines tend to work with current position, velocity, and acceleration rather than current path and parameterised position along it. The principle of least surprise suggests that it would be worth adopting the same approach.

The other thing is the separate structures to hold the ship, the submarines, and the bombs. This means that you need a `moving_objects` method to concatenate, and have separate

>             self.submarines = [sub for sub in self.submarines if sub.is_active()]
>             self.bombs = [bomb for bomb in self.bombs if bomb.is_active()]

I wonder how inelegant it would be to have each `MovingObject` increment a counter (probably literally a `Counter` from `collections`) on creation and decrement it when deactivated. That would then handle the one thing which really benefits from separate lists, which is knowing how many submarines and bombs are currently active.


### Robustness

I bodged the downloads of the assets, and the first time I tried to test the game it broke with an error when it tried to load `Uboot.png` (I'd saved it as `UBoot.png` on a case-sensitive filesystem). It might be worth adding a startup check which loads all of the assets and fails early if some of them are missing.