Habe folgendes Problem: Möchte, dass mein Player die Balls (in dem Falle Münzen) auffängt, die random (zufällig runterfallen). Der Code zeigt keinerlei Fehlermeldung, allerdings erscheinen auch keine Münzen. Was mache ich falsch? (Eingerückt ist alles, allerdings zerhaut es die Formatierung hier). Bin für jede Hilfe dankbar!
Anbei mein Code:
import pygame
import random
from pygame.sprite import Sprite
pygame.init()
#Display
win = pygame.display.set_mode((900,780))
pygame.display.set_caption("The Collector")
#Laufanimation
walkRight = [pygame.image.load('Assets/R1.png'), pygame.image.load('Assets/R2.png'), pygame.image.load('Assets/R3.png'), pygame.image.load('Assets/R4.png'), pygame.image.load('Assets/R5.png'), pygame.image.load('Assets/R6.png'), pygame.image.load('Assets/R7.png'), pygame.image.load('Assets/R8.png'), pygame.image.load('Assets/R9.png')]
walkLeft = [pygame.image.load('Assets/L1.png'), pygame.image.load('Assets/L2.png'), pygame.image.load('Assets/L3.png'), pygame.image.load('Assets/L4.png'), pygame.image.load('Assets/L5.png'), pygame.image.load('Assets/L6.png'), pygame.image.load('Assets/L7.png'), pygame.image.load('Assets/L8.png'), pygame.image.load('Assets/L9.png')]
#Hintergrund
bg = pygame.image.load('City.jpg')
bg = pygame.transform.scale(bg, (900,780))
#Charakter
char = pygame.image.load('Assets/R1.png')
#Geldschein
geld = pygame.image.load('Assets/Schein.png')
YELLOW = (255,255,000)
clock = pygame.time.Clock()
class player():
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 20
self.left = False
self.right = False
self.walkCount = 0
self.hitbox = (self.x + 20, self.y, 28, 60)
def draw(self, win):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount +=1
else:
win.blit(char, (self.x,self.y))
self.hitbox = (self.x + 215, self.y + 230, 220, 70) # NEW
pygame.draw.rect(win, (255,0,0), self.hitbox,2)
class Balls (Sprite):
def __init__(self, bg_settings, screen):
super(Balls, self).__init__()
self.screen = screen
self.bg_settings = bg_settings
# Load the ball image and set its rect attribute.
self.image = pygame.image.load('Assets/coin.png')
self.rect = self.image.get_rect()
# Start each new ball.
self.rect.x = random.randint(-10, self.bg_settings.screen_width)
self.rect.y = random.randint(-100, -40)
# Store the ball's exact position.
self.y = float(self.rect.y)
self.is_falling = False #Ball is not falling when first created
def update(self):
"""Move the ball down."""
if self.is_falling:
self.y += self.bg_settings.ball_speed_factor
self.rect.y = self.y
def blitme(self):
"""Draw the ball at its current location."""
self.screen.blit(self.image, self.rect)
def create_ball(bg_settings, screen, balls, ball_number):
"""Create a ball and place it in the row."""
ball = Ball(bg_settings, screen)
ball_width = ball.rect.width
ball.x = ball_width + 2 * ball_width * ball_number
ball.rect.x = ball.x
balls.add(ball)
def create_fleet(bg_settings, screen, boy, balls):
ball = Ball(bg_settings, screen)
number_balls_x = get_number_balls_x(bg_settings, ball.rect.width)
for ball_number in range(number_balls_x):
create_ball(bg_settings, screen, balls, ball_number)
def update_balls(balls):
"""Update the positions of all balls in the fleet."""
balls.update()
# Make the most recently drawn screen visible.
pygame.display.flip()
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
bg_settings = Settings()
screen = pygame.display.set_mode(
(bg_settings.screen_width, bg_settings.screen_height))
pygame.display.set_caption("Catch the Baseball!")
balls = Group()
# Create the fleet of aliens.
gf.create_fleet(bg_settings, screen, boy, balls)
# Make a ball
ball = Ball(bg_settings, screen)
# Start the main loop for the game.
while True:
gf.check_events(boy)
boy.update()
gf.update_balls(balls)
gf.update_screen(bg_settings, screen, boy, balls)
def update_balls(self):
if random() < 0.6: # 60% percent chance of making a ball drop
ball_index = randint(0, len(balls)-1)
balls[ball_index].is_falling = True
balls.update()
def draw(self,win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
def redrawGameWindow():
win.blit(bg, (0,0))
collector.draw(win)
pygame.display.update()
#mainloop
collector = player(200, 410, 64,64)
#geld = münzen(200, 15, 15, YELLOW , 50)
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and collector.x > -180 -collector.width -collector.vel:
collector.x -= collector.vel
collector.left = True
collector.right = False
elif keys[pygame.K_RIGHT] and collector.x < 550 - collector.width - collector.vel:
collector.x += collector.vel
collector.right = True
collector.left = False
else:
collector.right = False
collector.left = False
collector.walkCount = 0
redrawGameWindow()
pygame.quit()