Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection was aborted by the software in your host machine #303 #914

Open
Eyl0l opened this issue Nov 19, 2024 · 0 comments
Open

Connection was aborted by the software in your host machine #303 #914

Eyl0l opened this issue Nov 19, 2024 · 0 comments

Comments

@Eyl0l
Copy link

Eyl0l commented Nov 19, 2024

I tried CORS but it didn't work.

Here is my code:

import asyncio
import pygame
from os.path import join
from random import randint, uniform

Global score variable

score = 0
game_over = False

class Player(pygame.sprite.Sprite):
def init(self, groups):
super().init(groups)
self.image = pygame.image.load(join('images', 'neco.png')).convert_alpha()
self.rect = self.image.get_frect(center = (WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2))
self.direction = pygame.Vector2()
self.speed = 300

    #cooldown
    self.can_shoot = True
    self.papatya_shoot_time = 0
    self.cooldown_duration = 400

    # mask
    self.mask = pygame.mask.from_surface(self.image)

def papatya_timer(self):
    if not self.can_shoot:
        current_time = pygame.time.get_ticks()
        if current_time - self.papatya_shoot_time >= self.cooldown_duration:
            self.can_shoot = True

def update(self, dt):
    keys = pygame.key.get_pressed()
    self.direction.x = int(keys[pygame.K_RIGHT]) - int(keys[pygame.K_LEFT])
    self.direction.y = int(keys[pygame.K_DOWN]) - int(keys[pygame.K_UP])
    self.direction = self.direction.normalize() if self.direction else self.direction
    self.rect.center += self.direction * self.speed * dt

    recent_keys = pygame.key.get_just_pressed()
    if recent_keys[pygame.K_x] and self.can_shoot:
        Papatya(papatya_surf, self.rect.midright, (all_sprites, papatya_sprites))
        self.can_shoot = False
        self.papatya_shoot_time = pygame.time.get_ticks()
        papatya_sound.play()

    self.papatya_timer()

class Star(pygame.sprite.Sprite):
def init(self, groups, surf):
super().init(groups)
self.image = surf
self.rect = self.image.get_frect(center = (randint(0, WINDOW_WIDTH),randint(0, WINDOW_HEIGHT)))

class Papatya(pygame.sprite.Sprite):
def init(self, surf, pos, groups):
super().init(groups)
self.image = surf
self.rect = self.image.get_frect(midbottom = pos)

def update(self, dt):
    self.rect.centerx += 400 * dt
    if self.rect.bottom < 0:
        self.kill()

class Sapsal(pygame.sprite.Sprite):
def init(self, surf, pos, *groups):
super().init(*groups)
self.original_surf = surf
self.image = surf
self.rect = self.image.get_frect(center = pos)
self.start_time = pygame.time.get_ticks()
self.lifetime = 3000
self.direction = pygame.Vector2(-1, 0)
self.speed = randint(400,500)
self.rotation_speed = randint(40,80)
self.rotation = 0

def update(self, dt):
    self.rect.center += self.direction * self.speed * dt
    if pygame.time.get_ticks() - self.start_time >= self.lifetime:
        self.kill()
    self.rotation += self.rotation_speed * dt
    self.image = pygame.transform.rotozoom(self.original_surf, self.rotation, 1)
    self.rect = self.image.get_frect(center = self.rect.center )

class AnimatedExplosions(pygame.sprite.Sprite):
def init(self, frames, pos, groups):
super().init(groups)
self.frames = frames
self.frame_index = 0
self.image = frames[self.frame_index]
self.rect = self.image.get_frect(center = pos)
explosion_sound.play()

def update(self, dt):
    self.frame_index += 20 * dt
    if self.frame_index < len(self.frames):
        self.image = self.frames[int(self.frame_index) % len(self.frames)]
    else:
        self.kill()

def collisions():
global running, score, game_over

# Check for collision between player and sapsal
collision_sprites = pygame.sprite.spritecollide(player, sapsal_sprites, True, pygame.sprite.collide_mask)
if collision_sprites:
    game_over = True  # Trigger game over state

# Check for collision between papatya and sapsal
for papatya in papatya_sprites:
    collided_sprites = pygame.sprite.spritecollide(papatya, sapsal_sprites, True)
    if collided_sprites:
        papatya.kill()
        AnimatedExplosions(explosion_frames, papatya.rect.midright, all_sprites)
        score += 1  # Increase score when papatya collides with sapsal

def display_score():
text_surf = font.render(str(score), True, 'white')
text_rect = text_surf.get_frect(midbottom = (WINDOW_WIDTH / 2,WINDOW_HEIGHT - 50))
display_surface.blit(text_surf, text_rect)
pygame.draw.rect(display_surface, (240,240,240), text_rect.inflate(20,10).move(0,-8), 5, 10)

Reset the game

def reset_game():
global all_sprites, sapsal_sprites, papatya_sprites, player, score, game_over
# Clear all existing sprites
all_sprites.empty()
sapsal_sprites.empty()
papatya_sprites.empty()

# Reinitialize the player
player = Player(all_sprites)

# Reinitialize the stars (20 stars)
for i in range(20):
    Star(all_sprites, star_surf)

# Reset the score
score = 0
game_over = False  # Reset the game over state

print("Game restarted!")

Show game over screen with the last score and restart option

def show_game_over():
global score

display_surface.fill('black')
game_over_text = font.render("Game Over", True, 'white')
score_text = font.render(f"Score: {score}", True, 'white')
restart_text = font.render("Restart game with 'R'", True, 'white')  # Restart text
game_over_text_rect = game_over_text.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3))
score_text_rect = score_text.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2))
restart_text_rect = restart_text.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 1.5))  # Position below the score

display_surface.blit(game_over_text, game_over_text_rect)
display_surface.blit(score_text, score_text_rect)
display_surface.blit(restart_text, restart_text_rect)  # Display restart text

pygame.display.update()

general setup

pygame.init()
WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Faggoteria')
running = True
clock = pygame.time.Clock()

import

sapsal_surf = pygame.image.load(join('images', 'sapsal.png')).convert_alpha()
papatya_surf = pygame.image.load(join('images', 'papatya.png')).convert_alpha()
star_surf = pygame.image.load(join('images', 'star.png')).convert_alpha()
font = pygame.font.Font(join('images', 'Oxanium-Bold.ttf'), 40)
explosion_frames = [pygame.image.load(join('images', 'explosion', f'{i}.png')).convert_alpha() for i in range(8)]

papatya_sound = pygame.mixer.Sound(join('audio', 'laser.wav'))
papatya_sound.set_volume(0.2)
explosion_sound = pygame.mixer.Sound(join('audio', 'explosion.wav'))
explosion_sound.set_volume(0.2 )
damage_sound = pygame.mixer.Sound(join('audio', 'damage.ogg'))
game_music = pygame.mixer.Sound(join('audio', 'game_music.wav'))
game_music.set_volume(0.1)
game_music.play(loops=-1)

sprites

all_sprites = pygame.sprite.Group()
sapsal_sprites = pygame.sprite.Group()
papatya_sprites = pygame.sprite.Group()
for i in range(20):
Star(all_sprites, star_surf)
player = Player(all_sprites)

custom events -> sapsal event

sapsal_event = pygame.event.custom_type()
pygame.time.set_timer(sapsal_event, 500)

async def main():

while True:
    dt = clock.tick() / 1000
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == sapsal_event:
            x, y = randint(WINDOW_WIDTH, WINDOW_WIDTH + 100), randint(0, WINDOW_HEIGHT)
            Sapsal(sapsal_surf, (x, y), all_sprites, sapsal_sprites)

        # If the game is over and the user presses "r", restart the game
        if game_over and event.type == pygame.KEYDOWN and event.key == pygame.K_r:
            reset_game()

    # update
    if not game_over:
        all_sprites.update(dt)
        collisions()

    # draw the game
    if game_over:
        show_game_over()
    else:
        display_surface.fill('pink')
        display_score()
        all_sprites.draw(display_surface)


    pygame.display.update()

    await asyncio.sleep(0)

asyncio.run(main())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant