Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with game
#21
(Nov-14-2017, 09:23 PM)metulburr Wrote: if you remove the elif, to have all if, you can do two angles at the same time making it more "natural" feeling
(Nov-14-2017, 08:40 PM)Windspar Wrote: here a pygame example
import pygame
pygame.init()

SCREEN = 600, 400

class Bullet:
    def __init__(self, x, y, color, movement):
        self.position = [x - movement[0], y - movement[1]]
        self.color = color
        self.movement = movement

    def blit(self, surface):
        self.position[0] += self.movement[0]
        self.position[1] += self.movement[1]
        position = int(self.position[0]), int(self.position[1])
        pygame.draw.circle(surface, self.color, position, 3)

class Ship:
    def __init__(self, x, y, color, pointlist):
        self.position = [x, y]
        self.pointlist = pointlist
        self.color = color

    def move(self, x, y):
        self.position[0] += x
        self.position[1] += y

    def blit(self, surface):
        points = []
        for point in self.pointlist:
            points.append((self.position[0] + point[0], self.position[1]  + point[1]))

        pygame.draw.polygon(surface, self.color, points)

class Pauser:
    def __init__(self, length):
        self.length = length
        self.last_tick = 0

    def elaspe(self, ticks):
        if ticks > self.last_tick + self.length:
            self.last_tick = ticks
            return True
        return False

class App:
    def __init__(self):
        pygame.display.set_caption("Invaders")
        # create are screen. Your main surface
        self.screen = pygame.display.set_mode(SCREEN)
        # create a clock for framerate control
        self.clock = pygame.time.Clock()
        self.bullets = []
        self.bullets_pauser = Pauser(300)
        self.ship = Ship(300, 370, (0,0,200), ((0,0), (-20,30), (20,30)))

    def loop(self):
        running = True
        self.ticks = 0
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False

            last_ticks = self.ticks
            self.ticks = pygame.time.get_ticks()
            speed = (self.ticks - last_ticks) * 0.05
            # grab all keys return True if pressed
            key_press = pygame.key.get_pressed()
            # creaate bullets
            if key_press[pygame.K_SPACE]:
                # shoot bullets in intervals
                if self.bullets_pauser.elaspe(self.ticks):
                    x, y = self.ship.position
                    self.bullets.append(Bullet(x, y, (0,0,200), (0,-10)))

            # ship movement
            ship_speed = speed * 3
            if key_press[pygame.K_LEFT]:
                self.ship.move(-ship_speed, 0)
            if key_press[pygame.K_RIGHT]:
                self.ship.move(ship_speed, 0)
            if key_press[pygame.K_UP]:
                self.ship.move(0, -ship_speed)
            if key_press[pygame.K_DOWN]:
                self.ship.move(0, ship_speed)

            # clear screen
            self.screen.fill((0,0,0))
            # blits bullets
            remove_list = []
            for enum, bullet in enumerate(self.bullets):
                if bullet.position[1] < 0:
                    remove_list.append(enum)
                else:
                    bullet.blit(self.screen)

            for enum, index in enumerate(remove_list):
                self.bullets.pop(index - enum)

            self.ship.blit(self.screen)

            pygame.display.flip()
            # frame per seconds
            self.clock.tick(30)

def main():
    app = App()
    app.loop()
    pygame.quit()

if __name__ == '__main__':
    main()

Thank very much metulburr

But I'm looking for a code with the less libraries


Thanks for your concern
Reply


Messages In This Thread
help with game - by hammza - Oct-30-2017, 04:34 PM
RE: help with game - by heiner55 - Nov-11-2017, 03:24 PM
RE: help with game - by hammza - Nov-11-2017, 03:29 PM
RE: help with game - by heiner55 - Nov-11-2017, 05:25 PM
RE: help with game - by hammza - Nov-12-2017, 12:56 AM
RE: help with game - by heiner55 - Nov-13-2017, 06:13 AM
RE: help with game - by hammza - Nov-13-2017, 09:12 AM
RE: help with game - by metulburr - Nov-13-2017, 01:01 PM
RE: help with game - by heiner55 - Nov-13-2017, 01:46 PM
RE: help with game - by hammza - Nov-13-2017, 07:23 PM
RE: help with game - by Windspar - Nov-13-2017, 05:16 PM
RE: help with game - by metulburr - Nov-13-2017, 08:52 PM
RE: help with game - by Windspar - Nov-13-2017, 09:11 PM
RE: help with game - by heiner55 - Nov-14-2017, 05:56 AM
RE: help with game - by Windspar - Nov-14-2017, 02:06 PM
RE: help with game - by Windspar - Nov-14-2017, 08:40 PM
RE: help with game - by metulburr - Nov-14-2017, 09:23 PM
RE: help with game - by hammza - Dec-06-2017, 02:37 PM
RE: help with game - by Windspar - Nov-14-2017, 10:15 PM
RE: help with game - by Windspar - Nov-18-2017, 02:50 AM
RE: help with game - by hammza - Dec-06-2017, 01:13 PM
RE: help with game - by Windspar - Dec-06-2017, 02:47 PM
RE: help with game - by hammza - Dec-07-2017, 01:07 AM
RE: help with game - by Windspar - Dec-07-2017, 03:37 AM
RE: help with game - by hammza - Dec-07-2017, 06:00 AM
RE: help with game - by Windspar - Dec-07-2017, 03:48 PM
RE: help with game - by hammza - Dec-07-2017, 05:30 PM
RE: help with game - by Windspar - Dec-07-2017, 09:24 PM
RE: help with game - by hammza - Dec-08-2017, 02:10 AM
RE: help with game - by Windspar - Dec-08-2017, 02:44 AM
RE: help with game - by hammza - Dec-08-2017, 12:45 PM
RE: help with game - by Windspar - Dec-08-2017, 01:37 PM
RE: help with game - by hammza - Dec-08-2017, 03:09 PM
RE: help with game - by Windspar - Dec-08-2017, 03:17 PM
RE: help with game - by hammza - Dec-08-2017, 03:22 PM
RE: help with game - by Windspar - Dec-08-2017, 03:28 PM
RE: help with game - by hammza - Dec-08-2017, 03:30 PM
RE: help with game - by Windspar - Dec-08-2017, 03:39 PM
RE: help with game - by Windspar - Dec-08-2017, 11:17 PM
RE: help with game - by hammza - Dec-09-2017, 01:05 AM
RE: help with game - by Windspar - Dec-09-2017, 01:19 AM
RE: help with game - by hammza - Dec-09-2017, 01:25 AM

Forum Jump:

User Panel Messages

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