Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Code to realize simple aircraft Battle Game

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python code to achieve simple aircraft war game", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Python code to achieve simple aircraft war game" bar!

Function

Player aircraft can move, can fire bullets, enemy planes are randomly generated, free to fall. The function of shooting down enemy aircraft has not been added. It is mainly used to practice class encapsulation and inheritance.

Source code

#-*-coding=utf-8-*-import pygame, randomfrom pygame.locals import * class Base (object): def _ _ init__ (self, x, y, screen): self.screen = screen self.x = x self.y = yclass Player (Base): def _ init__ (self): super (). _ _ init__ Screen) self.playerName = ". / feiji/hero.gif" self.player = pygame.image.load (self.playerName). Convert () self.bullet_list = [] def display (self): self.screen.blit (self.player, (self.x) Self.y)) for bullet in self.bullet_list: bullet.display () bullet.mov () def move_left (self): self.x-= 10 def move_right (self): self.x + = 10 def move_up (self): self.y-= 10 def move_down (self): self.y + = 10 def shot (self ): bullet = Bullet () self.bullet_list.append (bullet) class Bullet (Base): def _ init__ (self): super (). _ init__ (player.x+40) Player.y-20, screen) self.bulletName = ". / feiji/bullet-3.gif" self.bullet = pygame.image.load (self.bulletName). Convert () def display (self): self.screen.blit (self.bullet, (self.x) Self.y)) def mov (self): self.y-= 10class Enemy (Base): def _ _ init__ (self): super (). _ _ init__ (random.randint (1,10) * 40,0 Screen) self.enemyName = ". / feiji/enemy-1.gif" self.enemy = pygame.image.load (self.enemyName). Convert () self.enemy_list = [] self.i = 0 def mov (self): self.y + = 2class EnemyArea (Enemy): def display (self): # create an enemy plane at regular intervals if self.i% 80 = = 0: new_enemy = Enemy () self.enemy_list.append (new_enemy) self.i + = 1 for enemy in self.enemy_list: self.screen.blit (enemy.enemy (enemy.x, enemy.y) enemy.mov () if _ _ name__ = = "_ _ main__": # create a window Used to display content screen = pygame.display.set_mode ((480,890)) # create a picture with window size Used as the background background = pygame.image.load (". / feiji/background.png"). Convert () player = Player () enemy = Enemy () enemy_area = EnemyArea () while True: screen.blit (background, (0) 0) player.display () enemy_area.display () # judge button for event in pygame.event.get (): if event.type = = QUIT: exit () elif event.type = = KEYDOWN: if event.key = = Karma or event.key = = K_LEFT: Player.move_left () elif event.key = = Knowd or event.key = = K_RIGHT: player.move_right () elif event.key = = Knoww or event.key = = K_UP: player.move_up () elif event.key = = Kings or event.key = = K_DOWN: Player.move_down () elif event.key = = K_SPACE: player.shot () elif event.key = = Kenzq: exit () pygame.display.update () pygame.time.delay (10)

Result display

Thank you for your reading, the above is the content of "how to use Python code to achieve simple aircraft war games". After the study of this article, I believe you have a deeper understanding of how to use Python code to achieve simple aircraft war games, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report