python飞机大战有游戏背景我方飞机以及子弹的代码
时间: 2023-08-16 11:04:31 浏览: 111
python飞机大战小游戏
5星 · 资源好评率100%
下面是Python飞机大战游戏背景、我方飞机和子弹的代码:
游戏背景代码:
```python
import pygame
class Background(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/background.png").convert_alpha()
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 1
self.rect.left, self.rect.top = (0, 0)
def move(self):
self.rect.top += self.speed
if self.rect.top >= self.height:
self.rect.top = -self.height
```
我方飞机代码:
```python
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load("images/me1.png").convert_alpha()
self.image2 = pygame.image.load("images/me2.png").convert_alpha()
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("images/me_destroy_1.png").convert_alpha(),
pygame.image.load("images/me_destroy_2.png").convert_alpha(),
pygame.image.load("images/me_destroy_3.png").convert_alpha(),
pygame.image.load("images/me_destroy_4.png").convert_alpha()])
self.rect = self.image1.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, self.height - self.rect.height - 60
self.speed = 10
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def move_down(self):
if self.rect.bottom < self.height - 60:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 60
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def move_right(self):
if self.rect.right < self.width:
self.rect.right += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, self.height - self.rect.height - 60
self.active = True
self.invincible = True
```
子弹代码:
```python
import pygame
class Bullet(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 12
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
```
阅读全文