You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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
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)
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())
The text was updated successfully, but these errors were encountered:
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
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)
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
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 collisions():
global running, score, game_over
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()
Show game over screen with the last score and restart option
def show_game_over():
global score
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():
asyncio.run(main())
The text was updated successfully, but these errors were encountered: