def main_menu(): title_font = pygame.font.SysFont('comicsans', 70) run = True while run: WIN.blit(BG, (0, 0)) title_label = title_font.render('Press the mouse to begin...', 1, (255, 255, 255)) WIN.blit(title_label, (WIDTH / 2 - title_label.get_width() / 2, 350)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False import gameStart gameStart.run() if event.type == pygame.MOUSEBUTTONDOWN: main() import gameStart gameStart.run()
def introscreen(): temp_rabbit = Rabbit(44, 47) temp_rabbit.isBlinking = True gameStart = False temp_ground, temp_ground_rect = load_sprite_sheet( 'runningGround.png', 15, 1, -1, -1, -1) temp_ground_rect.left = width / 20 temp_ground_rect.bottom = height logo, logo_rect = load_image('rabbitBack.png', 300, 140, -1) logo_rect.centerx = width * 0.6 logo_rect.centery = height * 0.6 while not gameStart: if pygame.display.get_surface() is None: print("Couldn't load display surface") return True else: for event in pygame.event.get(): if event.type == pygame.QUIT: import gameStart gameStart.run() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: temp_rabbit.isJumping = True temp_rabbit.isBlinking = False temp_rabbit.movement[ 1] = -1 * temp_rabbit.jumpSpeed temp_rabbit.update() if pygame.display.get_surface() is not None: screen.fill(background_col) screen.blit(temp_ground[0], temp_ground_rect) if temp_rabbit.isBlinking: screen.blit(logo, logo_rect) temp_rabbit.draw() pygame.display.update() clock.tick(FPS) if temp_rabbit.isJumping == False and temp_rabbit.isBlinking == False: gameStart = True
def run(): pygame.init() screen = pygame.display.set_mode((576, 760)) clock = pygame.time.Clock() game_font = pygame.font.Font('assets/font/04B_19.ttf', 40) image_path = 'assets/image/flappy/' sound_path = 'assets/sound/' # Game Variables gravity = 0.25 bird_movement = 0 game_active = True score = 0 high_score = 0 bg_surface = pygame.image.load(image_path + 'background.png').convert() bg_surface = pygame.transform.scale2x(bg_surface) floor_surface = pygame.image.load(image_path + 'floor.png').convert() floor_surface = pygame.transform.scale2x(floor_surface) floor_x_pos = 0 bird_surface = pygame.transform.scale2x(pygame.image.load(image_path + 'bird.png').convert_alpha()) bird_rect = bird_surface.get_rect(center=(100, 350)) BIRDFLAP = pygame.USEREVENT + 1 pygame.time.set_timer(BIRDFLAP, 200) pipe_surface = pygame.image.load(image_path + 'pipe.png') pipe_surface = pygame.transform.scale2x(pipe_surface) pipe_list = [] SPAWNPIPE = pygame.USEREVENT pygame.time.set_timer(SPAWNPIPE, 1200) pipe_height = [400, 480, 550] flap_sound = pygame.mixer.Sound(sound_path + 'sfx_wing.wav') death_sound = pygame.mixer.Sound(sound_path + 'sfx_hit.wav') score_sound = pygame.mixer.Sound(sound_path + 'sfx_point.wav') score_sound_countdown = 100 def draw_floor(): screen.blit(floor_surface, (floor_x_pos, 650)) screen.blit(floor_surface, (floor_x_pos + 576, 650)) def create_pipe(): random_pipe_pos = random.choice(pipe_height) bottom_pipe = pipe_surface.get_rect(midtop=(700, random_pipe_pos)) top_pipe = pipe_surface.get_rect(midbottom=(700, random_pipe_pos - 300)) return bottom_pipe, top_pipe def move_pipes(pipes): for pipe in pipes: pipe.centerx -= 5 return pipes def draw_pipes(pipes): for pipe in pipes: if pipe.bottom >= 1024: screen.blit(pipe_surface, pipe) else: flip_pipe = pygame.transform.flip(pipe_surface, False, True) screen.blit(flip_pipe, pipe) def remove_pipes(pipes): for pipe in pipes: if pipe.centerx == -600: pipes.remove(pipe) return pipes def check_collision(pipes): for pipe in pipes: if bird_rect.colliderect(pipe): death_sound.play() return False if bird_rect.top <= -100 or bird_rect.bottom >= 700: return False return True def rotate_bird(bird): new_bird = pygame.transform.rotozoom(bird, -bird_movement * 3, 1) return new_bird def score_display(game_state): if game_state == 'main_game': score_surface = game_font.render(str(int(score)), True, (255, 255, 255)) score_rect = score_surface.get_rect(center=(288, 100)) screen.blit(score_surface, score_rect) if game_state == 'game_over': score_surface = game_font.render(f'Score: {int(score)}', True, (255, 255, 255)) score_rect = score_surface.get_rect(center=(288, 100)) screen.blit(score_surface, score_rect) high_score_surface = game_font.render(f'High score: {int(high_score)}', True, (255, 255, 255)) high_score_rect = high_score_surface.get_rect(center=(288, 850)) screen.blit(high_score_surface, high_score_rect) def update_score(score, high_score): if score > high_score: high_score = score return high_score while True: for event in pygame.event.get(): if event.type == pygame.QUIT: import gameStart gameStart.run() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and game_active: bird_movement = 0 bird_movement -= 9 flap_sound.play() if event.key == pygame.K_SPACE and game_active == False: game_active = True pipe_list.clear() bird_rect.center = (100, 350) bird_movement = 0 score = 0 if event.type == SPAWNPIPE: pipe_list.extend(create_pipe()) screen.blit(bg_surface, (0, 0)) if game_active: # Bird bird_movement += gravity rotated_bird = rotate_bird(bird_surface) bird_rect.centery += bird_movement screen.blit(rotated_bird, bird_rect) game_active = check_collision(pipe_list) # Pipes pipe_list = move_pipes(pipe_list) pipe_list = remove_pipes(pipe_list) draw_pipes(pipe_list) score += 0.01 score_display('main_game') score_sound_countdown -= 1 if score_sound_countdown <= 0: score_sound.play() score_sound_countdown = 100 else: high_score = update_score(score, high_score) score_display('game_over') # Floor floor_x_pos -= 1 draw_floor() if floor_x_pos <= -576: floor_x_pos = 0 pygame.display.update() clock.tick(120)
def quit(self): "Quite pygame and the python interpreter" import gameStart gameStart.run() sys.exit()
def gameplay(): gamespeed = 4 startMenu = False gameOver = False gameQuit = False playerRabbit = Rabbit(44, 47) new_ground = Ground(-1 * gamespeed) scb = Scoreboard() counter = 0 tree = pygame.sprite.Group() eagles = pygame.sprite.Group() clouds = pygame.sprite.Group() last_obstacle = pygame.sprite.Group() Tree.containers = tree Ptera.containers = eagles Cloud.containers = clouds retbutton_image, retbutton_rect = load_image('replay_button.png', 35, 31, -1) gameover_image, gameover_rect = load_image('game_over.png', 190, 11, -1) temp_images, temp_rect = load_sprite_sheet('numbers.png', 12, 1, 11, int(11 * 6 / 5), -1) temp_rect.left += temp_rect.width while not gameQuit: while startMenu: pass while not gameOver: if pygame.display.get_surface() is None: print("Couldn't load display surface") gameQuit = True gameOver = True else: for event in pygame.event.get(): if event.type == pygame.QUIT: gameQuit = True gameOver = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: if playerRabbit.rect.bottom == int(0.98 * height): playerRabbit.isJumping = True if pygame.mixer.get_init() is not None: jump_sound.play() playerRabbit.movement[1] = - \ 1 * playerRabbit.jumpSpeed if event.key == pygame.K_DOWN: if not (playerRabbit.isJumping and playerRabbit.isDead): playerRabbit.isDucking = True if event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: playerRabbit.isDucking = False for c in tree: c.movement[0] = -1 * gamespeed if pygame.sprite.collide_mask(playerRabbit, c): playerRabbit.isDead = True if pygame.mixer.get_init() is not None: die_sound.play() for p in eagles: p.movement[0] = -1 * gamespeed if pygame.sprite.collide_mask(playerRabbit, p): playerRabbit.isDead = True if pygame.mixer.get_init() is not None: die_sound.play() if len(tree) < 2: if len(tree) == 0: last_obstacle.empty() last_obstacle.add(Tree(gamespeed, 40, 40)) else: for l in last_obstacle: if l.rect.right < width * 0.7 and random.randrange( 0, 50) == 10: last_obstacle.empty() last_obstacle.add(Tree(gamespeed, 40, 40)) if len(eagles) == 0 and random.randrange( 0, 200) == 10 and counter > 500: for l in last_obstacle: if l.rect.right < width * 0.8: last_obstacle.empty() last_obstacle.add(Ptera(gamespeed, 46, 40)) if len(clouds) < 5 and random.randrange(0, 300) == 10: Cloud(width, random.randrange(height / 5, height / 2)) playerRabbit.update() tree.update() eagles.update() clouds.update() new_ground.update() scb.update(playerRabbit.score) if pygame.display.get_surface() is not None: screen.fill(background_col) new_ground.draw() clouds.draw(screen) scb.draw() tree.draw(screen) eagles.draw(screen) playerRabbit.draw() pygame.display.update() clock.tick(FPS) if playerRabbit.isDead: gameOver = True if counter % 700 == 699: new_ground.speed -= 1 gamespeed += 1 counter = (counter + 1) if gameQuit: break while gameOver: if pygame.display.get_surface() is None: print("Couldn't load display surface") gameQuit = True gameOver = False else: for event in pygame.event.get(): if event.type == pygame.QUIT: gameQuit = True gameOver = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: gameQuit = True gameOver = False if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: gameOver = False gameplay() if pygame.display.get_surface() is not None: disp_gameOver_msg(retbutton_image, gameover_image) pygame.display.update() clock.tick(FPS) import gameStart gameStart.run()
def gameLoop(): game_over = False game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round( random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round( random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 while not game_over: # 뱀이 벽에 닿았을 때 while game_close == True: dis.fill(white) message("Game Over! Play Again: Press C Quit: Press Q", red) Your_score(Length_of_snake - 1) pygame.display.update() # 게임 start or 게임 종료 for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop() # 방향키로 움직이기 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True import gameStart gameStart.run() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change dis.fill(white) pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) Your_score(Length_of_snake - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round( random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round( random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) import gameStart gameStart.run()
def quit_game(): pygame.quit() import gameStart gameStart.run() sys.exit()
def run(): pygame.font.init() pygame.mixer.init() image_path = 'assets/image/carGame/' sound_path = 'assets/sound/' def player(): screen.blit(cars_img[0], (player_x - int(car_x / 2), player_y - int(car_y / 2))) def enemies(): screen.blit(cars_img[1], (enemies_x - 115, enemy1_y)) screen.blit(cars_img[2], (enemies_x - int(car_x / 2), enemy2_y)) screen.blit(cars_img[3], (enemies_x + 60, enemy3_y)) def text(txt_msg, txt_color, txt_size, txt_x, txt_y): font = pygame.font.SysFont('arial', txt_size, True) txt = font.render(txt_msg, True, txt_color) screen.blit(txt, (txt_x, txt_y)) 'COLORS (RGB)' white = (255, 255, 255) black = (0, 0, 0) 'WINDOW' wid, hei = 400, 500 screen = pygame.display.set_mode((wid, hei)) 'SPRITE SIZE' car_x, car_y = 54, 94 'PLAYER' player_x, player_y = int(wid / 2), int(hei - 50) player_spd = 5 'ENEMIES' enemies_x = int(wid / 2) enemy1_y = randint(-hei, -car_y) enemy2_y = randint(-hei, -car_y) enemy3_y = randint(-hei, -car_y) enemies_spd = 0 'IMAGES' bg = pygame.image.load(image_path + 'Road.png').convert() bg = pygame.transform.scale(bg, (wid, hei)) bg_y = 0 cars_img = [ pygame.image.load(image_path + 'Player_Car.png'), pygame.image.load(image_path + 'Enemy1_Car.png'), pygame.image.load(image_path + 'Enemy2_Car.png'), pygame.image.load(image_path + 'Enemy3_Car.png') ] 'MUSIC' pygame.mixer_music.load(sound_path + 'Chillwave_Nightdrive.mp3') pygame.mixer_music.play(-1) 'SOUND EFFECT' car_collision = pygame.mixer.Sound(sound_path + 'Car_Collision.wav') clock = pygame.time.Clock() score = 1 score_spd = 0 main = True while main: clock.tick(60) bg_y1 = bg_y % bg.get_height() bg_y += 3 screen.blit(bg, (0, bg_y1 - bg.get_height())) if bg_y1 < hei: screen.blit(bg, (0, bg_y1)) player() enemies() pygame.draw.rect(screen, white, (0, 0, 54, 20)) text('S: ' + str(score), black, 15, 0, 0) for event in pygame.event.get(): if event.type == pygame.QUIT: main = False 'CONTROLS' arrows = pygame.key.get_pressed() if arrows[pygame.K_RIGHT] and player_x <= 290: player_x += player_spd if arrows[pygame.K_LEFT] and player_x >= 110: player_x -= player_spd if arrows[pygame.K_UP] and player_y >= 40: player_y -= player_spd if arrows[pygame.K_DOWN] and player_y <= 440: player_y += player_spd 'ENEMIES SPEED' enemy1_y += enemies_spd + 5 enemy2_y += enemies_spd + 2 enemy3_y += enemies_spd + 4 if enemy1_y > hei: enemy1_y = randint(-2500, -2000) if enemy2_y > hei: enemy2_y = randint(-1000, -750) if enemy3_y > hei: enemy3_y = randint(-1750, -1250) 'SCORE' if score_spd <= 60: score_spd += 1 else: score += 1 score_spd = 0 'COLLISION' if player_x - 40 > enemies_x and player_y - 140 < enemy3_y: if player_x - 40 > enemies_x and player_y > enemy3_y: car_collision.play() score -= 10 enemy3_y = randint(-1750, -1250) if player_x + 40 < enemies_x and player_y - 140 < enemy1_y: if player_x + 40 < enemies_x and player_y > enemy1_y: car_collision.play() score -= 10 enemy1_y = randint(-2500, -2000) if player_x - 40 < enemies_x + 10 and player_y - 140 < enemy2_y: if player_x + 40 > enemies_x - 10 and player_y - 140 < enemy2_y: if player_x + 40 > enemies_x - 10 and player_y > enemy2_y: if player_x - 40 < enemies_x - 10 and player_y > enemy2_y: car_collision.play() score -= 10 enemy2_y = randint(-1000, -750) if score <= 0: break pygame.display.update() pygame.quit() import gameStart gameStart.run()
def window(): pygame.init() # - Using the cl_vars for the window screen = pygame.display.set_mode([MAP_SIZE[0], MAP_SIZE[1] + 70]) # - Creating the player class player = Player(WALL_SIZE + 8 * PLAYER_SIZE, WALL_SIZE + 14 * PLAYER_SIZE, PLAYER_SIZE) entities = pygame.sprite.Group() entities.add(player) clock = pygame.time.Clock() score_count = Text(210, 410, 35) score_label = Text(70, 410, 35) level_map = Map() score_value = 0 enemy_value = 200 for i in range(3): # - Pretty much the main game loop # This is a lot simpler than the other game cause its pacman done = False edible = False count = 0 # - Calculating FPS while not done: count += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: import gameStart gameStart.run() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.change_direction(-4, 0) elif event.key == pygame.K_RIGHT: player.change_direction(4, 0) elif event.key == pygame.K_DOWN: player.change_direction(0, 4) elif event.key == pygame.K_UP: player.change_direction(0, -4) elif event.key == pygame.K_SPACE: unpause = False while not unpause: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: unpause = True for i in level_map.enemy_list: i.move(level_map) # - Drawing, THIS SHOULD BE MOVED TO SEPERATE FUNCTION player.move(level_map) screen.fill([0, 0, 30]) score_label.update(screen, 'Score: ') score_count.update(screen, str(score_value)) level_map.button_list.draw(screen) level_map.wall_list.draw(screen) level_map.food_list.draw(screen) level_map.enemy_list.draw(screen) entities.draw(screen) foodlist = pygame.sprite.spritecollide(player, level_map.food_list, False) if edible: if count == 30 * 6: enemy_value = 200 edible = False for i in level_map.enemy_list: i.change_color(i.color) i.edible = False elif count > 30 * 3: for i in level_map.enemy_list: if i.edible: if count % 15 == 0 or count % 15 == 1: i.change_color([150, 255, 150]) elif count % 15 == 2: i.change_color([60, 60, 225]) for i in foodlist: score_value += 10 level_map.food_list.remove(i) buttonlist = pygame.sprite.spritecollide( player, level_map.button_list, False) if buttonlist: edible = True count = 0 for i in buttonlist: level_map.button_list.remove(i) for i in level_map.enemy_list: i.change_color([60, 60, 255]) i.edible = True enemylist = pygame.sprite.spritecollide( player, level_map.enemy_list, False) if enemylist: if enemylist[0].edible: score_value += enemy_value enemy_value *= 2 enemylist[0].waitcount = 0 enemylist[0].wait = True enemylist[0].rect.y = WALL_SIZE + 8 * PLAYER_SIZE enemylist[0].rect.x = WALL_SIZE + 8 * PLAYER_SIZE enemylist[0].change_color(enemylist[0].color) enemylist[0].edible = False else: done = True pygame.display.flip() clock.tick(30) player.rect.x = WALL_SIZE + 8 * PLAYER_SIZE player.rect.y = WALL_SIZE + 14 * PLAYER_SIZE for i in level_map.enemy_list: i.change_color(i.color) i.rect.x = WALL_SIZE + 8 * PLAYER_SIZE i.rect.y = WALL_SIZE + 6 * PLAYER_SIZE enemy_value = 200 clock.tick(2 / 3) import gameStart gameStart.run()
def main(): run = True FPS = 60 level = 0 main_font = pygame.font.SysFont("comicsans", 50) lost_font = pygame.font.SysFont("comicsans", 60) enemies = [] wave_length = 5 enemy_vel = 1 player_vel = 5 laser_vel = 5 player = Player(300, 630) clock = pygame.time.Clock() lost = False lost_count = 0 def redraw_window(): WIN.blit(BG, (0, 0)) # draw text level_label = main_font.render(f"Level: {level}", 1, (255, 255, 255)) WIN.blit(level_label, (WIDTH - level_label.get_width() - 10, 10)) for enemy in enemies: enemy.draw(WIN) player.draw(WIN) if lost: lost_label = lost_font.render('You Lost!!', 1, (255, 255, 255)) WIN.blit(lost_label, (WIDTH / 2 - lost_label.get_width() / 2, 350)) pygame.display.update() while run: clock.tick(FPS) redraw_window() if player.health <= 0: lost = True lost_count += 1 if lost: if lost_count > FPS * 3: run = False else: continue if len(enemies) == 0: level += 1 wave_length += 5 for i in range(wave_length): enemy = Enemy(random.randrange(50, WIDTH - 100), random.randrange(-1500, -100), random.choice(["red", "blue", "green"])) enemies.append(enemy) for event in pygame.event.get(): ### Event Check if event.type == pygame.QUIT: # exit import gameStart gameStart.run() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player.x - player_vel > 0: player.x -= player_vel if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH: player.x += player_vel if keys[pygame.K_UP] and player.y - player_vel > 0: player.y -= player_vel if keys[pygame.K_DOWN] and player.y + player_vel + player.get_height() + 15 < HEIGHT: player.y += player_vel if keys[pygame.K_SPACE]: player.shoot() for enemy in enemies[:]: enemy.move(enemy_vel) enemy.move_lasers(laser_vel, player) if random.randrange(0, 2 * 60) == 1: enemy.shoot() if collide(enemy, player): player.health -= 10 enemies.remove(enemy) elif enemy.y + enemy.get_height() > HEIGHT: enemies.remove(enemy) player.move_lasers(-laser_vel, enemies)
def run(): imagePath = "assets/image/rhythm" WIDTH = 640 HEIGHT = 480 FPS = 60 DEFAULT_FONT = "NotoSansCJKkr-Regular.otf" WHITE = (238, 238, 238) ### color setting BLACK = (32, 36, 32) RED = (246, 36, 74) BLUE = (32, 105, 246) ALPHA_MAX = 255 class Game: def __init__(self): ########################## Game Start pygame.init() pygame.mixer.init() # sound mixer self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) # screen size self.screen_mode = 2 # screen mode (0: logo, 1: logo2, 2: main, 3: stage select, 4: play, 5: score) self.screen_value = [-ALPHA_MAX, 0, 0, 0] # screen management value self.clock = pygame.time.Clock() # FPS timer self.start_tick = 0 # game timer self.running = True # game initialize Boolean value self.language_mode = 0 # 0: english, 1: korean, 2~: custom self.song_select = 1 # select song self.load_date() # data loading self.new() pygame.mixer.music.load(self.bg_main) # bgm def load_date(self): ########################## Data Loading self.dir = os.path.dirname(__file__) ### font self.fnt_dir = os.path.join(self.dir, '../assets/font') self.gameFont = os.path.join(self.fnt_dir, DEFAULT_FONT) with open(os.path.join(self.fnt_dir, 'language.ini'), "r", encoding='UTF-8') as language_file: language_lists = language_file.read().split('\n') self.language_list = [n.split("_") for n in language_lists] ### image self.spr_logoback = pygame.image.load(os.path.join(imagePath, 'logoback.png')) self.spr_circle = pygame.image.load(os.path.join(imagePath, 'circle.png')) self.spr_shot = Spritesheet(os.path.join(imagePath, 'shot.png')) ### sound self.snd_dir = os.path.join(self.dir, '../assets/sound') self.bg_main = os.path.join(self.snd_dir, 'bg_main.ogg') self.sound_click = pygame.mixer.Sound(os.path.join(self.snd_dir, 'click.ogg')) self.sound_drum1 = pygame.mixer.Sound(os.path.join(self.snd_dir, 'drum1.ogg')) self.sound_drum2 = pygame.mixer.Sound(os.path.join(self.snd_dir, 'drum2.ogg')) self.sound_drum3 = pygame.mixer.Sound(os.path.join(self.snd_dir, 'drum3.ogg')) self.sound_drum4 = pygame.mixer.Sound(os.path.join(self.snd_dir, 'drum4.ogg')) ### song self.sng_dir = os.path.join(self.dir, '../assets/song') music_type = ["ogg", "mp3", "wav"] song_lists = [i for i in os.listdir(self.sng_dir) if i.split('.')[-1] in music_type] self.song_list = list() # song name list self.song_path = list() # song path list for song in song_lists: try: pygame.mixer.music.load(os.path.join(self.sng_dir, song)) self.song_list.append(song.split('.')[0]) self.song_path.append(os.path.join(self.sng_dir, song)) except: print("error: " + str(song) + "is unsupported format music file.") self.song_num = len(self.song_list) # available song number self.song_dataPath = list() # song data file path list self.song_highScore = list() # song highscore list self.song_perfectScore = list() # song maxscore list for song in self.song_list: song_dataCoord = os.path.join(self.sng_dir, song + ".ini") try: with open(song_dataCoord, "r", encoding='UTF-8') as song_file: song_scoreList = song_file.read().split('\n')[0] self.song_highScore.append(int(song_scoreList.split(':')[1])) self.song_perfectScore.append(int(song_scoreList.split(':')[2])) self.song_dataPath.append(song_dataCoord) except: self.song_highScore.append(-1) self.song_perfectScore.append(-1) self.song_dataPath.append(-1) def new(self): ########################## Game Initialize self.song_data = list() # song data list self.song_dataLen = 0 # song data len self.song_dataIndex = 0 # song data index self.circle_dir = 1 # circle direction value (benchmark: white / down, right, up, left == 1, 2, 3, 4) self.circle_rot = 0 # circle rotation value self.score = 0 # current game score self.all_sprites = pygame.sprite.Group() # sprite group self.shots = pygame.sprite.Group() def run(self): ########################## Game Loop self.playing = True while self.playing: self.clock.tick(FPS) self.events() self.update() self.draw() pygame.display.flip() pygame.mixer.music.fadeout(600) def update(self): ########################## Game Loop - Update self.all_sprites.update() # screen update self.game_tick = pygame.time.get_ticks() - self.start_tick # play time calculation def events(self): ########################## Game Loop - Events mouse_coord = pygame.mouse.get_pos() # mouse coord value mouse_move = False # mouse move Boolean value mouse_click = 0 # mouse click value (1: left, 2: scroll, 3: right, 4: scroll up, 5: scroll down) key_click = 0 # key value (275: right, 276: left, 273: up, 274: down, 13: enter) for event in pygame.event.get(): ### Event Check if event.type == pygame.QUIT: # exit if self.playing: self.playing, self.running = False, False pygame.quit() import gameStart gameStart.run() elif event.type == pygame.KEYDOWN: # keyboard check key_click = event.key if self.screen_mode < 4: self.sound_click.play() elif event.type == pygame.MOUSEMOTION: if event.rel[0] != 0 or event.rel[1] != 0: # mousemove mouse_move = True elif event.type == pygame.MOUSEBUTTONDOWN: # mouse click mouse_click = event.button if self.screen_mode < 4: self.sound_click.play() if self.screen_mode == 0: ### Logo Screen1 pass elif self.screen_mode == 1: ### Logo Screen2 pass elif self.screen_mode == 2: ### Main Screen if self.screen_value[2] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if self.screen_value[3] > 0: self.screen_value[3] -= ALPHA_MAX / 15 else: for i in range(4): if mouse_move and 400 < mouse_coord[0] < 560 and 105 + i * 70 < mouse_coord[ 1] < 155 + i * 70: # mouse cursor check self.screen_value[1] = i + 1 if (key_click == 273 or mouse_click == 4) and self.screen_value[1] > 1: # key up check self.screen_value[1] -= 1 elif (key_click == 274 or mouse_click == 5) and self.screen_value[1] < 4: # key down check self.screen_value[1] += 1 if ( mouse_click == 1 or key_click == 13 or key_click == 275): # click or key enter, key right check if self.screen_value[1] == 1: # START self.screen_value[2] = 1 elif self.screen_value[1] == 2: # HELP self.screen_value[0] = ALPHA_MAX / 3 self.screen_value[2] = 2 elif self.screen_value[1] == 3: # EXIT self.screen_value[2] = 3 else: # Languague self.language_mode = self.language_mode + 1 if self.language_mode < len( self.language_list) - 1 else 0 self.gameFont = os.path.join(self.fnt_dir, self.load_language(1)) elif self.screen_value[2] == 1: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.screen_mode = 3 self.screen_value[1] = 0 self.screen_value[2] = 0 if self.song_highScore[self.song_select - 1] == -1: pygame.mixer.music.fadeout(600) else: pygame.mixer.music.load(self.song_path[self.song_select - 1]) pygame.mixer.music.play(loops=-1) elif self.screen_value[2] == 2: if mouse_click == 1 or key_click != 0: self.screen_value[2] = 0 elif self.screen_value[2] == 3: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.playing, self.running = False, False elif self.screen_mode == 3: ### Song Select Screen if self.screen_value[2] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 self.screen_value[1] = 0 songChange = False if round(0.31 * WIDTH - 75) < mouse_coord[0] < round(0.31 * WIDTH + 75): # mouse coord check if round(0.125 * HEIGHT + 30) > mouse_coord[1]: self.screen_value[1] = 1 elif round(0.875 * HEIGHT - 30) < mouse_coord[1]: self.screen_value[1] = 2 elif round(0.69 * WIDTH - 75) < mouse_coord[0] < round(0.69 * WIDTH + 75) and round( HEIGHT / 2 + 25) < mouse_coord[1] < round(HEIGHT / 2 + 65): self.screen_value[1] = 3 elif round(0.73 * WIDTH - 75) < mouse_coord[0] < round(0.73 * WIDTH + 75) and round( HEIGHT / 2 + 85) < mouse_coord[1] < round(HEIGHT / 2 + 125): self.screen_value[1] = 4 if (mouse_click == 1): # mouse clickcheck if self.screen_value[1] == 1: if self.song_select > 1: self.song_select -= 1 songChange = True elif self.screen_value[1] == 2: if self.song_select < self.song_num: self.song_select += 1 songChange = True elif self.screen_value[1] == 3: if self.song_highScore[self.song_select - 1] != -1: self.screen_value[2] = 1 elif self.screen_value[1] == 4: self.screen_value[2] = 2 elif key_click == 273 or mouse_click == 4: # key check if self.song_select > 1: self.song_select -= 1 songChange = True elif key_click == 274 or mouse_click == 5: if self.song_select < self.song_num: self.song_select += 1 songChange = True elif key_click == 275 or key_click == 13: if self.song_highScore[self.song_select - 1] != -1: self.screen_value[2] = 1 elif key_click == 276: self.screen_value[2] = 2 if songChange: if self.song_highScore[self.song_select - 1] == -1: pygame.mixer.music.fadeout(600) else: pygame.mixer.music.load(self.song_path[self.song_select - 1]) pygame.mixer.music.play(loops=-1) else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: if self.screen_value[2] == 1: self.screen_mode = 4 self.screen_value[1] = 0 self.screen_value[2] = 0 self.start_tick = pygame.time.get_ticks() self.load_songData() else: self.screen_mode = 2 self.screen_value[1] = 0 self.screen_value[2] = 0 self.screen_value[3] = ALPHA_MAX pygame.mixer.music.load(self.bg_main) pygame.mixer.music.play(loops=-1) elif self.screen_mode == 4: ### Play Screen if self.screen_value[1] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if (mouse_click == 1): # mouse clickcheck if mouse_coord[0] < WIDTH / 2: self.circle_dir += 1 else: self.circle_dir -= 1 elif key_click == 276: # key check self.circle_dir += 1 elif key_click == 275: self.circle_dir -= 1 if self.circle_dir > 4: # circle direction management self.circle_dir = 1 elif self.circle_dir < 1: self.circle_dir = 4 rotToDir = (self.circle_dir - 1) * 90 # circle rotation management if self.circle_rot != rotToDir: if self.circle_rot >= rotToDir: if self.circle_rot >= 270 and rotToDir == 0: self.circle_rot += 15 else: self.circle_rot -= 15 else: if self.circle_rot == 0 and rotToDir == 270: self.circle_rot = 345 else: self.circle_rot += 15 if self.circle_rot < 0: self.circle_rot = 345 elif self.circle_rot > 345: self.circle_rot = 0 self.create_shot() # create shot else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 85 else: self.screen_mode = 5 self.screen_value[1] = 0 else: ### Score Screen if self.screen_value[1] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if mouse_move: if round(WIDTH / 2 - 160) < mouse_coord[0] < round(WIDTH / 2 - 40) and round(HEIGHT / 2 + 110) < \ mouse_coord[1] < round(HEIGHT / 2 + 170): self.screen_value[2] = 1 elif round(WIDTH / 2 + 40) < mouse_coord[0] < round(WIDTH / 2 + 160) and round( HEIGHT / 2 + 110) < mouse_coord[1] < round(HEIGHT / 2 + 170): self.screen_value[2] = 2 if (mouse_click == 1): # mouse clickcheck self.screen_value[1] = self.screen_value[2] elif key_click == 276 or mouse_click == 4: # key check self.screen_value[2] = 1 elif key_click == 275 or mouse_click == 5: self.screen_value[2] = 2 elif key_click == 13: self.screen_value[1] = self.screen_value[2] else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.new() if self.screen_value[1] == 1: self.screen_mode = 3 else: self.screen_mode = 4 self.start_tick = pygame.time.get_ticks() self.load_songData() self.screen_value[1] = 0 self.screen_value[2] = 0 def draw(self): ########################## Game Loop - Draw self.background = pygame.Surface((WIDTH, HEIGHT)) # white background self.background = self.background.convert() self.background.fill(WHITE) self.screen.blit(self.background, (0, 0)) self.draw_screen() # draw screen self.all_sprites.draw(self.screen) pygame.display.update() def draw_screen(self): # Draw Screen screen_alpha = self.screen_value[0] if self.screen_mode == 0: # logo screen1 pass elif self.screen_mode == 1: # logo screen2 pass elif self.screen_mode == 2: # main screen select_index = [True if self.screen_value[1] == i + 1 else False for i in range(4)] if self.screen_value[2] == 0: self.draw_sprite((0, 0), self.spr_logoback, ALPHA_MAX - self.screen_value[3]) else: self.spr_logoback.set_alpha(screen_alpha) logoback_coord = 0 if self.screen_value[2] == 2 else round((screen_alpha - ALPHA_MAX) / 10) self.screen.blit(self.spr_logoback, (logoback_coord, 0)) if self.screen_value[2] == 2: help_surface = pygame.Surface((WIDTH - 60, HEIGHT - 60)) help_surface.fill(WHITE) help_surface.set_alpha(200) self.screen.blit(help_surface, pygame.Rect(30, 30, 0, 0)) self.draw_text("- " + self.load_language(5) + " -", 36, BLACK, 320, 50, 255) self.draw_text(self.load_language(9), 16, BLACK, 320, 150) self.draw_text(self.load_language(10), 16, BLACK, 320, 220) self.draw_text(self.load_language(11), 16, BLACK, 320, 290) else: self.draw_text(self.load_language(2), 36, BLACK, 480, 105, screen_alpha, select_index[0]) self.draw_text(self.load_language(3), 36, BLACK, 480, 175, screen_alpha, select_index[1]) self.draw_text(self.load_language(4), 36, BLACK, 480, 245, screen_alpha, select_index[2]) self.draw_text(self.load_language(0), 24, BLACK, 480, 315, screen_alpha, select_index[3]) elif self.screen_mode == 3: # song select screen surface = pygame.Surface((WIDTH, HEIGHT)) surface.fill(WHITE) surface.set_alpha(max(screen_alpha - 50, 0)) circle_coord = (round(WIDTH * 1.2), round(HEIGHT / 2)) pygame.draw.circle(surface, BLACK, circle_coord, round(0.78 * WIDTH + screen_alpha), 1) pygame.draw.circle(surface, BLACK, circle_coord, round(0.32 * WIDTH + screen_alpha), 1) pygame.draw.circle(surface, BLACK, circle_coord, max(round(-0.1 * WIDTH + screen_alpha), 1), 1) pygame.draw.circle(surface, RED, circle_coord, max(round(-0.12 * WIDTH + screen_alpha), 1), 1) pygame.draw.circle(surface, BLUE, circle_coord, max(round(-0.08 * WIDTH + screen_alpha), 1), 1) self.screen.blit(surface, (0, 0)) if self.song_select > 2: self.draw_text(self.song_list[self.song_select - 3], 16, BLACK, 0.29 * WIDTH, 0.25 * HEIGHT - 20, max(screen_alpha - 220, 0)) if self.song_select > 1: self.draw_text(self.song_list[self.song_select - 2], 18, BLACK, 0.27 * WIDTH, 0.375 * HEIGHT - 20, max(screen_alpha - 180, 0)) self.draw_text(self.song_list[self.song_select - 1], 24, BLACK, 0.25 * WIDTH, 0.5 * HEIGHT - 20, screen_alpha) if self.song_select < self.song_num: self.draw_text(self.song_list[self.song_select], 18, BLACK, 0.27 * WIDTH, 0.625 * HEIGHT - 20, max(screen_alpha - 180, 0)) if self.song_select < self.song_num - 1: self.draw_text(self.song_list[self.song_select + 1], 16, BLACK, 0.29 * WIDTH, 0.75 * HEIGHT - 20, max(screen_alpha - 220, 0)) button_songUp = '▲' if self.screen_value[1] == 1 else '△' button_songDown = '▼' if self.screen_value[1] == 2 else '▽' select_index = [True if self.screen_value[1] == i + 3 else False for i in range(2)] self.draw_text(button_songUp, 24, BLACK, 0.31 * WIDTH, 0.125 * HEIGHT - 20, screen_alpha) self.draw_text(button_songDown, 24, BLACK, 0.31 * WIDTH, 0.875 * HEIGHT - 30, screen_alpha) if self.song_highScore[self.song_select - 1] == -1: self.draw_text(self.load_language(12), 32, RED, 0.71 * WIDTH, HEIGHT / 2 - 100, screen_alpha) else: if self.song_highScore[self.song_select - 1] >= self.song_perfectScore[self.song_select - 1]: try: font = pygame.font.Font(self.gameFont, 36) except: font = pygame.font.Font(os.path.join(self.fnt_dir, DEFAULT_FONT), 36) font.set_bold(True) cleartext_surface = font.render(self.load_language(14), False, BLUE) rotated_surface = pygame.transform.rotate(cleartext_surface, 25) rotated_surface.set_alpha(max(screen_alpha - 180, 0)) cleartext_rect = rotated_surface.get_rect() cleartext_rect.midtop = (round(0.71 * WIDTH), round(HEIGHT / 2 - 150)) self.screen.blit(rotated_surface, cleartext_rect) self.draw_text(self.load_language(8), 28, BLACK, 0.69 * WIDTH, HEIGHT / 2 - 130, screen_alpha) self.draw_text(str(self.song_highScore[self.song_select - 1]), 28, BLACK, 0.69 * WIDTH, HEIGHT / 2 - 70, screen_alpha) self.draw_text(self.load_language(7), 32, BLACK, 0.69 * WIDTH, HEIGHT / 2 + 25, screen_alpha, select_index[0]) self.draw_text(self.load_language(6), 32, BLACK, 0.73 * WIDTH, HEIGHT / 2 + 85, screen_alpha, select_index[1]) elif self.screen_mode == 4: # play screenA surface = pygame.Surface((WIDTH, HEIGHT)) surface.fill(WHITE) surface.set_alpha(max(screen_alpha - 240, 0)) pygame.draw.circle(surface, BLACK, (round(WIDTH / 2), round(HEIGHT / 2)), 200, 1) self.screen.blit(surface, (0, 0)) self.draw_sprite(((WIDTH - 99) / 2, (HEIGHT - 99) / 2), self.spr_circle, screen_alpha, self.circle_rot) time_m = self.game_tick // 60000 time_s = str(round(self.game_tick / 1000) - time_m * 60) if (len(time_s) == 1): time_s = "0" + time_s time_str = str(time_m) + " : " + time_s score_str = self.load_language(13) + " : " + str(self.score) self.draw_text(time_str, 24, BLACK, 10 + len(time_str) * 6, 15, screen_alpha) self.draw_text(score_str, 24, BLACK, WIDTH - 10 - len(score_str) * 6, 15, screen_alpha) else: print("other") surface = pygame.Surface((WIDTH, HEIGHT)) surface.fill(WHITE) surface.set_alpha(max(screen_alpha - 50, 0)) circle_coord = (round(WIDTH / 2), round(HEIGHT / 2)) pygame.draw.circle(surface, BLUE, circle_coord, round(HEIGHT / 2 - 30), 1) pygame.draw.circle(surface, BLACK, circle_coord, round(HEIGHT / 2), 1) pygame.draw.circle(surface, RED, circle_coord, round(HEIGHT / 2 + 30), 1) self.screen.blit(surface, (0, 0)) self.draw_text(self.load_language(15) + " : " + str(self.song_perfectScore[self.song_select - 1]), 32, BLACK, WIDTH / 2, HEIGHT / 2 - 65, screen_alpha) self.draw_text(self.load_language(13) + " : " + str(self.score), 32, BLACK, WIDTH / 2, HEIGHT / 2 - 5, screen_alpha) select_index = [True if self.screen_value[2] == i + 1 else False for i in range(2)] self.draw_text(self.load_language(17), 24, BLACK, WIDTH / 2 - 100, HEIGHT / 2 + 125, ALPHA_MAX, select_index[0]) self.draw_text(self.load_language(16), 24, BLACK, WIDTH / 2 + 100, HEIGHT / 2 + 125, ALPHA_MAX, select_index[1]) def load_language(self, index): try: return self.language_list[self.language_mode][index] except: return "Font Error" def load_songData(self): with open(self.song_dataPath[self.song_select - 1], "r", encoding='UTF-8') as data_file: data_fileLists = data_file.read().split('\n') for data_line in data_fileLists: if data_line != "" and data_line[0] != 's': data_fileList = data_line.split(' - ') time_list = data_fileList[0].split(':') shot_list = data_fileList[1].split(', ') current_songData = list() current_songData.append( int(time_list[0]) * 60000 + int(time_list[1]) * 1000 + int(time_list[2]) * 10) for shot in shot_list: if shot[0] == 'E': shot_color = -1 elif shot[0] == 'W': shot_color = 1 elif shot[0] == 'B': shot_color = 2 elif shot[0] == 'D': shot_color = 3 else: shot_color = 4 if shot_color != -1: if shot[1] == 'D': shot_mode = 0 elif shot[1] == 'R': shot_mode = 90 elif shot[1] == 'U': shot_mode = 180 else: shot_mode = 270 if shot[2] == 'D': shot_dir = 0 elif shot[2] == 'R': shot_dir = 90 elif shot[2] == 'U': shot_dir = 180 else: shot_dir = 270 shot_data = (shot_color, shot_mode, shot_dir, int(shot[3])) current_songData.append(shot_data) else: shot_data = -1 current_songData.append(shot_data) self.song_data.append(current_songData) def create_shot(self): if self.game_tick >= self.song_data[self.song_dataIndex][0]: if self.song_data[self.song_dataIndex][1] != -1: shot_num = len(self.song_data[self.song_dataIndex]) - 1 for shot in range(shot_num): shot_data = self.song_data[self.song_dataIndex][shot + 1] obj_shot = Shot(self, shot_data[0], shot_data[1], shot_data[2], shot_data[3]) self.all_sprites.add(obj_shot) self.shots.add(obj_shot) self.song_dataIndex += 1 else: if self.score >= self.song_highScore[self.song_select - 1]: with open(self.song_dataPath[self.song_select - 1], "r", encoding='UTF-8') as file: file_lists = file.read().split('\n') file_list = 'score:' + str(self.score) + ':' + str( self.song_perfectScore[self.song_select - 1]) + '\n' for shot_file in file_lists: if shot_file != '' and shot_file[0] != 's': file_list += '\n' + shot_file with open(self.song_dataPath[self.song_select - 1], 'w+', encoding='UTF-8') as song_file: song_file.write(file_list) self.song_highScore[self.song_select - 1] = self.score self.screen_value[1] = 1 def draw_sprite(self, coord, spr, alpha=ALPHA_MAX, rot=0): if rot == 0: spr.set_alpha(alpha) self.screen.blit(spr, (round(coord[0]), round(coord[1]))) else: rotated_spr = pygame.transform.rotate(spr, rot) rotated_spr.set_alpha(alpha) self.screen.blit(rotated_spr, (round(coord[0] + spr.get_width() / 2 - rotated_spr.get_width() / 2), round(coord[1] + spr.get_height() / 2 - rotated_spr.get_height() / 2))) def draw_text(self, text, size, color, x, y, alpha=ALPHA_MAX, boldunderline=False): try: font = pygame.font.Font(self.gameFont, size) except: font = pygame.font.Font(os.path.join(self.fnt_dir, DEFAULT_FONT), size) font.set_underline(boldunderline) font.set_bold(boldunderline) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() text_rect.midtop = (round(x), round(y)) if (alpha == ALPHA_MAX): self.screen.blit(text_surface, text_rect) else: surface = pygame.Surface((len(text) * size, size + 20)) surface.fill(WHITE) surface.blit(text_surface, pygame.Rect(0, 0, 10, 10)) surface.set_alpha(alpha) self.screen.blit(surface, text_rect) class Spritesheet: def __init__(self, filename): self.spritesheet = pygame.image.load(filename).convert() def get_image(self, x, y, width, height): image = pygame.Surface((width, height)) image.blit(self.spritesheet, (0, 0), (x, y, width, height)) return image class Shot(pygame.sprite.Sprite): ####################################### Shot Class def __init__(self, game, color, mode, direction, speed): # color(WBDR) mode(DRUL) direction(DRUL) pygame.sprite.Sprite.__init__(self) self.game = game self.color = color self.mode = mode self.direction = direction self.speed = speed self.alpha = ALPHA_MAX self.correct_code = [1, 2, 3, 4] self.correct = 0 image = self.game.spr_shot.get_image((color - 1) * 45, 0, 45, 61) if self.mode == 0: self.image = pygame.transform.rotate(image, 270) self.touch_coord = (round(- self.image.get_width() / 2), round(23 - self.image.get_height() / 2)) elif self.mode == 90: self.image = image self.touch_coord = (round(23 - self.image.get_width() / 2), round(- self.image.get_height() / 2)) elif self.mode == 180: self.image = pygame.transform.rotate(image, 90) self.touch_coord = (round(- self.image.get_width() / 2), round(-23 - self.image.get_height() / 2)) else: self.image = pygame.transform.rotate(image, 180) self.touch_coord = (round(-23 - self.image.get_width() / 2), round(- self.image.get_height() / 2)) self.image.set_colorkey((0, 0, 0)) self.rect = self.image.get_rect() self.rect.x, self.rect.y = round(WIDTH / 2), round(HEIGHT / 2) if self.direction == 0: self.rect.y += round(WIDTH / 2 + 100) elif self.direction == 90: self.rect.x += round(WIDTH / 2 + 100) elif self.direction == 180: self.rect.y -= round(WIDTH / 2 + 100) else: self.rect.x -= round(WIDTH / 2 + 100) self.rect.x += self.touch_coord[0] self.rect.y += self.touch_coord[1] def update(self): self.image.set_alpha(self.alpha) if self.alpha > 0: if self.correct == 1: self.alpha -= ALPHA_MAX / 5 else: if self.correct == -1: self.alpha -= ALPHA_MAX / 85 if self.direction == 0: self.rect.y -= self.speed elif self.direction == 90: self.rect.x -= self.speed elif self.direction == 180: self.rect.y += self.speed else: self.rect.x += self.speed if self.rect.x > WIDTH * 2 or self.rect.x < -WIDTH or self.rect.y > HEIGHT * 2 or self.rect.y < -HEIGHT: self.kill() else: self.kill() if self.correct == 0 and self.rect.x == round(WIDTH / 2) + self.touch_coord[0] and self.rect.y == round( HEIGHT / 2) + self.touch_coord[1]: if self.game.circle_dir == self.correct_code[round(self.mode / 90 - self.color + 1)]: self.game.score += 100 self.correct = 1 if self.color == 1: self.game.sound_drum1.play() elif self.color == 2: self.game.sound_drum2.play() elif self.color == 3: self.game.sound_drum3.play() else: self.game.sound_drum4.play() else: self.correct = -1 game = Game() while game.running: game.run() import gameStart gameStart.run()
def events(self): ########################## Game Loop - Events mouse_coord = pygame.mouse.get_pos() # mouse coord value mouse_move = False # mouse move Boolean value mouse_click = 0 # mouse click value (1: left, 2: scroll, 3: right, 4: scroll up, 5: scroll down) key_click = 0 # key value (275: right, 276: left, 273: up, 274: down, 13: enter) for event in pygame.event.get(): ### Event Check if event.type == pygame.QUIT: # exit if self.playing: self.playing, self.running = False, False pygame.quit() import gameStart gameStart.run() elif event.type == pygame.KEYDOWN: # keyboard check key_click = event.key if self.screen_mode < 4: self.sound_click.play() elif event.type == pygame.MOUSEMOTION: if event.rel[0] != 0 or event.rel[1] != 0: # mousemove mouse_move = True elif event.type == pygame.MOUSEBUTTONDOWN: # mouse click mouse_click = event.button if self.screen_mode < 4: self.sound_click.play() if self.screen_mode == 0: ### Logo Screen1 pass elif self.screen_mode == 1: ### Logo Screen2 pass elif self.screen_mode == 2: ### Main Screen if self.screen_value[2] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if self.screen_value[3] > 0: self.screen_value[3] -= ALPHA_MAX / 15 else: for i in range(4): if mouse_move and 400 < mouse_coord[0] < 560 and 105 + i * 70 < mouse_coord[ 1] < 155 + i * 70: # mouse cursor check self.screen_value[1] = i + 1 if (key_click == 273 or mouse_click == 4) and self.screen_value[1] > 1: # key up check self.screen_value[1] -= 1 elif (key_click == 274 or mouse_click == 5) and self.screen_value[1] < 4: # key down check self.screen_value[1] += 1 if ( mouse_click == 1 or key_click == 13 or key_click == 275): # click or key enter, key right check if self.screen_value[1] == 1: # START self.screen_value[2] = 1 elif self.screen_value[1] == 2: # HELP self.screen_value[0] = ALPHA_MAX / 3 self.screen_value[2] = 2 elif self.screen_value[1] == 3: # EXIT self.screen_value[2] = 3 else: # Languague self.language_mode = self.language_mode + 1 if self.language_mode < len( self.language_list) - 1 else 0 self.gameFont = os.path.join(self.fnt_dir, self.load_language(1)) elif self.screen_value[2] == 1: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.screen_mode = 3 self.screen_value[1] = 0 self.screen_value[2] = 0 if self.song_highScore[self.song_select - 1] == -1: pygame.mixer.music.fadeout(600) else: pygame.mixer.music.load(self.song_path[self.song_select - 1]) pygame.mixer.music.play(loops=-1) elif self.screen_value[2] == 2: if mouse_click == 1 or key_click != 0: self.screen_value[2] = 0 elif self.screen_value[2] == 3: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.playing, self.running = False, False elif self.screen_mode == 3: ### Song Select Screen if self.screen_value[2] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 self.screen_value[1] = 0 songChange = False if round(0.31 * WIDTH - 75) < mouse_coord[0] < round(0.31 * WIDTH + 75): # mouse coord check if round(0.125 * HEIGHT + 30) > mouse_coord[1]: self.screen_value[1] = 1 elif round(0.875 * HEIGHT - 30) < mouse_coord[1]: self.screen_value[1] = 2 elif round(0.69 * WIDTH - 75) < mouse_coord[0] < round(0.69 * WIDTH + 75) and round( HEIGHT / 2 + 25) < mouse_coord[1] < round(HEIGHT / 2 + 65): self.screen_value[1] = 3 elif round(0.73 * WIDTH - 75) < mouse_coord[0] < round(0.73 * WIDTH + 75) and round( HEIGHT / 2 + 85) < mouse_coord[1] < round(HEIGHT / 2 + 125): self.screen_value[1] = 4 if (mouse_click == 1): # mouse clickcheck if self.screen_value[1] == 1: if self.song_select > 1: self.song_select -= 1 songChange = True elif self.screen_value[1] == 2: if self.song_select < self.song_num: self.song_select += 1 songChange = True elif self.screen_value[1] == 3: if self.song_highScore[self.song_select - 1] != -1: self.screen_value[2] = 1 elif self.screen_value[1] == 4: self.screen_value[2] = 2 elif key_click == 273 or mouse_click == 4: # key check if self.song_select > 1: self.song_select -= 1 songChange = True elif key_click == 274 or mouse_click == 5: if self.song_select < self.song_num: self.song_select += 1 songChange = True elif key_click == 275 or key_click == 13: if self.song_highScore[self.song_select - 1] != -1: self.screen_value[2] = 1 elif key_click == 276: self.screen_value[2] = 2 if songChange: if self.song_highScore[self.song_select - 1] == -1: pygame.mixer.music.fadeout(600) else: pygame.mixer.music.load(self.song_path[self.song_select - 1]) pygame.mixer.music.play(loops=-1) else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: if self.screen_value[2] == 1: self.screen_mode = 4 self.screen_value[1] = 0 self.screen_value[2] = 0 self.start_tick = pygame.time.get_ticks() self.load_songData() else: self.screen_mode = 2 self.screen_value[1] = 0 self.screen_value[2] = 0 self.screen_value[3] = ALPHA_MAX pygame.mixer.music.load(self.bg_main) pygame.mixer.music.play(loops=-1) elif self.screen_mode == 4: ### Play Screen if self.screen_value[1] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if (mouse_click == 1): # mouse clickcheck if mouse_coord[0] < WIDTH / 2: self.circle_dir += 1 else: self.circle_dir -= 1 elif key_click == 276: # key check self.circle_dir += 1 elif key_click == 275: self.circle_dir -= 1 if self.circle_dir > 4: # circle direction management self.circle_dir = 1 elif self.circle_dir < 1: self.circle_dir = 4 rotToDir = (self.circle_dir - 1) * 90 # circle rotation management if self.circle_rot != rotToDir: if self.circle_rot >= rotToDir: if self.circle_rot >= 270 and rotToDir == 0: self.circle_rot += 15 else: self.circle_rot -= 15 else: if self.circle_rot == 0 and rotToDir == 270: self.circle_rot = 345 else: self.circle_rot += 15 if self.circle_rot < 0: self.circle_rot = 345 elif self.circle_rot > 345: self.circle_rot = 0 self.create_shot() # create shot else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 85 else: self.screen_mode = 5 self.screen_value[1] = 0 else: ### Score Screen if self.screen_value[1] == 0: if self.screen_value[0] < ALPHA_MAX: self.screen_value[0] += ALPHA_MAX / 15 if mouse_move: if round(WIDTH / 2 - 160) < mouse_coord[0] < round(WIDTH / 2 - 40) and round(HEIGHT / 2 + 110) < \ mouse_coord[1] < round(HEIGHT / 2 + 170): self.screen_value[2] = 1 elif round(WIDTH / 2 + 40) < mouse_coord[0] < round(WIDTH / 2 + 160) and round( HEIGHT / 2 + 110) < mouse_coord[1] < round(HEIGHT / 2 + 170): self.screen_value[2] = 2 if (mouse_click == 1): # mouse clickcheck self.screen_value[1] = self.screen_value[2] elif key_click == 276 or mouse_click == 4: # key check self.screen_value[2] = 1 elif key_click == 275 or mouse_click == 5: self.screen_value[2] = 2 elif key_click == 13: self.screen_value[1] = self.screen_value[2] else: if self.screen_value[0] > 0: self.screen_value[0] -= ALPHA_MAX / 15 else: self.new() if self.screen_value[1] == 1: self.screen_mode = 3 else: self.screen_mode = 4 self.start_tick = pygame.time.get_ticks() self.load_songData() self.screen_value[1] = 0 self.screen_value[2] = 0
def run(): WIDTH = 20 HEIGHT = 15 SIZE = 50 NUM_OF_BOMBS = 20 EMPTY = 0 BOMB = 1 OPENED = 2 global OPEN_COUNT OPEN_COUNT = 0 CHECKED = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)] pygame.init() SURFACE = pygame.display.set_mode([WIDTH * SIZE, HEIGHT * SIZE]) FPSCLOCK = pygame.time.Clock() def num_of_bomb(field, x_pos, y_pos): """ 주위에 있는 폭탄 수를 반환한다 """ count = 0 for yoffset in range(-1, 2): for xoffset in range(-1, 2): xpos, ypos = (x_pos + xoffset, y_pos + yoffset) if 0 <= xpos < WIDTH and 0 <= ypos < HEIGHT and field[ypos][ xpos] == BOMB: count += 1 return count def open_tile(field, x_pos, y_pos): """타일을 오픈""" global OPEN_COUNT if CHECKED[y_pos][x_pos]: # 이미 확인된 타일 return CHECKED[y_pos][x_pos] = True for yoffset in range(-1, 2): for xoffset in range(-1, 2): xpos, ypos = (x_pos + xoffset, y_pos + yoffset) if 0 <= xpos < WIDTH and 0 <= ypos < HEIGHT and field[ypos][ xpos] == EMPTY: field[ypos][xpos] = OPENED OPEN_COUNT += 1 count = num_of_bomb(field, xpos, ypos) if count == 0 and not (xpos == x_pos and ypos == y_pos): open_tile(field, xpos, ypos) def main(): """ 메인 루틴 """ smallfont = pygame.font.SysFont(None, 36) largefont = pygame.font.SysFont(None, 72) message_clear = largefont.render("!!CLEARED!!", True, (0, 255, 225)) message_over = largefont.render("GAME OVER!!", True, (0, 255, 225)) message_rect = message_clear.get_rect() message_rect.center = (WIDTH * SIZE / 2, HEIGHT * SIZE / 2) game_over = False field = [[EMPTY for xpos in range(WIDTH)] for ypos in range(HEIGHT)] # 폭탄을 설치 count = 0 while count < NUM_OF_BOMBS: xpos, ypos = randint(0, WIDTH - 1), randint(0, HEIGHT - 1) if field[ypos][xpos] == EMPTY: field[ypos][xpos] = BOMB count += 1 while True: for event in pygame.event.get(): if event.type == QUIT: return if event.type == MOUSEBUTTONDOWN and event.button == 1: xpos, ypos = floor(event.pos[0] / SIZE), floor( event.pos[1] / SIZE) if field[ypos][xpos] == BOMB: game_over = True else: open_tile(field, xpos, ypos) # 그리기 SURFACE.fill((0, 0, 0)) for ypos in range(HEIGHT): for xpos in range(WIDTH): tile = field[ypos][xpos] rect = (xpos * SIZE, ypos * SIZE, SIZE, SIZE) if tile == EMPTY or tile == BOMB: pygame.draw.rect(SURFACE, (192, 192, 192), rect) if game_over and tile == BOMB: pygame.draw.ellipse(SURFACE, (225, 225, 0), rect) elif tile == OPENED: count = num_of_bomb(field, xpos, ypos) if count > 0: num_image = smallfont.render( "{}".format(count), True, (255, 255, 0)) SURFACE.blit(num_image, (xpos * SIZE + 10, ypos * SIZE + 10)) # 선 그리기 for index in range(0, WIDTH * SIZE, SIZE): pygame.draw.line(SURFACE, (96, 96, 96), (index, 0), (index, HEIGHT * SIZE)) for index in range(0, HEIGHT * SIZE, SIZE): pygame.draw.line(SURFACE, (96, 96, 96), (0, index), (WIDTH * SIZE, index)) # 메시지 나타내기 if OPEN_COUNT == WIDTH * HEIGHT - NUM_OF_BOMBS: SURFACE.blit(message_clear, message_rect.topleft) elif game_over: SURFACE.blit(message_over, message_rect.topleft) pygame.display.update() FPSCLOCK.tick(15) main() import gameStart gameStart.run()
def run(): pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) # 공, 배경화면 출력 변수 back = pygame.Surface((640, 480)) background = back.convert() background.fill((0, 0, 0)) bar = pygame.Surface((10, 50)) bar1 = bar.convert() bar1.fill((255, 255, 255)) bar2 = bar.convert() bar2.fill((255, 255, 255)) circ_sur = pygame.Surface((15, 15)) pygame.draw.circle(circ_sur, (255, 255, 255), (int(15 / 2), int(15 / 2)), int(15 / 2)) circle = circ_sur.convert() circle.set_colorkey((0, 0, 0)) bar1_x, bar2_x = 10., 620. bar1_y, bar2_y = 215., 215. circle_x, circle_y = 307.5, 232.5 bar1_move, bar2_move = 0., 0. speed_x, speed_y, speed_circ = 250., 250., 250. bar1_score, bar2_score = 0, 0 clock = pygame.time.Clock() font = pygame.font.SysFont("calibri", 40) message_win = font.render("You WIN!!", True, (255, 0, 0)) message_lose = font.render("You LOSE!!", True, (255, 0, 0)) done = False while not done: for event in pygame.event.get(): # 유저 이벤트 if event.type == pygame.QUIT: # 창 닫기 done = True # 반복문 탈출 if event.type == KEYDOWN: if event.key == K_UP: bar1_move = -ai_speed elif event.key == K_DOWN: bar1_move = ai_speed elif event.type == KEYUP: if event.key == K_UP: bar1_move = 0. elif event.key == K_DOWN: bar1_move = 0. score1 = font.render(str(bar1_score), True, (255, 255, 255)) score2 = font.render(str(bar2_score), True, (255, 255, 255)) screen.blit(background, (0, 0)) pygame.draw.rect(screen, (255, 255, 255), Rect((5, 5), (630, 470)), 2) pygame.draw.aaline(screen, (255, 255, 255), (330, 5), (330, 475)) screen.blit(bar1, (bar1_x, bar1_y)) screen.blit(bar2, (bar2_x, bar2_y)) screen.blit(circle, (circle_x, circle_y)) screen.blit(score1, (250., 210.)) screen.blit(score2, (380., 210.)) bar1_y += bar1_move # 공 이동 time_passed = clock.tick(30) time_sec = time_passed / 1000.0 circle_x += speed_x * time_sec circle_y += speed_y * time_sec ai_speed = speed_circ * time_sec # 컴퓨터 움직임 if circle_x >= 305.: if not bar2_y == circle_y + 7.5: if bar2_y < circle_y + 7.5: bar2_y += ai_speed if bar2_y > circle_y - 42.5: bar2_y -= ai_speed else: bar2_y == circle_y + 7.5 if bar1_y >= 420.: bar1_y = 420. elif bar1_y <= 10.: bar1_y = 10. if bar2_y >= 420.: bar2_y = 420. elif bar2_y <= 10.: bar2_y = 10. # since i don't know anything about collision, ball hitting bars goes like this. if circle_x <= bar1_x + 10.: if bar1_y - 7.5 <= circle_y <= bar1_y + 42.5: circle_x = 20. speed_x = -speed_x if circle_x >= bar2_x - 15.: if bar2_y - 7.5 <= circle_y <= bar2_y + 42.5: circle_x = 605. speed_x = -speed_x if circle_x < 5.: bar2_score += 1 circle_x, circle_y = 320., 232.5 bar1_y, bar_2_y = 215., 215. elif circle_x > 620.: bar1_score += 1 circle_x, circle_y = 307.5, 232.5 bar1_y, bar2_y = 215., 215. if circle_y <= 10.: speed_y = -speed_y circle_y = 10. elif circle_y >= 457.5: speed_y = -speed_y circle_y = 457.5 if bar1_score >= 15 or bar2_score >= 15: if bar1_score >= 15: screen.blit(message_win, (230., 210.)) else: screen.blit(message_lose, (230., 210.)) done = True pygame.display.update() import gameStart gameStart.run()