def shutDown(self, shutDownOk): if shutDownOk: os.system("sudo shutdown -h now") #halt after sys.exit() else: pygame.guit() sys.exit()
# 캐릭터(스프라이트) 불러오기 character = pygame.image.load( "C:\\Users\\hanapsk61\\Documents\\GitHub\\StudyRecord\\Py 나도코딩 활용편\\pygame_basic\\character.png" ) character_size = character.get_rect().size # 이미지의 크기를 구해옴 character_width = character_size[0] # 캐릭터의 가로 크기 character_height = character_size[1] # 캐릭터의 세로 크기 character_x_pos = (screen_width / 2) - (character_width / 2 ) # 화면 가로의 절반 크기에 해당하는 곳에 위치 character_y_pos = screen_height - character_height # 화면 세로 크기 가장 아래에 해당하는 곳에 위치 # 화면 타이틀 설정 pygame.display.set_caption("Nado Game") # 게임 이름 # 이벤트 루프 running = True # 게임이 진행중인가? while running: for event in pygame.event.get(): # 게임이 진행되고있는 동안 "어떠한 이벤트가 발생"하였는가 if event.type == pygame.QUIT: # 창이 닫히는 이벤트가 발생하였는가? running = False # 게임이 진행중이 아님 screen.blit(background, (0, 0)) # 배경 그리기 [배경] 맨왼쪽 맨위에 screen.blit(character, (character_x_pos, character_y_pos)) # 캐릭터 그리기 [캐릭터] pygame.display.update() # 게임화면을 다시 그리기 [배경] # 게임 종료 pygame.guit()
for event in pg.event.get(): # Check for closing window if event.type == pg.QUIT: if self.playing: self.playing = False self.running = False def draw(self): # Game loop - draw self.screen.fill(black) self.all_sprites.draw(self.screen) # after draw everything, flip screen pg.display.flip() def show_start_screen(self): # Game start screen pass def show_go_screen(self): # Game over pass g = Game() g.show_start_screen() while g.running: g.new() g.show_go_screen() pg.guit()
#========================================================================== #创建消息 #========================================================================== for msg in msgs: screen.blit(msg[0], msg[1]) #========================================================================== #刷新屏幕 设定帧率 #========================================================================== pygame.display.flip() #or .update() clock.tick(30) #========================================================================== #调用主函数 处理异常 #========================================================================== if __name__ == "__main__": try: #捕获异常 main() except SystemExit: pass except: traceback.print_exc() #打印出错误的代码 pygame.guit() input()
def main(): pygame.init() white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) #variables: display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('Tytuł') block_size = 10 FPS = 10 clock = pygame.time.Clock() font = pygame.font.SysFont(None, 25) lead_x = display_width / 2 lead_y = display_height / 2 lead_x_change = 0 lead_y_change = 0 gameExit = False while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: lead_x_change = -block_size if event.key == pygame.K_RIGHT: lead_x_change = block_size if event.key == pygame.K_DOWN: lead_y_change = block_size if event.key == pygame.K_UP: lead_y_change = -block_size if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: lead_x_change = 0 if event.key == pygame.K_DOWN or event.key == pygame.K_UP: lead_y_change = 0 #bounderies: if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: gameExit = True lead_x += lead_x_change lead_y += lead_y_change gameDisplay.fill(white) pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size]) gameDisplay.fill(red, rect=[200, 200, 50, 50]) pygame.display.update() clock.tick(FPS) message_to_screen("You lose", red) pygame.display.update() pygame.guit() sys.quit()