Python Forum
Noob needs help with easy fix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob needs help with easy fix
#1
My code is not working, any fixes?

import pygame
from pygame.locals import *
import sys
import random
from os import path
snd_dir = path.join(path.dirname(__file__), 'assets')


class TripBird:
    def __init__(self):
        self.screen = pygame.display.set_mode((400, 708))
        self.bird = pygame.Rect(65, 50, 50, 50)
        self.background = pygame.image.load("assets/background.png").convert()
        self.birdSprites = [pygame.image.load("assets/plane.png").convert_alpha(),
                            pygame.image.load("assets/plane.png").convert_alpha(),
                            pygame.image.load("assets/dead.png")]
        self.wallUp = pygame.image.load("assets/bottomobstacle.png").convert_alpha()
        self.wallDown = pygame.image.load("assets/topobstacle.png").convert_alpha()
        self.gap = 130
        self.wallx = 400
        self.birdY = 350
        self.jump = 0
        self.jumpSpeed = 10
        self.gravity = 5
        self.dead = False
        self.sprite = 0
        self.counter = 0
        self.offset = random.randint(-110, 110)
    def run(self):
        clock = pygame.time.Clock()
        pygame.font.init()
        pygame.mixer.init()
        pygame.display.set_caption("Trip Bird")
        font = pygame.font.SysFont("Arial", 45)
        game_over = True
        while True:
            clock.tick(60)
            for event in pygame.event.get():
                jump_sound = pygame.mixer.Sound(path.join(snd_dir, 'jumpsound.wav'))
                if event.type == pygame.QUIT:
                    sys.exit()
                if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not self.dead:
                    self.jump = 17
                    self.gravity = 5
                    self.jumpSpeed = 10
                    jump_sound.play()
                if self.birdY == 350:
                    pygame.mixer.music.load(path.join(snd_dir,'music.ogg'))
                    pygame.mixer.music.set_volume(0.4)
                    pygame.mixer.music.play(loops=-1)
                if game_over:
                    show_go_screen():
                    draw_text(screen, "Rip", 64, Width / )
            self.screen.fill((255, 255, 255))
            self.screen.blit(self.background, (0, 0))
            self.screen.blit(self.wallUp,
                             (self.wallx, 360 + self.gap - self.offset))
            self.screen.blit(self.wallDown,
                             (self.wallx, 0 - self.gap - self.offset))
            self.screen.blit(font.render(str(self.counter),
                                         -1,
                                         (255, 255, 255)),
                             (200, 50))
            if self.dead:
                self.sprite = 2
            elif self.jump:
                self.sprite = 1
            self.screen.blit(self.birdSprites[self.sprite], (70, self.birdY))
            if not self.dead:
                self.sprite = 0
            self.updateWalls()
            self.birdUpdate()
            pygame.display.update()

    def updateWalls(self):
        self.wallx -= 4
        if self.wallx < -80: #Wall refreshes when it reaches -80 on x
            self.wallx = 400
            self.counter += 1 #Counter onlys goes up when the walls update. It doesn't update after you pass the pipe.
            self.offset = random.randint(-150, 110)

    def birdUpdate(self):

        if self.jump:
            self.jumpSpeed -= 1
            self.birdY -= self.jumpSpeed
            self.jump -= 1
            welcome()
        else:
            self.birdY += self.gravity
            self.gravity += 0.2
        self.bird[1] = self.birdY
        upRect = pygame.Rect(self.wallx,
                             360 + self.gap - self.offset + 10,
                             self.wallUp.get_width() - 10,
                             self.wallUp.get_height())
        downRect = pygame.Rect(self.wallx,
                               0 - self.gap - self.offset - 10,
                               self.wallDown.get_width() - 10,
                               self.wallDown.get_height())
        if upRect.colliderect(self.bird):
            self.dead = True
            self.gravity = 0
            self.wallx += 4
        if downRect.colliderect(self.bird):
            self.dead = True
            self.gravity = 0
            self.wallx += 4
        if not 0 < self.bird[1] < 720:
            self.bird[1] = 50
            self.birdY = 50
            self.dead = False
            self.counter = 0
            self.wallx = 400
            self.offset = random.randint(-110, 110)
            self.gravity = 5
if __name__ == "__main__":
    TripBird().run()
show_go_screen():
Gives me a syntax error saying invalid syntax, but don't know how to fix it.
Reply
#2
This would have been an easy fix, if you had posted the full text of your error. That would have given the exact line number (52) of the error, and the exact location (the colon). I think you want to delete the colon. However, you might have meant that as an if statement. In that case, you would want to include the actual 'if', and indent the next line of code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020