Python shooter game code copy and paste
Alright, this may be something where you need/desire/end up with multiple reviews - since changing this from it's current (procedural) style to OOP is (very) unlikely to be something someone new to OOP gets right first time. Show But, there are certain aspects of this code that could be made better even in a procedural style - so I'll start with these... DecompositionDecomposition, or "functional decomposition" is important in all structured programming paradigms (which includes both OOP and procedural programming). One of the most elementary steps in this process is to construct your programs using modularity and hierarchies. Structurally, using different scopes for different things is
important in creating conceptually seperated 'modules'. For example, in your program the variable For example, instead of:
(which are
ALL in the
This would mean, instead of using Note: in an object oriented design, you may wish to decompose these to a class or subclass rather than using a dictionary. This same decomposition concept could also be used in your twenty lines of sprite loading! Don't Repeat Yourself (DRY)Some of your code, for example the following block of sprite definitions, is VERY repetitive...
Generally, any time your have many lines like this that are largely the same, you're better off using some sort of loop to construct a data structure to contain them. For example:
This has several advantages;
It is fairly apparent that your game world is using square tiles, thus the x and y of the sprites (as oppose to the screens, such as 'background" You may also wish to alter your
to something which either uses default arguments (x and y defaulting to
On to the OOP!Your program currently has a number (7, actually) of global variables:
(That is, excluding the 8 colors and 18 sprites that I already mentioned). These pretty much ALL belong in some sort of class. Probably named something like Using the hierarchies idea from the
decomposition concept, this The line Each of the 3 global functions; Note: the Your main()... monster?This function is 153 lines of code, with a cyclomatic complexity of around 54! I really don't even want to begin on it until the program structure (everything else I've talked about) is improved. However, I will just say that you CLEARLY need decomposition here! Methods should
always have a complexity less than 7, so this function would need to be decomposed into around 8 other (smaller) functions. Although, from scanning over it and looking at all your loop conditions (like As I said at the start of this answer, this may be something where you need/desire/end up with multiple reviews. You can refactor your code and post another question with the new (OOP structured) version (then put a comment on this question pointing to it, etc). How do you write a game code in Python?Simple Pygame Example. import pygame.. pygame.init(). screen = pygame.display.set_mode((400,500)). done = False.. while not done:. for event in pygame.event.get():. if event.type == pygame.QUIT:. done = True.. Can Python make 3d games?You can actually develop games in Blender via Python. There are quite a few tutorials, I'll let you google around for the style you like.
Can you write a game using Python?Creating your own computer games in Python is a great way to learn the language. To build a game, you'll need to use many core programming skills. The kinds of skills that you'll see in real-world programming.
|