First Pygame (i guess you could call it a game)

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

First Pygame (i guess you could call it a game)

Postby metulburr » Fri May 10, 2013 3:52 am

Woohoo, first workable pygame "thing". Its stolen art, the source is has a lot of work to do, Mekire had to pull me most of the way, and a lot of it is Mekire's example code transfered over, but its a start.

Code: Select all
import pygame
import sys
import math
import random

def image_from_url(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read())

class Projectile:
    limit = 3
    def __init__(self, loc, color):
        self.surf = pygame.Surface((5,10)).convert()
        self.surf.set_colorkey((255,0,255))
        self.surf.fill(color)
        self.rect = self.surf.get_rect()
        self.rect.center = loc
        self.mask = pygame.mask.from_surface(self.surf)
        self.speed = 8

class Enemy:
    count = 3
    def __init__(self, start_loc, orig_image):
        self.score_value = 100
        self.orig_image = orig_image
        #link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship_enemy3_zpscfeb13bf.png'
        #self.orig_image = pygame.image.load(image_from_url(link)).convert()
        self.orig_image = pygame.transform.scale(self.orig_image, (40,80))
        self.orig_image.set_colorkey((255,0,255))
        self.image = pygame.transform.rotate(self.orig_image, 180)
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.center = start_loc
        self.bullets = []
        self.speed = 2
        self.is_hit = False
        self.range_to_fire = False
        self.timestamp = pygame.time.get_ticks()
        self.distance_above_player = 200 #enemy to go above player to shootat player
        self.bullet_color = (255,0,0)
       
    def pos_towards_player(self, player_rect):
        '''get new coords towards player'''
       
        c = math.sqrt((player_rect.x - self.rect[0]) ** 2 + (player_rect.y - self.distance_above_player  - self.rect[1]) ** 2)
        try:
            x = (player_rect.x - self.rect[0]) / c
            y = ((player_rect.y - self.distance_above_player)  - self.rect[1]) / c
        except ZeroDivisionError:
            return False
        return (x,y)
       
    def update(self, player):
        if player.active:
            if not self.is_hit:
                #move enemy towards player
                new_pos = self.pos_towards_player(player.rect)
                if new_pos: #if not ZeroDivisonError
                    self.rect.x, self.rect.y = (self.rect.x + new_pos[0] * self.speed, self.rect.y + new_pos[1] * self.speed)
       
   
        self.fire_update(player)
        if self.range_to_fire: 
            diff = pygame.time.get_ticks() - self.timestamp
            if diff > 1500.0:
                self.timestamp = pygame.time.get_ticks()
                if player.active: #if player is not dead
                    self.bullets.append(Projectile(self.rect.center, self.bullet_color))
       
    def fire_update(self, player):
        #if player is lower than enemy
        if player.rect.y >= self.rect.y:
            try:
                offset_x =  self.rect.x - player.rect.x
                offset_y =  self.rect.y - player.rect.y
                d = int(math.degrees(math.atan(offset_x / offset_y)))
            except ZeroDivisionError:
                #player is above enemy
                return
            #if player is within 15 degrees lower of enemy
            if math.fabs(d) <= 15:
                self.range_to_fire = True
            else:
                self.range_to_fire = False
                #self.bullets.append(Projectile(self.rect.center))
           


class Player:
    def __init__(self, start_loc):
        self.score = 0
        self.damage = 10
        self.active = True #check for end of game
        self.bullet_color = (255,255,255)
       
        self.speed = 4

        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship2_zps095c332a.png'
        self.orig_image = pygame.image.load(image_from_url(link)).convert()
        self.orig_image = pygame.transform.scale(self.orig_image, (40,80))
        self.orig_image.set_colorkey((255,0,255))

        self.image = pygame.transform.rotate(self.orig_image, 180)
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.center = start_loc

        self.bullets = []

    def move(self, x, y):
        if self.active:
            self.rect[0] += x * self.speed
            self.rect[1] += y * self.speed
           
    def update(self):
        if self.damage < 0:
            self.damage = 0
            self.active = False


class Control:
    def __init__(self):
        pygame.init()
        self.screensize = (800,600)
        self.screen = pygame.display.set_mode(self.screensize)
        self.screenrect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.player = Player((0,600))
        #self.enemy = Enemy((800,0))
        self.score_loss_per_shot = 25
        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship_enemy3_zpscfeb13bf.png'
        self.enemy_image = pygame.image.load(image_from_url(link)).convert()
        self.enemies = []

        self.mainloop()
       
    def write(self, displaytext, color=(0,0,0), size=15, ul=False, bold=False,
            ital=False, font='timesnewroman'):
        font = pygame.font.SysFont(font, size)
        font.set_underline(ul)
        font.set_bold(bold)
        font.set_italic(ital)
        label = font.render(displaytext, 1, color)
        label_rect = label.get_rect()
        return label,label_rect

    def mainloop(self):
        run = True
        while run:
            self.screen.fill((0,0,0))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if len(self.player.bullets) <= Projectile.limit:
                            self.player.score -= self.score_loss_per_shot
                            self.player.bullets.append(Projectile(self.player.rect.center, self.player.bullet_color))

            self.update()
            pygame.display.flip()
            self.clock.tick(60)

    def update(self):
        self.keys = pygame.key.get_pressed()
       
        self.player.update()
        self.player.rect.clamp_ip(self.screenrect) #make sure rect does not go outside of screen
       
        if len(self.enemies) <= Enemy.count:
            self.enemies.append(Enemy((random.randint(1,800),-100), self.enemy_image))

        if self.player.bullets:
            for obj in self.player.bullets[:]:
                self.screen.blit(obj.surf, obj.rect)
                obj.rect[1] -= obj.speed

                #did obj move off screen
                if obj.rect[1] < 0:
                    self.player.bullets.remove(obj)
               
                for enem in self.enemies[:]:
                    #did obj hit enemy rect
                    if obj.rect.colliderect(enem.rect):
                        offset_x =  enem.rect.x - obj.rect.x
                        offset_y =  enem.rect.y - obj.rect.y
                        #did object hit enemy mask
                        if obj.mask.overlap(enem.mask, (offset_x, offset_y)):
                            self.player.bullets.remove(obj)
                            self.enemies.remove(enem)
                            enem.is_hit = True
                            self.player.score += enem.score_value
                            break
                        #break
                       
       
        for enem in self.enemies:
            if enem.bullets:
                for obj in enem.bullets[:]:
                    self.screen.blit(obj.surf, obj.rect)
                    obj.rect[1] += obj.speed
                    offset_x =  obj.rect.x - self.player.rect.x
                    offset_y =  obj.rect.y - self.player.rect.y
                    #did object hit player
                    if self.player.mask.overlap(obj.mask, (offset_x, offset_y)):
                        self.player.damage -= 1
                        enem.bullets.remove(obj)
                        break

   
        if self.keys[pygame.K_UP]:
            self.player.move(0,-1)
        if self.keys[pygame.K_DOWN]:
            self.player.move(0,1)
        if self.keys[pygame.K_RIGHT]:
            self.player.move(1,0)
        if self.keys[pygame.K_LEFT]:
            self.player.move(-1,0)

       
       
        for obj in self.enemies:
            obj.update(self.player)
            if not obj.is_hit:
                self.screen.blit(obj.image, obj.rect)
           
            #if player collides with enemy
            offset_x =  obj.rect.x - self.player.rect.x
            offset_y =  obj.rect.y - self.player.rect.y
            if self.player.mask.overlap(obj.mask, (offset_x, offset_y)):
                obj.is_hit = True
                self.player.damage -= 1
                self.player.score -= obj.score_value
                self.enemies.remove(obj)
                break


        self.screen.blit(self.player.image, self.player.rect)
        scoreboard = self.write('Score: {}'.format(self.player.score), color=(255,255,255), size=30)
        self.screen.blit(scoreboard[0], (10,10))

        if self.player.active:
            lifeboard = self.write('Damage: {}'.format(self.player.damage), color=(255,255,255), size=30)
            self.screen.blit(lifeboard[0], (self.screensize[0] - 150, 10))
        else:
            game_over = self.write('GAME OVER', color=(255,255,255), size=50)
            game_over[1].center = (self.screensize[0] // 2, self.screensize[1] // 2)
            self.screen.blit(game_over[0], game_over[1])
       

app = Control()
pygame.quit()
sys.exit()


now i just mainly have to just split up Contorl.update() into where the code should be


https://github.com/metulburr/fighter
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: First Pygame (i guess you could call it a game)

Postby Mekire » Fri May 10, 2013 6:14 am

Let me know if you want me to throw up a page on pygame.org for this or any projects.
Just let me know what you want the page to include.
I'll explicitly state at the top that it is your project and not mine. :lol:

-Mek

Edit: Not a bad start. I didn't realize that you had implemented the life, score, and attacking enemies.

Edit2: the math module has this great function hypot:
Code: Select all
>>> math.sqrt(5*5+7*7)
8.602325267042627
>>> math.hypot(5,7)
8.602325267042627
User avatar
Mekire
 
Posts: 1132
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: First Pygame (i guess you could call it a game)

Postby metulburr » Fri May 10, 2013 12:05 pm

Let me know if you want me to throw up a page on pygame.org for this or any projects

That would be awesome! I plan on more extravagant and in-depth pygame games later for sure, especially as the pygame becomes more second nature.

the math module has this great function hypot:

:o thats funny. You can tell how little i used the math module beforehand
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: First Pygame (i guess you could call it a game)

Postby Hissing Cockroach » Tue Sep 24, 2013 9:50 pm

Which part of the code does the graphics?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: First Pygame (i guess you could call it a game)

Postby metulburr » Wed Sep 25, 2013 12:10 am

Which part of the code does the graphics?


if you mean loading the graphics:
the images are on a universal pic site, photobucket. It made it easy for run and go. It makes it nice, especially when asking question here regarding pygame code that needs those resources to work.

in Control() there is:
Code: Select all
        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship_enemy3_zpscfeb13bf.png'
        self.enemy_image = pygame.image.load(image_from_url(link)).convert()

which loads the image form the url, and loads the image with pygame, onto enemy_image
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: First Pygame (i guess you could call it a game)

Postby Crimson King » Wed Sep 25, 2013 4:24 am

Hey metulburr great start! It's nice seeing a non-Pong first game :D

I tried it a couple of times and noticed that whenever the enemy's shot and your own collide close to the enemy their shot disappears.

This reminds me i should stop wasting time and start learning some pygame myself.
User avatar
Crimson King
 
Posts: 132
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: First Pygame (i guess you could call it a game)

Postby metulburr » Wed Sep 25, 2013 3:25 pm

Yeah its a bug, that i havent gotten to. I actually havent messed with this one in awhile. I already have an idea on how to fix it, but this was pretty much a test to get something up and running.

This also reminds me to get back into pygame, as i keep veering off course with something else.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 4 guests