Feb-09-2022, 07:27 PM
Hi,
I'm new to python/pygame and I'm trying to program a breakout clone.
I've got an error message: self = {Paddle} Unable to get repr for <class 'paddle.Paddle'>
I occurs in line 6 of class Paddle (super(Paddle, self).__init__()).
I'm not experienced and don't understand this error...
Please be so kind and help me...
Thanks a lot...
I'm new to python/pygame and I'm trying to program a breakout clone.
I've got an error message: self = {Paddle} Unable to get repr for <class 'paddle.Paddle'>
I occurs in line 6 of class Paddle (super(Paddle, self).__init__()).
I'm not experienced and don't understand this error...
Please be so kind and help me...
Thanks a lot...
import pygame class Paddle(pygame.sprite.Sprite): def __init__(self, playfield_rect): super(Paddle, self).__init__() self.image = pygame.image.load('images/ship_fertig.png').convert() self.rect = self.image.get_rect() self.rect.centerx = self.rect.x self.playfield_rect = playfield_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)
# !/usr/bin/env python3 import pygame, random, math from ball import Ball from brick import Brick from paddle import Paddle pygame.init() WHITE = (255, 255, 255) DARKBLUE = (0, 0, 139) Screen_Width = 800 Screen_Height = 600 screen = pygame.display.set_mode((Screen_Width, Screen_Height)) pygame.display.set_caption("Breakout") pygame.mixer.init() pygame.mixer.music.load('images/Ketsa - Holding The Line.mp3') pygame.mixer.music.set_volume(0.7) pygame.mixer.music.play(-1) def main(): try: paddle = Paddle(screen.get_rect()) ball = Ball() score = 0 lives = 5 brickgroup = pygame.sprite.Group() brick_coord_list = [ 32, 64, 32, 96, 32, 128, 32, 160 ] i = 0 while i < len(brick_coord_list): brick = Brick(brick_coord_list[i], brick_coord_list[i + 1]) brickgroup.add(brick) i = i + 2 paddlegroup = pygame.sprite.Group() paddlegroup.add(paddle) ballgroup = pygame.sprite.Group() ballgroup.add(ball) all_spritesgroup = pygame.sprite.Group() all_spritesgroup.add(paddlegroup, ballgroup, brickgroup) # to control how fast the screen updates clock = pygame.time.Clock() while True: # Limit to 60 frames per second clock.tick(60) # update object lives = ball.move(lives) 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 # check for collisions # ball / paddle if ball.collpaddle(paddlegroup): ball.bounce() # ball / brick for brick in brickgroup: if ball.collbrick(brickgroup): ball.bounce() score = brick.takehit(score) if len(brickgroup) == 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_spritesgroup.draw(screen) pygame.display.flip() finally: pygame.quit() if __name__ == "__main__": main()
import math import random, pygame Screen_Width = 800 Screen_Height = 600 class Ball(pygame.sprite.Sprite): def __init__(self): super(Ball, self).__init__() self.image = pygame.image.load('images/ballblue.png').convert() self.rect = self.image.get_rect() self.rect.x = 10 self.rect.y = 300 self.x = self.rect.x self.y = self.rect.y # random direction of the ball self.dx = random.randint(4, 6) self.dy = random.randint(4, 6) if self.dx != 0: pass else: self.dx = 4 if self.dy != 0: pass else: self.dy = 4 self.width = 15 self.height = 15 def move(self, lives): self.x = self.x + self.dx self.y = self.y + self.dy # Check border collision if self.x >= Screen_Width - self.width / 2: self.dx = -self.dx if self.x <= 0: self.dx = -self.dx if self.y <= 0: self.dy = -self.dy if self.y >= Screen_Height - self.height / 2: self.bounce() lives -= 1 return lives def bounce(self): self.dx = self.dx self.dy = -self.dy def collpaddle(self, a): collide = pygame.sprite.spritecollide(self, a, False) return collide def collbrick(self, a): collide = pygame.sprite.spritecollide(self, a, False) return collide
import pygame, random class Brick(pygame.sprite.Sprite): def __init__(self, a, b): super(Brick, self).__init__() # random number for blue and green Brick randomnumber = random.randint(1, 2) if randomnumber == 1: self.image = pygame.image.load('images/greenbrick.png').convert() else: self.image = pygame.image.load('images/bluebrick.png').convert() self.rect = self.image.get_rect() self.rect.x = a self.rect.y = b self.hits = 3 def takehit(self, score): # the ball has collided with *this* brick self.hits -= 1 if self.hits == 0: self.kill() score += 1 return score