OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pygame, object balloon won't move

  • Thread starter Thread starter GhostaNero
  • Start date Start date
G

GhostaNero

Guest
I've written a program following a PDF which my teacher sent us to do over the summer (it is not a homework). However as I follow this through I encountered a problem where the balloons won't move. ( According to the PDF it should).

My code is below:

Code:
import pygame
import random


pygame.init() 

class Balloon(pygame.sprite.Sprite):
    
    def __init__(self,x,y,direction,balloonType):
        
        pygame.sprite.Sprite.__init__(self)
        
        self.Direction = direction
        self.BalloonType = balloonType

        if balloonType == 1:
            balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\RedBalloon.png")
            self.Speed = 3
            self.Score = 5
        if balloonType == 2:
            balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\YellowBalloon.png")
            self.Speed = 7
            self.Score = 15
        if balloonType == 3:
            balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\GreenBalloon.png")
            self.Speed = 5
            self.Score = 10
        if balloonType == 4:
            balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\BlueBalloon.png")
            self.Speed = 10
            self.Score = 0

        self.image = pygame.Surface([26,50])
        self.image.set_colorkey(black)
        self.image.blit(balloonImage, (0,0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        
        
    def move(self):
        

        if self.Direction == "Right":
            self.rect.x = self.rect.x + self.Speed
        if self.Direction == "left":
            self.rect.x = self.rect.x - self.Speed
        
        
screen = pygame.display.set_mode([800,400])
pygame.display.set_caption("Ballon game")
bg = pygame.image.load("C:\Coding Portfolio\Blue_Sky_with_Clouds_Wallpaper_(5080737587).jpg").convert()
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
running =  True


otherBalloons = pygame.sprite.Group()
blueBalloons = pygame.sprite.Group()
allBalloons = pygame.sprite.Group()

timeTillNextBalloon = random.randint(1000,2000)


while running == True:
    
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if pygame.time.get_ticks() > timeTillNextBalloon:
        timeTillNextBalloon += random.randint(300,2500)
        yCoord = random.randint(50,350)
        balloonType = random.randint(1,4)
        balloon = Balloon(0,yCoord,"right",balloonType)
        if balloonType >= 1 and balloonType <=3:
            otherBalloons.add(balloon)
        else:
            blueBalloons.add(balloon)
        allBalloons.add(balloon)
    for balloon in(allBalloons.sprites()):
        if balloon.rect.x < 0:
            balloon.Direction = "right"
        if balloon.rect.x > 774:
            balloon.Direction = "left"
            
    for balloon in (allBalloons.sprites()):
        balloon.move()
        
    screen.blit(bg, [0,0])
    allBalloons.draw(screen)
    pygame.display.update
    pygame.display.flip()
    clock.tick(20)
    
pygame.quit()

I am also quite new to object orientation as a whole so I myself do not understand half of the things that is in this program, and an extra favor if anyone can recommend me object orientation tutorials/courses then I would really really appreciate it.
<p>I've written a program following a PDF which my teacher sent us to do over the summer (it is not a homework). However as I follow this through I encountered a problem where the balloons won't move. ( According to the PDF it should).</p>
<p>My code is below:</p>
<pre><code>import pygame
import random


pygame.init()

class Balloon(pygame.sprite.Sprite):

def __init__(self,x,y,direction,balloonType):

pygame.sprite.Sprite.__init__(self)

self.Direction = direction
self.BalloonType = balloonType

if balloonType == 1:
balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\RedBalloon.png")
self.Speed = 3
self.Score = 5
if balloonType == 2:
balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\YellowBalloon.png")
self.Speed = 7
self.Score = 15
if balloonType == 3:
balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\GreenBalloon.png")
self.Speed = 5
self.Score = 10
if balloonType == 4:
balloonImage = pygame.image.load("C:\Coding Portfolio\Python\schoolWork\BlueBalloon.png")
self.Speed = 10
self.Score = 0

self.image = pygame.Surface([26,50])
self.image.set_colorkey(black)
self.image.blit(balloonImage, (0,0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y


def move(self):


if self.Direction == "Right":
self.rect.x = self.rect.x + self.Speed
if self.Direction == "left":
self.rect.x = self.rect.x - self.Speed


screen = pygame.display.set_mode([800,400])
pygame.display.set_caption("Ballon game")
bg = pygame.image.load("C:\Coding Portfolio\Blue_Sky_with_Clouds_Wallpaper_(5080737587).jpg").convert()
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
running = True


otherBalloons = pygame.sprite.Group()
blueBalloons = pygame.sprite.Group()
allBalloons = pygame.sprite.Group()

timeTillNextBalloon = random.randint(1000,2000)


while running == True:


for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if pygame.time.get_ticks() > timeTillNextBalloon:
timeTillNextBalloon += random.randint(300,2500)
yCoord = random.randint(50,350)
balloonType = random.randint(1,4)
balloon = Balloon(0,yCoord,"right",balloonType)
if balloonType >= 1 and balloonType <=3:
otherBalloons.add(balloon)
else:
blueBalloons.add(balloon)
allBalloons.add(balloon)
for balloon in(allBalloons.sprites()):
if balloon.rect.x < 0:
balloon.Direction = "right"
if balloon.rect.x > 774:
balloon.Direction = "left"

for balloon in (allBalloons.sprites()):
balloon.move()

screen.blit(bg, [0,0])
allBalloons.draw(screen)
pygame.display.update
pygame.display.flip()
clock.tick(20)

pygame.quit()

</code></pre>
<p>I am also quite new to object orientation as a whole so I myself do not understand half of the things that is in this program, and an extra favor if anyone can recommend me object orientation tutorials/courses then I would really really appreciate it.</p>
 
Top