ich weiß nicht, wie ich das Problem umgehen kann, bin erst seit 2 wochen dabei.
Schonmal danke für eure Verbesserungsvorschläge.
Code: Alles auswählen
import pygame
import random
import sys
import time
# general settings
pygame.init()
clock = pygame.time.Clock()
# colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
black = (0, 0, 0)
light_gray = (200, 200, 200)
light_red = (150, 0, 0)
light_green = (0, 150, 0)
dark_gray = (50, 50, 50)
blood = (102, 0, 0)
colors = [red, green, black, blue, white, light_gray]
# constants
FPS = 60
font = pygame.font.Font("freesansbold.ttf",17)
activ = False
class Game():
global font
def __init__(self):
self.player_speed = 0
self.ball_speed_x = 4 * random.choice((1, -1))
self.ball_speed_y = 4
self.score = 0
self.game_over = False
# window settings
self.width = 400
self.height = 800
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption("bounce em all")
# game rectangles
self.player = pygame.Rect(self.width / 2 - 20, 760, 40, 10)
self.ball = pygame.Rect(self.width / 2, 50, 10, 10)
self.left_border = pygame.Rect(0, 0, 10, self.height)
self.right_border = pygame.Rect(self.width - 10, 0, 10, self.height)
self.upper_border = pygame.Rect(0, 0, self.width, 10)
def text_object(text,font):
text_content = font.render(text, True, black)
return text_content, text_content.get_rect()
def test_method():
for x in range(1,4):
print(x)
#Buttons
def button(bx, by, label, lenght, tall, color_passiv, color_activ):
global activ
if mouse[0] >= bx and mouse[0] <= bx + lenght and mouse[1] >= by and mouse[1] <= by + tall:
pygame.draw.rect(self.screen, color_activ, (bx, by, lenght, tall))
if click[0] == 1 and activ == False:
activ = True
if label == "Try again":
test_method()
elif label == "Quit":
sys.exit()
else:
pygame.draw.rect(self.screen, color_passiv, (bx, by, lenght, tall))
text_ground, text_box = text_object(label, font)
text_box.center = ((bx +(lenght/ 2)), (by +(tall / 2)))
self.screen.blit(text_ground, text_box)
def draw_button():
button(50, 500, "Try again", 100, 50, light_green, green)
button(230, 500, "Quit", 100, 50, light_red, red)
# game loop
running = True
while running:
# input handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if self.game_over == False:
if event.key == pygame.K_RIGHT:
self.player_speed += 7
if event.key == pygame.K_LEFT:
self.player_speed -= 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.player_speed -= 7
if event.key == pygame.K_LEFT:
self.player_speed += 7
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# commands
self.player_movement()
self.ball_movement()
# visuals
self.screen.fill(white)
pygame.draw.rect(self.screen, red, self.player)
pygame.draw.rect(self.screen, dark_gray, self.left_border)
pygame.draw.rect(self.screen, dark_gray, self.right_border)
pygame.draw.rect(self.screen, dark_gray, self.upper_border)
pygame.draw.ellipse(self.screen, blue, self.ball)
if self.game_over == True:
draw_button()
#update and time
self.print_score()
if self.game_over == True:
self.print_game_over()
pygame.display.flip()
pygame.time.wait(10)
clock.tick(FPS)
#functions
def player_movement(self):
self.player.x += self.player_speed
if self.player.left <= 10:
self.player.left = 10
if self.player.right >= self.width - 10:
self.player.right = self.width - 10
def ball_movement(self):
self.ball.x += self.ball_speed_x
self.ball.y += self.ball_speed_y
# ball borders
if self.ball.colliderect(self.player):
self.ball_speed_y *= -1
self.score += 1
elif self.ball.colliderect(self.upper_border):
self.ball_speed_y *= -1
elif self.ball.colliderect(self.right_border):
self.ball_speed_x *= -1
elif self.ball.colliderect(self.left_border):
self.ball_speed_x *= -1
elif self.ball.y >= self.height:
self.game_over = True
def print_game_over(self):
go_font = pygame.font.Font("Youmurdererbb-pwoK.otf", 70)
go_text = go_font.render("Game Over", False, blood)
self.screen.blit(go_text, (80, 330))
def print_score(self):
score_font = pygame.font.Font("freesansbold.ttf", 17)
score_text = score_font.render("Score: " + str(self.score), True, black)
self.screen.blit(score_text, (14, 14))
game = Game()