上個星期弄了好久的打飛機,折磨了我好久,其實上周末都已經做好了,整體的一些大功能已經實現,缺少的就是一些小功能,比如說選關啊,變身啊,超級炸彈啊,本來想加上去,但是又比較花費時間,主要以后我也沒有想要去從事游戲這么方面的工作,但是Pygame這個東西是真的好用,里面有各種各樣的工具,基本可以滿足你的所有需求,我只是用了幾個必須的方法,做出來了游戲的雛形,以后有時間的話再慢慢優化吧,不過比較Low的事就是我把素材包給換了,我自己再網上找了圖片,自己P了一套素材,整出來一個美少女戰士,驚了吧,我自己也驚了,好好的打飛機不好嗎?其實原理都一樣只是換了一個外殼,廢話不多說,先給大家看看效果圖。
其實就是都做的是飛機沒啥意思,就面向‘肥宅’來一個美少女吧,基本就是這個樣子,簡單的功能,基本沒有什么bug,代碼全部寫在了一個Py文件中,本來想分開寫,然后導入呢,但是有點麻煩,現在我的水平只能達到這樣,等以后有能力了,把代碼優化一下,我基本都是用類來寫的,把代碼封裝起來,顯得簡潔一點吧(雖然也不是那么簡潔),下面給大家看一下我的代碼,一共500多行吧。
總體的代碼就是這樣,下面給大家看看我每個類里面的代碼
每個列表里面放的基本都是圖片
import pygame
import sys
import math
import copy
import random
import os
'''enemy_imgs = [pygame.image.load('images/enemy1.png'),pygame.image.load('images/enemy2.png'),pygame.image.load('images/enemy3.png'),pygame.image.load('images/enemy4.png'),pygame.image.load('images/enemy5.png')
]
'''
history =
0enemy_list = []
enemy_imgs1 = [pygame.transform.scale((pygame.image.load(
'images/diren1.png')), (
120,
130)),pygame.transform.scale((pygame.image.load(
'images/diren1baozha1.png')), (
120,
130)),pygame.transform.scale((pygame.image.load(
'images/diren1baozha2.png')), (
120,
130)),pygame.transform.scale((pygame.image.load(
'images/diren1baozha3.png')), (
120,
130))
]
enemy_imgs2 = [pygame.transform.scale((pygame.image.load(
'images/diren2.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/deren2baozha1.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/diren2baozha2.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/diren2baozha3.png')), (
80,
85))
]
enemy_imgs3 = [pygame.transform.scale((pygame.image.load(
'images/diren3.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/diren3baozha1.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/diren3baozha2.png')), (
80,
85)),pygame.transform.scale((pygame.image.load(
'images/diren3baozha3.png')), (
80,
85))]
enemy_imgs4 = [pygame.transform.scale((pygame.image.load(
'images/direndaboss.png')), (
220,
160)),pygame.transform.scale((pygame.image.load(
'images/bossbaozha1.png')), (
220,
160)),pygame.transform.scale((pygame.image.load(
'images/bossbaozha2.png')), (
220,
160)),pygame.transform.scale((pygame.image.load(
'images/bossbaozha3.png')), (
220,
160)),pygame.transform.scale((pygame.image.load(
'images/bossbaozha4.png')), (
220,
160)),pygame.transform.scale((pygame.image.load(
'images/bossbaozha5.png')), (
220,
160))]bullet_img = [pygame.transform.scale((pygame.image.load(
'images/bullet2.png')), (
36,
34)),pygame.transform.scale((pygame.image.load(
'images/heartbullet.png')), (
36,
34)),pygame.transform.scale((pygame.image.load(
'images/heartbullet.png')), (
36,
34)),
]
bullet_list = []
start_bg = [pygame.image.load(
'images/beijing2.png'),pygame.image.load(
'images/beijing4.png'),pygame.image.load(
'images/zitibiaoti5.png'),pygame.image.load(
'images/starbit.png'),pygame.image.load(
'images/engbg1.png'),pygame.image.load(
'images/beijingtu1.png'),pygame.image.load(
'images/beijingtu2.png')
]
start_star = [pygame.image.load(
"images/star3.png"),pygame.image.load(
"images/star2.png"),pygame.image.load(
"images/star1.png")
]
class StatPanel:is_inside =
Falsedef __init__(self, screen, start_bg=start_bg, start_star=start_star):self.screen = screenself.start_bg = start_bgself.start_star = start_starself.index =
0self.statIndex =
0self.statAddIndex =
0def display(self):self.index +=
0.01self.screen.blit(self.start_bg[
0], (
0,
0))self.screen.blit(self.start_bg[
5], (
10,
520))self.screen.blit(self.start_bg[
6], (
370,
530))self.screen.blit(self.start_bg[
2], (
60,
100 +
30 * math.sin(self.index)))btn = self.screen.blit(self.start_bg[
3], (
180,
380))StatPanel.is_inside = btn.collidepoint(pygame.mouse.get_pos())self.statAddIndex +=
1if self.statAddIndex ==
1000:self.statAddIndex =
0if self.statAddIndex %
7 ==
0:self.statIndex +=
1if self.statIndex ==
3:self.statIndex =
0self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))self.screen.blit(self.start_star[self.statIndex], (random.randint(-
40,
400), random.randint(-
60,
150)))
class EndPanel:global fontObj1
def __init__(self, screen, start_bg=start_bg, start_star=start_star):self.screen = screen
def end_panel(self):endbg = pygame.transform.scale(pygame.image.load(
'images/endbeijing1.png'), (
580,
870))self.screen.blit(endbg, (
0,
0))
def font(self):scoreSurface = fontObj1.render(
'The score:%s' % Hero.score,
True, (
255,
125,
125,
0))screen.blit(scoreSurface, (
180,
400))hisSurface = fontObj1.render(
'Highest score in history :%s' % history,
True, (
255,
125,
125,
0))screen.blit(hisSurface, (
170,
450))
pygame.mixer.init()
sounds = [pygame.mixer.Sound(
'sound/button.ogg')
]
class AllSound:@staticmethoddef play_sounds(sound):if sound !=
None:sound.play()
hero_imgs = [pygame.transform.scale((pygame.image.load(
'images/hero1.png')), (
120,
210)),pygame.transform.scale((pygame.image.load(
'images/hero2.png')), (
120,
210)),pygame.transform.scale((pygame.image.load(
'images/hero3.png')), (
120,
210))]
class Hero(pygame.sprite.Sprite):up =
Falsedown =
Falseleft =
Falseright =
Falsescore =
0def __init__(self, screen, imgs, pos, hp, speed):self.screen = screenself.imgs = imgsself.image = self.imgs[
0]self.rect = self.imgs[
0].get_rect()self.rect.topleft = posself.hp = hpself.speed = speedself.indexAdd =
1self.imgIndex =
0def Move(self):if Hero.right:self.rect = self.rect.move(self.speed,
0)
if Hero.left:self.rect = self.rect.move(-self.speed,
0)
if Hero.down:self.rect = self.rect.move(
0, self.speed)
if Hero.up:self.rect = self.rect.move(
0, -self.speed)self.indexAdd +=
1if self.indexAdd ==
1000:self.indexAdd =
0if self.indexAdd %
97 ==
0:self.imgIndex +=
1if self.imgIndex ==
3:self.imgIndex =
0if self.rect.x <=
0:self.rect.x =
0if self.rect.x >= (self.screen.get_width() - self.imgs[
0].get_width()):self.rect.x = self.screen.get_width() - self.imgs[
0].get_width()
if self.rect.y <=
0:self.rect.y =
0if self.rect.y >= (self.screen.get_height() - self.imgs[
0].get_height()):self.rect.y = self.screen.get_height() - self.imgs[
0].get_height()self.screen.blit(self.imgs[self.imgIndex], self.rect)
if self.indexAdd %
30 ==
0:Bullet((self.rect.centerx - (bullet_img[
0].get_width() /
2), self.rect.y -
20),
4, self.screen)
for i
in bullet_list:
if i !=
None and isinstance(i, Bullet):i.Move()self.Collide()
if self.hp <=
0:
global isOverHistroyScore.UpdateHistory(Hero.score)isOver =
Truedef Collide(self, target=enemy_list):tempEnemy = pygame.sprite.spritecollideany(self, target, collided=pygame.sprite.collide_mask)
if tempEnemy
in enemy_list:self.hp -=
1self.rect.topleft = (
180,
500)tempEnemy.hp -=
10
class Bullet(pygame.sprite.Sprite):def __init__(self, pos, speed, screen, img=bullet_img[0]):self.image = imgself.rect = self.image.get_rect()self.rect.topleft = posself.speed = speedself.screen = screenbullet_list.append(self)
def Move(self):self.rect = self.rect.move(
0, -self.speed)self.screen.blit(self.image, self.rect)
if self.rect.y <= -
69:
if self
in bullet_list:bullet_list.remove(self)self.CollideEnemy()
def CollideEnemy(self, target=enemy_list):tempEnemy = pygame.sprite.spritecollideany(self, target, collided=pygame.sprite.collide_mask)
if tempEnemy
and self
in bullet_list:bullet_list.remove(self)tempEnemy.hp -=
1
class BackGround:def __init__(self, img, speed, screen):self.img1 = imgself.img1rect = self.img1.get_rect()self.img2 = copy.copy(self.img1)self.img2rect = self.img2.get_rect()self.img2rect.y = -
870self.speed = speedself.screen = screen
def Move(self):self.img1rect = self.img1rect.move(
0, self.speed)self.img2rect = self.img2rect.move(
0, self.speed)
if self.img1rect.y >=
880:self.img1rect.y = self.img2rect.y -
870if self.img2rect.y >=
880:self.img2rect.y = self.img1rect.y -
870self.screen.blit(self.img1, self.img1rect)self.screen.blit(self.img2, self.img2rect)
class Enemy(pygame.sprite.Sprite):def __init__(self, img, pos, hp, speed, screen, tag, imgs):self.image = imgself.imgs = imgsself.rect = self.image.get_rect()self.rect.topleft = posself.hp = hpself.speed = speedself.screen = screenself.tag = tagenemy_list.append(self)self.indexAdd =
0self.indexImg =
0def Move(self):if self.hp <=
0:self.dead()
else:self.rect = self.rect.move(
0, self.speed)self.screen.blit(self.image, self.rect)
if self.rect.y >
880:
if self
in enemy_list
and self !=
None:enemy_list.remove(self)
def dead(self):global scoreself.indexAdd +=
1if self.indexAdd ==
1000:self.indexAdd =
0if self.tag ==
'enemy0':
if self.indexAdd %
7 ==
0:self.indexImg +=
1if self.indexImg ==
3:
if self
in enemy_list:Hero.score +=
3enemy_list.remove(self)
if self.tag ==
'enemy1':
if self.indexAdd %
7 ==
0:self.indexImg +=
1if self.indexImg ==
3:
if self
in enemy_list:Hero.score +=
2enemy_list.remove(self)
if self.tag ==
'enemy2':
if self.indexAdd %
7 ==
0:self.indexImg +=
1if self.indexImg ==
3:
if self
in enemy_list:Hero.score +=
2enemy_list.remove(self)
if self.tag ==
'enemy3':
if self.indexAdd %
7 ==
0:self.indexImg +=
1if self.indexImg ==
5:
if self
in enemy_list:Hero.score +=
5enemy_list.remove(self)self.screen.blit(self.imgs[self.indexImg], self.rect)
class EnemyFactor:@staticmethoddef Creat_enemy(screen):r = random.randint(
1,
3200)
if r <
700:Enemy(enemy_imgs1[
0], (random.randint(
0,
460), -
130),
3,
2, screen,
'enemy0', enemy_imgs1)
elif r <
1700:Enemy(enemy_imgs2[
0], (random.randint(
0,
500), -
85),
1,
3, screen,
'enemy1', enemy_imgs2)
elif r <
2700:Enemy(enemy_imgs3[
0], (random.randint(
0,
500), -
85),
1,
3, screen,
'enemy2', enemy_imgs3)
elif r <
3200:Enemy(enemy_imgs4[
0], (random.randint(
0,
360), -
160),
5,
1, screen,
'enemy3', enemy_imgs4)
@staticmethoddef Move():for i
in enemy_list:
if isinstance(i, Enemy):i.Move()
class HistroyScore:def ReadHistory(path='score.txt'):global history
if os.path.exists(path):
with open(path, mode=
'r', encoding=
'utf-8')
as f_r:history = f_r.read()
else:
with open(path, mode=
'w', encoding=
'utf-8')
as f_w:f_w.write(
'0')
def UpdateHistory(score, path='score.txt'):if os.path.exists(path):
with open(path, mode=
'r', encoding=
'utf-8')
as f_r:
if int(f_r.read()) <= score:
with open(path, mode=
'w', encoding=
'utf-8')
as f_w:f_w.write(str(score))screen =
None
def AllEvent():global screen
global is_play
for i
in pygame.event.get():
if i.type == pygame.QUIT:pygame.quit()sys.exit()
if i.type == pygame.MOUSEBUTTONDOWN:
if i.button ==
1 and StatPanel.is_inside
and is_play ==
False:AllSound.play_sounds(sounds[
0])print(
'開始游戲')HistroyScore.ReadHistory()Hero.score =
0heroObj.hp =
5enemy_list.clear()bullet_list.clear()is_play =
Trueif i.type == pygame.KEYDOWN:
if i.key == pygame.K_SPACE:
for j
in enemy_list:j.hp =
0if i.key == pygame.K_ESCAPE:is_play =
FalseAllSound.play_sounds(sounds[
0])
if i.key == pygame.K_w:Hero.up =
Trueif i.key == pygame.K_s:Hero.down =
Trueif i.key == pygame.K_a:Hero.left =
Trueif i.key == pygame.K_d:Hero.right =
Trueif i.type == pygame.KEYUP:
if i.key == pygame.K_w:Hero.up =
Falseif i.key == pygame.K_s:Hero.down =
Falseif i.key == pygame.K_a:Hero.left =
Falseif i.key == pygame.K_d:Hero.right =
Falseif i.type == pygame.USEREVENT +
1 and is_play:EnemyFactor.Creat_enemy(screen)
def Main():global heroObj
global overObj
global screen
global is_play
global isOver
global fontObj1HistroyScore.ReadHistory()pygame.init()pygame.display.init()screen = pygame.display.set_mode((
580,
870))pygame.display.set_caption(
'代表月亮消滅你')icon = pygame.image.load(
"images/heartbullet.png")pygame.display.set_icon(icon)startObj = StatPanel(screen)overObj = EndPanel(screen)heroObj = Hero(screen, hero_imgs, (
180,
500),
5,
3)fontObj = pygame.font.Font(
'ziti/MarkerFelt.ttf',
25)fontObj1 = pygame.font.Font(
'ziti/MarkerFelt.ttf',
25)bg = pygame.transform.scale(start_bg[
4], (
580,
870))bgobj = BackGround(bg,
0, screen)is_play =
FalseisOver =
Falsepygame.mixer.music.load(
'sound/beijing.ogg')pygame.mixer.music.play(-
1)pygame.time.set_timer(pygame.USEREVENT +
1,
1000)
while True:AllEvent()
if is_play:
if isOver ==
False:bgobj.Move()heroObj.Move()EnemyFactor.Move()hpSurface = fontObj.render(
'Hp :',
True, (
255,
125,
125,
0))screen.blit(hpSurface, (
0,
0))scoreSurface = fontObj.render(
'Score :%s' % Hero.score,
True, (
255,
125,
125,
0))screen.blit(scoreSurface, (
0,
30))hisSurface = fontObj.render(
'MAX score :%s' % history,
True, (
255,
125,
125,
0))screen.blit(hisSurface, (
0,
60))
for i
in range(
0, heroObj.hp):screen.blit(bullet_img[
1], (
35 +
35 * i,
0))
else:overObj.end_panel()overObj.font()
else:startObj.display()pygame.display.update()
if __name__ ==
'__main__':Main()
代碼寫的也比較亂,以后再慢慢整理吧,最近要整理的東西太多了。
下面給大家看看正常的飛機大戰素材包:
各種飛機大戰,雷霆戰機,全民飛機大戰的素材包,就等你們來了,心動不如行動,趕快留言吧。
總結
以上是生活随笔為你收集整理的飞机大战项目(源代码(直接用),有效果图,素材包)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。