ich habe mein erstes, kleines Pygame-Spiel "Catch the Squares" fertiggestellt und würde mich um Feedback dazu freuen. Ist das Programm effizient umgesetzt oder gibt es grundsätzliche Dinge, die ich verbessern könnte?
Code: Alles auswählen
#!/usr/bin/env python
# Catch the Squares
# by Pythoguras
# Catch as much brown (good) squares (+1 point) as possible and avoid the
# radioactive (bad) squares (-1 point). Each collected radioactive square
# will increase the the spawn chance of following radioactive ones
# The game is lost when this chance reaches 100 % so the score cannot
#increase anymore.
import pygame
import sys
import os
import math
import random
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
IMAGE_PATH = 'Images'
SOUND_PATH = 'Sounds'
FPS = 30
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 700
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT
BOTTOM_MARGIN = 30
BGCOLOR = (0, 190, 255)
FONTCOLOR = (255, 0, 0)
class Player(pygame.sprite.Sprite):
def __init__(self, image, position):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = image.get_rect()
self.rect.center = position
def get_rect(self):
return self.rect
def draw(self, surface):
surface.blit(self.image, self.rect)
def update(self, player_direction, player_speed):
if player_direction != 0:
for step in range (player_speed):
if (self.rect.left + player_direction >= 5
and self.rect.right + player_direction < SCREEN_WIDTH - 5):
self.rect.x += player_direction
class Square(pygame.sprite.Sprite):
def __init__(self, image, spawn_point, is_bad_square):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = image.get_rect()
self.rect.topleft = spawn_point
self.is_bad_square = is_bad_square
def is_bad(self):
return self.is_bad_square
def draw(self, surface):
surface.blit(self.image, self.rect)
def update(self, square_speed):
if self.rect.bottom <= SCREEN_HEIGHT + self.rect.height:
for step in range(square_speed):
self.rect.bottom += 1
def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Catch the Squares')
player_image = pygame.image.load(os.path.join(IMAGE_PATH, "player.png"))
good_square_image = pygame.image.load(os.path.join(IMAGE_PATH,
"good_square.png"))
bad_good_square_image = pygame.image.load(os.path.join(IMAGE_PATH,
"bad_square.png"))
collision_sound = pygame.mixer.Sound(os.path.join(SOUND_PATH,
"collision.wav"))
font = pygame.font.Font(None, 28)
player = Player(player_image, (screen.get_rect().center[0],
SCREEN_HEIGHT - BOTTOM_MARGIN))
player_speed = 30
player_direction = 0
player_score = 0
squares = pygame.sprite.Group()
square_size = good_square_image.get_height()
square_speed = 17
bad_squares_chance = 15
frames_between_squares = 30 - square_speed
frame_counter = 0
frames_until_next_square = frames_between_squares
# Game Loop
while True:
screen.fill(BGCOLOR)
# Spawn a square
if frame_counter == frames_until_next_square:
spawn_point = (random.randint(0, SCREEN_WIDTH - square_size),
- square_size)
if random.randint(0, 100) <= bad_squares_chance:
square = Square(bad_good_square_image, spawn_point, 1)
else:
square = Square(good_square_image, spawn_point, 0)
squares.add(square)
frame_counter = 0
variation = random.randint(- frames_between_squares / 3,
frames_between_squares / 3)
frames_until_next_square = frames_between_squares + variation
# Player colliding with square
colliding_square = pygame.sprite.spritecollideany(player, squares)
if colliding_square:
if colliding_square.is_bad() and player_score > 0:
player_score -= 1
bad_squares_chance += 5
else:
player_score += 1
collision_sound.play()
squares.remove(colliding_square)
squares.update(square_speed)
squares.draw(screen)
player.update(player_direction, player_speed)
player.draw(screen)
player_score_text = font.render("Score: " + str(player_score),
1, FONTCOLOR)
screen.blit(player_score_text, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYUP:
if event.key in (K_RIGHT, K_d):
if left_pressed():
player_direction = -1
else:
player_direction = 0
elif event.key in (K_LEFT, K_a):
if right_pressed():
player_direction = 1
else:
player_direction = 0
elif event.type == KEYDOWN:
if event.key in (K_RIGHT, K_d):
if not left_pressed():
player_direction = 1
else:
player_direction = 0
elif event.key in (K_LEFT, K_a):
if not right_pressed():
player_direction = -1
else:
player_direction = 0
clock.tick(FPS)
frame_counter += 1
def left_pressed():
if pygame.key.get_pressed()[pygame.K_a] or\
pygame.key.get_pressed()[pygame.K_LEFT]:
return True
else:
return False
def right_pressed():
if pygame.key.get_pressed()[pygame.K_d] or\
pygame.key.get_pressed()[pygame.K_RIGHT]:
return True
else:
return False
if __name__ == '__main__':
main()
Vielen Dank im Voraus!
Mfg
Pythoguras