0

Good day for everyone. I have this question: right now i am creating something like slather.io in pygame. I need the snake to move to the folders in a transparent window and hide them - and this is the most ideal variant. But now I'm trying to find a way to move the snake normally. And I have an idea: you need to draw each block of the snake and delete it after a few seconds - this will give the illusion of movement. I think I've learning python about a year, but this is the first time I've dooing project like this, so I decided to ask how this can be done. Thanks in advance. And sorry for my english. This is my code:

import pygame
from random import randint
from sys import exit
from pygame.locals import *
import threading
import win32api
import win32con
import win32gui
import math
import time


class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos, filename, speed):
        pygame.sprite.Sprite.__init__(self)
        self.correction_angle = 90
        self.image = pygame.image.load(filename).convert_alpha()
        self.rect = self.image.get_rect(center=pos)
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=pos)
        self.vel = pygame.math.Vector2(0, 0)
        self.pos = pygame.math.Vector2(pos)
        self.speed = speed

    def rotate(self):
        flags, hcursor, (self.mousex, self.mousey) = win32gui.GetCursorInfo()
        x, y = self.pos
        mV2 = pygame.math.Vector2(self.mousex, self.mousey)
        self.vel = (mV2 - self.pos).normalize() * self.speed
        self.pos += self.vel
        rel_x, rel_y = self.mousex - x, self.mousey - y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
        self.image = pygame.transform.rotate(self.orig_image, int(angle))
        self.rect = self.image.get_rect(center=(x, y))

    def update(self):
        threading.Timer(0.4, self.kill, args=()).start()
        self.pos += self.vel


pygame.init()
WIDTH = 1800
HEIGHT = 1500
screen = pygame.display.set_mode((HEIGHT, WIDTH), pygame.FULLSCREEN,  pygame.NOFRAME)
hwnd = pygame.display.get_wm_info()["window"] # Handle
styles = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
styles = win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, styles)
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(255, 0, 128), 0, win32con.LWA_COLORKEY)
screen.fill((255, 0, 128))

group = pygame.sprite.Group()
obj = Sprite((250, 250), 'ball.png', 0.6)

while True:
    obj.rotate()
    group.add(obj)
    group.update()
    group.draw(screen)
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
Nay3fire
  • 1
  • 1

0 Answers0