HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Fast Paced Shooter (30 seconds)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fastshootersecondspaced

Problem

A fast paced shooter which counts and displays your kills. If you are to copy and paste this code also copy the images and save them as png files with names in the code



```
import pygame
import time
import random
import timeit

pygame.init()

display_width = 1600
display_height = 850
speed = [1, -1]

black = (0,0,0)
white = (255,255,255)
red = (255, 0, 0)
grey = (177,177,177)
blue = (0,0,255)
green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Kill hitler before he gets trump')

clock = pygame.time.Clock()

crosshairImg = pygame.image.load('crosshair.PNG')
hitlerImg = pygame.image.load('hitler.png')
donaldImg = pygame.image.load('donald.png')

def crosshair(x, y):
gameDisplay.blit(crosshairImg, (x,y))

def enemy(x, y):
gameDisplay.blit(hitlerImg, (x,y))

def friendly(x, y):
gameDisplay.blit(donaldImg, (x,y))
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',45)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)

pygame.display.update()

def text_objects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()

def status(total, enemy_kills):
if total == 0:
status = 0
else:
status = round((enemy_kills/total)*100, 2)

font = pygame.font.SysFont(None, 25)
text = font.render('Status:'+str(status)+'%', True, blue)
gameDisplay.blit(text,(0,30))

def score(count):
font = pygame.font.SysFont(None,25)
text = font.render('Confirmed Enemy Kills:'+str(count), True, blue)
gameDisplay.blit(text,(0,0))

def timer(count):
count = round(count, 1)
font = pygame.font.SysFont(None,25)
text = font.render('time left:'+str(count), True, blue)
gameDisplay.blit(text,(0,45))

def friend_kill(count):
font = pygame.font.SysFont(None,25)
text = font.render('

Solution

style

PEP8 asks that you use underscore in identifiers, e.g. donald_img.

Introducing some vertical space between friendly() and message_display() would be helpful. PEP8 asks for 2 blank lines.

In some lengthy conditionals, e.g. if (event.pos[0] > enemy_startx ..., you have an opportunity to take advantage of the open paren, so you can break the conditional into multiple lines, each a maximum of 80 characters.

def main()

Even for a game, it is customary to ensure that foo.py can be safely imported (import foo) without unfortunate side effects and without polluting the global namespace. This is usually accomplished by burying top-level statements in main(), invoked under if __name__ == '__main__':. The statements from pygame.init() through loading donald.png would benefit from being moved into main(). If those identifiers really do need to be in the global namespace, then names like DONALD_IMG would be more appropriate.

hoisting constants

In status(), maybe the font assignment should be a one-time global assignment. Oh, wait, I see that in several methods. I'm just saying that maybe evaluating SysFont() is more expensive than you'd like, and you would prefer to do it just once.

representing two dimensions

For friend and for enemy, there are numerous parallel X & Y quantities. This suggests modeling the quantities as (x, y), or (x_speed, y_speed), tuples.

Context

StackExchange Code Review Q#155232, answer score: 2

Revisions (0)

No revisions yet.