ich benötige etwas Hilfe bei einem Breakout-Spiel.
Leider bekomme ich es nicht hin, den Ball zum bewegen/bouncen zu bringen.
Der Ball bleibt bewegungslos in der linken oberen Ecke.
Ich habe für den Ball eine Grafik als image eingesetzt.
In der Ball-Klasse möchte ich einen zufälligen Start-Wert für die Richtung erzeugen.
Vielleicht ist jemand so freundlich und sagt mir was ich falsch mache...
Vielen Dank!
main:
Code: Alles auswählen
# !/usr/bin/env python3
import pygame, random
from paddle import Paddle
from ball import Ball
from brick import Brick
pygame.init()
WHITE = (255, 255, 255)
DARKBLUE = (0, 0, 139)
ball_Width = 22
ball_Height = 22
Screen_Width = 800
Screen_Height = 600
screen = pygame.display.set_mode((Screen_Width, Screen_Height))
pygame.display.set_caption("Breakout")
def main():
try:
all_sprites = pygame.sprite.Group()
all_bricks = pygame.sprite.Group()
score = 0
lives = 5
pygame.mouse.set_visible(1)
paddle = Paddle(screen.get_rect())
all_sprites.add(paddle)
ball = Ball()
ball.rect.x = 600
ball.rect.y = 600
all_sprites.add(ball)
brick_coord_list = [64, 32, 64, 64]
i = 0
while i < len(brick_coord_list):
# Zufallszahl für blauen oder grünen Brick
randomnumber = random.randint(1, 2)
brick = Brick(randomnumber)
all_sprites.add(brick)
all_bricks.add(brick)
brick.rect.x = brick_coord_list[i]
brick.rect.y = brick_coord_list[i + 1]
i = i + 2
# to control how fast the screen updates
clock = pygame.time.Clock()
while True:
# Limit to 60 frames per second
clock.tick(60)
all_sprites.update()
ball.rect.x = ball.velocity[0]
ball.rect.y = ball.velocity[1]
# Check if the ball is bouncing against the 4 walls
if ball.rect.x >= Screen_Width:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x <= 0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y >= Screen_Height:
ball.velocity[1] = -ball.velocity[1]
if ball.rect.y >= 0:
lives -= 1
if lives == 0:
font = pygame.font.Font(None, 74)
text = font.render("Game over", 1, WHITE)
screen.blit(text, (250, 300))
pygame.display.flip()
pygame.time.wait(3000)
# stop the game
return
# Detect collisions between the ball and the paddle
# if pygame.sprite.spritecollide(ball, paddle):
if ball.rect.colliderect(paddle):
ball.rect.x -= ball.velocity[0]
ball.rect.y -= ball.velocity[1]
ball.bounce()
# Check if the ball collides with any of the bricks
brick_collision = pygame.sprite.spritecollide(ball, all_bricks, False)
for brick in brick_collision:
ball.bounce()
score += 1
brick.kill()
if len(all_bricks) == 0:
font = pygame.font.Font(None, 74)
text = font.render("Level complete", 1, WHITE)
screen.blit(text, (200, 300))
pygame.display.flip()
pygame.time.wait(3000)
# stop the game
return
screen.fill(DARKBLUE)
font = pygame.font.Font(None, 34)
text = font.render("Score: " + str(score), 1, WHITE)
screen.blit(text, (20, 10))
text = font.render("Lives: " + str(lives), 1, WHITE)
screen.blit(text, (650, 10))
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEMOTION:
paddle.update2(event.pos)
all_sprites.draw(screen)
# update the screen with what we have drawn
pygame.display.flip()
finally:
pygame.quit()
if __name__ == "__main__":
main()
Code: Alles auswählen
import pygame
from random import randint
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/ballblue.png')
self.rect = self.image.get_rect()
z1 = randint(8, 8)
z2 = randint(8, 8)
if z1 != 0:
pass
else:
z1 = 5
if z2 != 0:
pass
else:
z2 = 1
self.velocity = [z1, z2]
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.rect.x = -self.velocity[0]
self.rect.y = -self.velocity[1]
Code: Alles auswählen
import pygame
class Brick(pygame.sprite.Sprite):
# This class represents a brick. It derives from the "Sprite" class in Pygame.
def __init__(self, randomnumber):
super().__init__()
if randomnumber == 1:
self.image = pygame.image.load('images/greenbrick.png')
else:
self.image = pygame.image.load('images/bluebrick.png')
self.rect = self.image.get_rect()
Code: Alles auswählen
import pygame
BLACK = (0, 0, 0)
class Paddle(pygame.sprite.Sprite):
def __init__(self, playfield_rect):
super().__init__()
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((120, 10))
self.image.fill(BLACK)
self.playfield_rect = playfield_rect
self.rect = self.image.get_rect()
self.rect.bottom = self.playfield_rect.bottom
def update2(self, position):
x, _ = position
self.rect.centerx = x
self.rect = self.rect.clamp(self.playfield_rect)