Hey, I am doing a 'Helicopter'-style game for a project where the player holds the spacebar for the main sprite to go up, releases to go down. Objects come from the right side of the screen at random y-cords moving until they hit 0 on the x-cord. The player must dodge these objects, while making sure to not hit the top or bottom of the screen. If the player collides with the top, bottom or any object, game is over.

So far the game is coming along OK, with the exception of the scoreboard not working right. BUT I want to implement a start menu that runs before the game starts that explains directions, and starts the game after some user-interaction (if user presses spacebar).
Also, I want to implement a 'Play again?' message if the player loses.

I am very new to pygame and livewires, as this is the 2nd or 3rd time really using them. Any ideas would be greatly appreciated.

from livewires import games, color
import random
import time

games.init(screen_width = 900, screen_height = 480, fps = 50)


class Pig(games.Sprite):
    """The Pig, controlled by the player"""
    image = games.load_image("pig2.bmp")


    def __init__(self):
        super(Pig, self).__init__(image = Pig.image,
                                  y = games.screen.height/2,
                                  x = 75)


    def update(self):
        """falls at a greater rate than flies, spacebar to fly"""
        if games.keyboard.is_pressed(games.K_SPACE):
            self.y -= 1.7
            self.angle = -20
        else:
            self.y += 2
            self.angle = 20

        if self.y > games.screen.height:
            self.end_game()
        if self.y < 1:
            self.end_game()

        self.check_hit()


    def check_hit(self):
        """checks to see if pig hits a fence"""
        for fence in self.overlapping_sprites:
            self.end_game()


    def end_game(self):
        end_message = games.Message(value = "GAME OVER",
                                    size = 100,
                                    color = color.black,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 5 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)



class Fence(games.Sprite):
    """a razor-wire fence"""
    image = games.load_image("razor.bmp")

    SCORE = 0
    SPEEDS = [-1, -2]
    
    def __init__(self, x, y):
        super(Fence, self).__init__(image = Fence.image,
                                    x = x,
                                    y = y,
                                    dx = random.choice(Fence.SPEEDS))

        self.score_display = games.Text(value = 0,
                                        size = 50,
                                        color = color.red,
                                        top = 5,
                                        right = games.screen.width - 10)
        games.screen.add(self.score_display)

    def update(self):
        if self.x < 0:
            self.die()
        if self.x == games.screen.width/2:
            y_cord = random.randrange(games.screen.height)
            new_fence = Fence(games.screen.width - 10, y_cord)
            games.screen.add(new_fence)

    def die(self):
        self.score_display.destroy()
        Fence.SCORE += 1
        self.score_display = games.Text(value = Fence.SCORE,
                                        size = 50,
                                        color = color.red,
                                        top = 5,
                                        right = games.screen.width - 10)
        games.screen.add(self.score_display)

        self.destroy()



def pause(seconds):
    go = False
    while go != True:
        time.sleep(seconds)
        go = True

def main():
    """main func"""
    pig = Pig()
    games.screen.add(pig)

    fence = Fence(games.screen.width - 10, games.screen.height/2)
    games.screen.add(fence)

    games.mouse.is_visible = False
    games.screen.event_grab = True

    wall_image = games.load_image("wall.jpg")
    games.screen.background = wall_image


    games.screen.mainloop()

main()

Thanks.

Recommended Answers

All 3 Replies

hey, i am doing a 'helicopter'-style game for a project where the player holds the spacebar for the main sprite to go up, releases to go down. Objects come from the right side of the screen at random y-cords moving until they hit 0 on the x-cord. The player must dodge these objects, while making sure to not hit the top or bottom of the screen. If the player collides with the top, bottom or any object, game is over.

So far the game is coming along ok, with the exception of the scoreboard not working right. But i want to implement a start menu that runs before the game starts that explains directions, and starts the game after some user-interaction (if user presses spacebar).
Also, i want to implement a 'play again?' message if the player loses.

I am very new to pygame and livewires, as this is the 2nd or 3rd time really using them. Any ideas would be greatly appreciated.

from livewires import games, color
import random
import time

games.init(screen_width = 900, screen_height = 480, fps = 50)


class pig(games.sprite):
    """the pig, controlled by the player"""
    image = games.load_image("pig2.bmp")


    def __init__(self):
        Super(pig, self).__init__(image = pig.image,
                                  y = games.screen.height/2,
                                  x = 75)


    def update(self):
        """falls at a greater rate than flies, spacebar to fly"""
        if games.keyboard.is_pressed(games.k_space):
            Self.y -= 1.7
            self.angle = -20
        else:
            Self.y += 2
            self.angle = 20

        if self.y > games.screen.height:
            Self.end_game()
        if self.y < 1:
            Self.end_game()

        self.check_hit()


    def check_hit(self):
        """checks to see if pig hits a fence"""
        for fence in self.overlapping_sprites:
            Self.end_game()


    def end_game(self):
        End_message = games.message(value = "game over",
                                    size = 100,
                                    color = color.black,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 5 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)



class fence(games.sprite):
    """a razor-wire fence"""
    image = games.load_image("razor.bmp")

    score = 0
    speeds = [-1, -2]
    
    def __init__(self, x, y):
        Super(fence, self).__init__(image = fence.image,
                                    x = x,
                                    y = y,
                                    dx = random.choice(fence.speeds))

        self.score_display = games.text(value = 0,
                                        size = 50,
                                        color = color.red,
                                        top = 5,
                                        right = games.screen.width - 10)
        games.screen.add(self.score_display)

    def update(self):
        If self.x < 0:
            Self.die()
        if self.x == games.screen.width/2:
            Y_cord = random.randrange(games.screen.height)
            new_fence = fence(games.screen.width - 10, y_cord)
            games.screen.add(new_fence)

    def die(self):
        Self.score_display.destroy()
        fence.score += 1
        self.score_display = games.text(value = fence.score,
                                        size = 50,
                                        color = color.red,
                                        top = 5,
                                        right = games.screen.width - 10)
        games.screen.add(self.score_display)

        self.destroy()



def pause(seconds):
    Go = false
    while go != true:
        Time.sleep(seconds)
        go = true

def main():
    """main func"""
    pig = pig()
    games.screen.add(pig)

    fence = fence(games.screen.width - 10, games.screen.height/2)
    games.screen.add(fence)

    games.mouse.is_visible = false
    games.screen.event_grab = true

    wall_image = games.load_image("wall.jpg")
    games.screen.background = wall_image


    games.screen.mainloop()

main()

thanks.

ignore the pause function---i was trying to use it earlier but just kind of left it there

If you want to do pause, do

import pygame
import sys, os

#Pause Variable
pause = False

#Main loop
while 1:
    for event in pygame.event.get():
        if event.type==KEYUP:
            if event.key==K_p:
                pause = False

    while pause == False:
        for event in pygame.event.get():
            if event.type==KEYUP:
                if event.key==K_p:
                    pause = True
            #Do your game here

And for converting, I used py2exe awhile ago, but can't quide remember how to use it...
Hope this helps: http://www.py2exe.org/index.cgi/Tutorial. If not, sorry :(

Oops, sorry, Wrong thread. Good luck, though! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.