ich habe mit Hilfe aus dem Forum eine Maussteuerung für ein Arkanoid/Breakout-Spiel erstellt. Habe dazu eine Vorlage, die per Tastatur gesteuert wird, angepasst.
Jetzt erhalte ich einen Fehler, mit dem ich nichts anfangen kann. Unter anderem wird da von Zeile 134 gesprochen, allerdings verfügt mein main nur über 120 Zeilen...
Ich werde die Programmteile und die Fehlermeldung hier veröffentlichen, mit der Bitte um Hilfe - also was ich tun kann bzw. falsch mache...
Ich verwende Pycharm als Editor...
Hier die Fehlermeldung:
C:\Users\...\AppData\Local\Microsoft\WindowsApps\python3.10.exe "D:/Daten_Tablet/aufbewahren/Tablet/Programmierung/Python/__weiter breakout/main.py"
pygame 2.1.2 (SDL 2.0.18, Python 3.10.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\Daten_Tablet\aufbewahren\Tablet\Programmierung\Python\__weiter breakout\main.py", line 119, in <module>
main()
File "D:\Daten_Tablet\aufbewahren\Tablet\Programmierung\Python\__weiter breakout\main.py", line 27, in main
ball = Ball(WHITE, 10, 10)
File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pygame\sprite.py", line 116, in __init__
self.add(*groups)
File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pygame\sprite.py", line 134, in add
self.add(*group)
File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pygame\sprite.py", line 134, in add
self.add(*group)
TypeError: pygame.sprite.Sprite.add() argument after * must be an iterable, not int
Process finished with exit code 1
hier main:
Code: Alles auswählen
# !/usr/bin/env python3
import pygame
from paddle import Paddle
from ball import Ball
from brick import Brick
pygame.init()
WHITE = (255, 255, 255)
DARKBLUE = (36, 90, 190)
LIGHTBLUE = (0, 176, 240)
RED = (255, 0, 0)
ORANGE = (255, 100, 0)
YELLOW = (255, 255, 0)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("cooles Breakout")
def main():
try:
score = 0
lives = 5
pygame.mouse.set_visible(0)
all_sprites = pygame.sprite.Group()
all_bricks = pygame.sprite.Group()
paddle = Paddle(screen.get_rect())
all_sprites.add(paddle)
ball = Ball(WHITE, 10, 10)
all_sprites.add(ball)
ball.rect.x = 345
ball.rect.y = 195
for i in range(7):
brick = Brick(RED, 80, 30)
brick.rect.x = 60 + i * 100
brick.rect.y = 60
all_sprites.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(ORANGE, 80, 30)
brick.rect.x = 60 + i * 100
brick.rect.y = 100
all_sprites.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(YELLOW, 80, 30)
brick.rect.x = 60 + i * 100
brick.rect.y = 140
all_sprites.add(brick)
all_bricks.add(brick)
# 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()
# Check if the ball is bouncing against the 4 walls
if ball.rect.x >= 790:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x <=0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y > 590:
ball.velocity[1] = -ball.velocity[1]
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
if ball.rect.y < 40:
ball.velocity[1] = -ball.velocity[1]
# Detect collisions between the ball and the paddle
if pygame.sprite.collide_mask(ball, 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.update(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
BLACK = (0, 0, 0)
class Ball(pygame.sprite.Sprite):
def __int__(self, color, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
pygame.draw.rect(self.image, color, [0, 0, width, height])
self.velocity = [randint(4, 8), randint(-8, 8)]
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8,8)
Code: Alles auswählen
import pygame
BLACK = (0,0,0)
class Brick(pygame.sprite.Sprite):
# This class represents a brick. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the brick, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# Draw the brick (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
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):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((80, 15))
self.image.fill(BLACK)
self.playfield_rect = playfield_rect
self.rect = self.image.get_rect()
self.rect.bottom = self.playfield_rect.bottom
def update(self, position):
x, _ = position
self.rect.centerx = x
self.rect = self.rect.clamp(self.playfield_rect)
Grüße,
kwon