Example #1
0
class Car(Game_Object):
    def __init__(self, coords=(0,0), speed=(0,0)):
        Game_Object.__init__(self, coords=coords, speed=speed)
        self.image = load_image('yellow_car.png', path='Images', alpha_cannel=True)
        self.image = pygame.transform.rotate(self.image, -90)
        self.image = pygame.transform.scale(self.image, (50,50))
        self.rect = self.image.get_rect()
        self.track = None

    def move(self, dt):
        if self.track:
            temp_coords = self.coords
            Game_Object.move(self, dt)
            self.rect.center = self.coords.as_point()
            if self.rect.collidelist(self.track) != -1:
                #self.acsel = self.acsel*-1
                self.speed = Vector((0,0))
        else:
            Game_Object.move(self, dt)


    def add_track(self, track):
        self.track = track

    def render(self, screen):
        angle_of_rotate = math.degrees(math.acos(self.speed.normalize().x))
        #print(angle_of_rotate)

        if self.speed.y>0:
            angle_of_rotate = 360-angle_of_rotate

        rotated_img = pygame.transform.rotate(self.image, angle_of_rotate)
        rect_img = rotated_img.get_rect()
        rect_img.center = self.coords.as_point()  # центр картинки приравниваем вектору позиции
        screen.blit(rotated_img, rect_img) # блитуем все на скрин
        Game_Object.render(self, screen)
Example #2
0
class Ship:
    def __init__(self, pos):
        # self.w = 45
        # self.h = 40
        self.rotate_speed = 5
        self.state = NORMAL
        self.pos = Vector(pos)
        self.image = pygame.Surface((45, 40), pygame.SRCALPHA)
        self.speed = Vector((0, 0))
        self.normal_speed = Vector((1, 0))  # Скорость и направление, сохраняемые при полном торможении
        self.max_speed = 10

    def events(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.state = SPEED_UP
            if event.key == pygame.K_DOWN:
                self.state = SPEED_DOWN
            if event.key == pygame.K_LEFT:
                self.state = TURN_LEFT
            if event.key == pygame.K_RIGHT:
                self.state = TURN_RIGHT
        if event.type == pygame.KEYUP:
            self.state = NORMAL

    def update(self):
        if self.state == TURN_LEFT:
            self.speed.rotate(-self.rotate_speed)
            pygame.transform.rotate(self.image, self.speed.angle)
        if self.state == TURN_RIGHT:
            self.speed.rotate(self.rotate_speed)
        if self.state == SPEED_UP:
            if self.speed.len() < self.max_speed:
                self.speed = self.speed + self.speed.normalize()
                if self.speed.len() == 0:
                    self.speed = self.normal_speed
        if self.speed.len() == 0:
            return
        if self.state == SPEED_DOWN:
            if self.speed.len() < 1:
                self.normal_speed = self.speed
                self.speed = Vector((0, 0))
            else:
                self.speed = self.speed - self.speed.normalize()
        self.pos += self.speed
        if self.pos.as_point()[0] > 800:
            self.pos = Vector((0, self.pos.as_point()[1]))
        if self.pos.as_point()[1] > 600:
            self.pos = Vector((self.pos.as_point()[0], 0))
        if self.pos.as_point()[1] < -1:
            self.pos = Vector((self.pos.as_point()[0], 599))
        if self.pos.as_point()[0] < -1:
            self.pos = Vector((799, self.pos.as_point()[1]))

    def draw(self):
        pygame.draw.lines(
            self.image,
            (155, 0, 0),
            False,
            [
                (40, 20),
                (38, 18),
                (32, 18),
                (32, 16),
                (26, 16),
                (26, 18),
                (26, 15),
                (30, 10),
                (30, 8),
                (36, 8),
                (36, 6),
                (30, 6),
                (28, 4),
                (20, 4),
                (14, 10),
                (10, 10),
                (6, 14),
                (6, 24),
                (12, 30),
                (16, 30),
                (22, 36),
                (30, 36),
                (32, 34),
                (36, 34),
                (36, 32),
                (30, 32),
                (30, 30),
                (26, 26),
                (26, 22),
                (26, 24),
                (32, 24),
                (32, 22),
                (38, 22),
                (40, 20),
            ],
        )
        pygame.draw.rect(self.image, (255, 255, 255), self.image.get_rect(), 1)

    def render(self, screen):
        r = Vector(self.image.get_rect().center)
        rotate_image = pygame.transform.rotate(self.image, self.speed.angle)
        origin_rect = self.image.get_rect()
        rotate_rect = rotate_image.get_rect()
        rotate_rect.center = origin_rect.center
        rotate_rect.move_ip(self.pos.as_point())
        screen.blit(rotate_image, rotate_rect)
        pygame.draw.line(screen, (0, 255, 0), (self.pos + r).as_point(), ((self.pos + self.speed * 5) + r).as_point())
Example #3
0
class SpaceShip:
    def __init__(self, position):
        self.default_position = Vector(position)  # Стандартная позиция (центр экрана_
        self.position = Vector(position)        # Вектор позиции объекта
        self.speed = Vector((3, 0))     # Вектор скорости
        self.speed_rotate = 10      # Угол поворота вектора скорости
        self.image = pygame.Surface((30, 50))     # Поверхность, где будет отрисовываться объект
        self.state = NORMAL
        self.draw()

    def update(self):
        # Функция обновляет позицию объекта на экране
        # # Заставляет пролетать объект сквозь границы окна
        if self.position.as_point()[0] > 801:
            self.position = Vector((0, self.position.as_point()[1]))
        if self.position.as_point()[1] > 601:
            self.position = Vector((self.position.as_point()[0], 0))
        if self.position.as_point()[1] < -1:
            self.position = Vector((self.position.as_point()[0], 599))
        if self.position.as_point()[0] < -1:
            self.position = Vector((799, self.position.as_point()[1]))
        # # Поворот объекта
        if self.state == TURN_LEFT:
            self.speed.rotate(-self.speed_rotate)
        if self.state == TURN_RIGHT:
            self.speed.rotate(self.speed_rotate)
        # # Ускорение и замедление объкта
        if self.state == SPEED_DOWN:
            self.speed = self.speed - self.speed.normalize()
        if self.state == SPEED_UP:
            self.speed = self.speed + self.speed.normalize()
        # # Возвращение объекта в центр экрана
        if self.state == COMEBACK:
            self.position = self.default_position

        self.position += self.speed

    def events(self, event):
        # События
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.state = TURN_LEFT
            if event.key == pygame.K_RIGHT:
                self.state = TURN_RIGHT
            if event.key == pygame.K_SPACE:
                self.state = COMEBACK
            if event.key == pygame.K_DOWN:
                self.state = SPEED_DOWN
            if event.key == pygame.K_UP:
                self.state = SPEED_UP
        if event.type == pygame.KEYUP:
            self.state = NORMAL

    def draw(self):
        # Отрисовывает корабль на поверхности
        pygame.draw.lines(self.image, (255, 255, 255), False, [(2, 2), (12, 10), (26, 13),
                                                               (12, 17), (2, 24), (2, 16),
                                                               (7, 13), (2, 10), (2, 2)])

    def render(self, screen):
        # Вывод изображения на экран
        origin_rect = self.image.get_rect()
        rotate_image = pygame.transform.rotate(self.image, self.speed.angle)
        rotate_rect = rotate_image.get_rect()
        rotate_rect.center = origin_rect.center
        rotate_rect.move_ip(self.position.as_point())
        screen.blit(rotate_image, rotate_rect)
        screen.blit(rotate_image, rotate_rect)
Example #4
0
class Ship:
    def __init__(self, pos):
        self.pos = Vector(pos)
        self.image = pygame.Surface((65, 40), pygame.SRCALPHA)
        self.speed = Vector((5.8, 7.4))
        self.state = NORMAL
        self.direction = self.speed
        self.draw()
        self.image_time = self.image

    def events(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.state = TURN_LEFT
            if event.key == pygame.K_RIGHT:
                self.state = TURN_RIGHT
            if event.key == pygame.K_UP:
                self.state = SPEED_UP
            if event.key == pygame.K_DOWN:
                self.state = SPEED_DOWN
        if event.type == pygame.KEYUP:
            self.state = NORMAL

    def update(self):
        if self.state == TURN_LEFT:
            self.speed.rotate(-5)

        if self.state == TURN_RIGHT:
            self.speed.rotate(5)

        if self.state == SPEED_DOWN:
            self.speed -= self.speed.normalize()
            if self.speed.len < 1 and self.speed.len != 0:
                self.direction = self.speed
                self.speed = Vector((0, 0))

        if self.state == SPEED_UP:
            if self.speed.len < 1:
                self.speed = self.direction
            self.speed += self.speed.normalize()
        self.pos += self.speed

        if self.pos.x > 800:
            self.pos.x = 0
        if self.pos.x < 0:
            self.pos.x = 800
        if self.pos.y > 600:
            self.pos.y = 0
        if self.pos.y < 0:
            self.pos.y = 600

    def draw(self):
        pygame.draw.polygon(self.image, (220, 220, 220), [(0, 0), (10, 10), (55, 20), (10, 30),
                                                          (0, 40), (0, 30), (7, 20), (0, 10)])

    def render(self, screen):
        image_rotate = pygame.transform.rotate(self.image, self.speed.angle)
        origin_rec = self.image.get_rect()
        rotate_rec = image_rotate.get_rect()
        rotate_rec.center = origin_rec.center
        rotate_rec.move_ip(self.pos.as_point())
        screen.blit(image_rotate, rotate_rec)
        pygame.draw.line(screen, (0, 255, 0), self.pos.as_point(), (self.pos + self.speed*10).as_point())
Example #5
0
class Ship:
    def __init__(self, pos):
        self.pos = Vector(pos)
        self.image = pygame.Surface((40, 40), pygame.SRCALPHA)
        # self.copyimage = self.image.copy()
        self.speed = Vector((0, 0))
        self.angle_speed = 5
        self.state = NORMAL
        self.direction = Vector((1, 0))

        self.draw()

    def events(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.state = TURN_LEFT
            if event.key == pygame.K_RIGHT:
                self.state = TURN_RIGHT
            if event.key == pygame.K_DOWN:
                self.state = SLOW
            if event.key == pygame.K_UP:
                self.state = FAST
            if event.key == pygame.K_ESCAPE:
                sys.exit()
        if event.type == pygame.KEYUP:
            self.state = NORMAL

    def update(self):
        if self.pos.x > Display[0]:
            self.pos = Vector((-50, self.pos.y))
        if self.pos.y > 850:
            self.pos = Vector((self.pos.x, -50))
        if self.pos.y < -50:
            self.pos = Vector((self.pos.x, 850))
        if self.pos.x < -50:
            self.pos = Vector((850, self.pos.y))

        if self.state == TURN_LEFT:
            self.speed.rotate(-self.angle_speed)
            # (Так не надо)self.image = pygame.transform.rotate(self.copyimage, self.angle + Angle)

        if self.state == TURN_RIGHT:
            self.speed.rotate(self.angle_speed)

        if self.state == FAST:
            if self.speed.len() == 0:
                self.speed = self.direction
            self.speed = self.speed + self.speed.normalize()
        if self.speed.len() == 0:
            return

        if self.state == SLOW:
            if self.speed.len() < 1:
                self.direction = self.speed
                self.speed = Vector((0, 0))

            else:
                self.speed = self.speed - self.speed.normalize()

        self.pos += self.speed

    def draw(self):
        pygame.draw.lines(self.image, (0, 0, 200), False, [(35, 20), (20, 15), (0, 0), (0, 40), (20, 25), (35, 20)])

    def render(self, screen):
        r = Vector(self.image.get_rect().center)  # r - центр фигуры для вектора направления
        rotate_image = pygame.transform.rotate(self.image, self.speed.angle)
        origin_rect = self.image.get_rect()
        rotate_rect = rotate_image.get_rect()
        rotate_rect.center = origin_rect.center
        rotate_rect.move_ip(self.pos.as_point())
        screen.blit(rotate_image, rotate_rect)
        pygame.draw.line(screen, (200, 255, 200), (self.pos + r).as_point(),
                         (self.pos + self.speed * 20 + r).as_point())
        pygame.draw.lines(self.image, (0, 0, 200), False, [(35, 20), (20, 15), (0, 0), (0, 40), (20, 25), (35, 20)])
Example #6
0
class Game_Object:
    def __init__(self, coords=(0,0), speed=(0,0)):
        self.coords = Vector(coords)
        self.speed = Vector(speed)  #пикселей/сек
        self.max_speed = 100
        self.len_speed = self.speed.len()
        self.color = (250,0,0)
        self.acsel = Vector((0,0))
        self.len_acsel = self.acsel.len()
        self.d_acsel = 0            #Изменение ускорения, x пикс/сек
        self.friction = 0         #Сила трения
        self.status = STOP
        self.angle_speed = 40      #Угловая скорость, градусов/сек

    def event(self, event):
        """
        Обработка событий объектом
        """
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                self.status = TURN_RIGHT
            elif event.key == K_RIGHT:
                self.status = TURN_LEFT
            elif event.key == K_UP:
                self.d_acsel = +12
            elif event.key == K_DOWN:
                self.d_acsel = -6
        elif event.type == KEYUP:
            if event.key == K_LEFT:
                self.status = MOVE
            elif event.key == K_RIGHT:
                self.status = MOVE
            elif event.key == K_UP or event.key == K_DOWN:
                self.d_acsel = 0

    def change_acselerate(self):
        self.acsel = self.speed.normalize()* (self.d_acsel + self.friction) #?
        self.len_acsel = self.acsel.len()

    def change_speed(self, dt):
        self.speed+= self.acsel*(dt/1000)
        self.len_speed = self.speed.len()
        #fixme: добавить ограничение на max_speed

    def move(self, dt):
        self.coords += (self.speed*(dt/1000))

    def update(self, dt):
        """
        Обновление состояния объкта (вызывается каждый кадр)
        """
        if self.status == TURN_LEFT:
            self.speed.rotate(self.angle_speed/1000*dt)
        elif self.status == TURN_RIGHT:
            self.speed.rotate(-self.angle_speed/1000*dt)
        self.change_acselerate()
        self.change_speed(dt)
        self.move(dt)

    def render(self, screen):
        center = self.coords.as_point()
        dx = 4
        dy = 4
        #cross
        pygame.draw.line(screen, (0,250,0), (self.coords.x, self.coords.y-dy),(self.coords.x, self.coords.y+dy))
        pygame.draw.line(screen, (0,250,0), (self.coords.x-dx, self.coords.y),(self.coords.x+dx, self.coords.y))

        pygame.draw.line(screen, self.color, (self.coords.as_point()),(self.coords+self.speed).as_point())
        pygame.draw.line(screen, (0,0,250), (self.coords.as_point()),(self.coords+self.acsel*2).as_point(),4)
Example #7
0
class Ship:
    def __init__(self, pos):
        self.pos = Vector(pos)
        self.image = pygame.Surface((50, 20), pygame.SRCALPHA)
        self.speed = Vector((50, 0))
        self.speed_rotate = 90
        self.boost = 100 # ускорение
        self.w = 50
        self.h = 20
        self.state = NORMAL
        self.last_state = NORMAL  # ?? тема с поворотом
        self.draw()

    def events(self, event):  # !
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.state = TURN_LEFT
            if event.key == pygame.K_RIGHT:
                self.state = TURN_RIGHT
            if event.key == pygame.K_DOWN:
                self.state = TURN_DOWN
            if event.key == pygame.K_UP:
                self.state = TURN_UP
        if event.type == pygame.KEYUP:
            self.state = NORMAL

    def update(self, dt):  # !
        if self.state == TURN_LEFT:
            self.speed.rotate(-self.speed_rotate*(dt/1000))
        if self.state == TURN_RIGHT:
            self.speed.rotate(self.speed_rotate*(dt/1000))
        if self.state == TURN_DOWN:
            self.speed -= self.speed.normalize()*self.boost*(dt/1000)
        if self.state == TURN_UP:
            self.speed += self.speed.normalize()*self.boost*(dt/1000)
        # self.speed *= (dt/1000)
        self.pos += self.speed*(dt/1000)
        #self.last_state = self.state
        if self.pos.x < 0:
            self.pos.x = DISPLAY_W - self.w
        if self.pos.x > DISPLAY_W - self.w:
            self.pos.x = 0
        if self.pos.y < self.h:
            self.pos.y = DISPLAY_H -  self.h
        if self.pos.y > DISPLAY_H - self.h:
            self.pos.y = self.h

    def draw(self):

        # pygame.draw.polygon(self.image, COLOR_SPACEHIP, [(0, 0), (0, self.h), (self.w * 0.75, self.h * (3 / 4)),
        #                                                  (self.w * (5 / 6), self.h * (3 / 4)), (self.w / 2, self.h / 2),
        #                                                  (self.w * (5 / 6), self.h * (1 / 4)),
        #                                                  (self.w / 4, self.h * (1 / 4)),
        #                                                  (0, 0)])
        # pygame.draw.rect(self.image, COLOR_SPACEHIP, [(0, 0), (self.w, self.h)], 1)
        pygame.draw.polygon(self.image, COLOR_SPACEHIP, [(0, 0), (0, self.h), (self.w, self.h * 0.5), (0, 0)])


    def render(self, screen):

        rot_image = pygame.transform.rotate(self.image, self.speed.angle)
        rot_rect = rot_image.get_rect()
        rot_rect.center = self.image.get_rect().center
        rot_rect.move_ip(self.pos.as_point())
        # self.image = image.subsurface(rot_rect).copy()
        #
        dv = Vector((self.w / 2, self.h / 2))
        screen.blit(rot_image, rot_rect)
        # screen.blit(self.image, self.pos.as_point())
        pygame.draw.line(screen, COLOR_LINE, (self.pos + dv).as_point(), (self.pos + dv + self.speed).as_point())