Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 13:44

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Python Pygame Object randomly falling code

Discussion on Python Pygame Object randomly falling code within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
.UnlimiTeD's Avatar
 
elite*gold: 6
Join Date: Mar 2014
Posts: 597
Received Thanks: 24
Exclamation Python Pygame Object randomly falling code

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()

.UnlimiTeD is offline  
Old 01/28/2021, 14:54   #2
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 166
Quote:
Originally Posted by .UnlimiTeD View Post
Habe folgendes Problem: Möchte, dass mein Player die ***** (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!

Please use [ code ] tag to show your code, this is unreadable, it also has some weird unused code. If you don't call your generator function inside your loop it won't show anything
elmarcia is offline  
Reply


Similar Threads Similar Threads
[Selling] OBJECT 907, T-22 MEDIUM, OBJECT 260, 121B, OBJECT 279 EARLY, T95/FV4201 CHIEFT
12/16/2020 - World of Tanks Trading - 0 Replies
EU SERVER WN8 - 3046; WN7 - 1905; Rate of wins - 62% AMX 50 B, E100, IS-7, S. Conqueror, T110E5, T57 Heavy, IS-4, Kranvagn, 113, WZ-111 5A, Type 5 Heavy, Obj. 277, Bat.-Châtillon 25 t, E 50 Ausf. M, Leopard 1, M48A5 Patton, Object 140, T-62A, Jagdpanzer E 100, TVP T 50/51, Centurion Action X, Grille 15, STB-1, AMX 30 B, T57 Heavy Tank, FV4005 Stage II, Object 261, XM551 Sheridan, Object 430U, T-100 LT, Object 268 Version 4, AMX 13 105, WZ-111 model 5A, WZ-132-1, Type 5 Heavy,...
[Selling] FV215b, M60, Object 907, T-22 medium, Object 260, 121B, Object 279 early, T95/FV4201
11/15/2020 - World of Tanks Trading - 0 Replies
EU ACCOUNT 45000 battles, 61% wins In hangar: 30 tiers 10 level and special tiers: FV215b, M60, Object 907, T-22 medium, Object 260, 121B, Object 279 early, T95/FV4201 Chieftain 25 premium tiers 8 lvl Write to: WhatsApp: +79050356196 Instagram: mamiev10z email:[email protected] Facebook: https://www.facebook.com/profile.php?id=1000014433 23712
[Selling] Object 260 + Object 907 + 26 tier X + 3 tier IX + 18 premium + 56 in garage [430 EUR]
04/14/2017 - World of Tanks Trading - 2 Replies
I'm selling this World of Tanks WoT account on EU server + Clean dedicated email included. Server: EU Battles: 37 800 Winrate: 54.53% WN8: 1 910 Gold: 1 180 Credits: 9 000 000 Free experience: 8 400
[Selling] Great account with Object 260 + Object 907 + 14 Tier X + 20000 Gold
04/11/2016 - World of Tanks Trading - 3 Replies
I'm selling this World of Tanks WoT account on EU server + Clean dedicated email included. No telephone added to the account, so the account is ready to be sold. Battles: 32 000 Winrate: 53.70% (last 1000 battles: 56%) WN8: 1700 (last 1000 battles: 2800) Server: EU Gold: 18 000 Credits: 2 900 000 Free experience: 0



All times are GMT +2. The time now is 13:44.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.