最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

飛機(jī)大戰(zhàn)1.0(改編于編程候老師)

2023-02-17 19:16 作者:xiaotiaotiao2011  | 我要投稿

import pygame ?# 導(dǎo)入pygame
import random ?# 導(dǎo)入random
import os


# 設(shè)置常量,常量就是在python中不變的量

FPS = 60 ?# FPS是指幀數(shù),指在一秒內(nèi)運(yùn)行多少次
WIDTH = 500
HEIGHT = 600
WHITE = (255, 255, 255) ?# 這是一個(gè)RGB,R表示red,G表示green,B表示blue,數(shù)值是一個(gè)元組,必須帶括號(hào)
GREEN = (0, 255, 0)
RED = (225, 0, 0)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
分?jǐn)?shù)=0
big_or_small=random.randint(30,50)

# 初始化pygame

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(
? ?(WIDTH, HEIGHT)) ?# 創(chuàng)建一個(gè)窗口,寬為WIDTH常量,高為HEIGHT常量
clock = pygame.time.Clock() ?# 設(shè)置幀數(shù)

background_img=pygame.image.load(os.path.join("img", "background.png")).convert()

player_img=pygame.image.load(os.path.join("img","player.png")).convert()

rock_img=pygame.image.load(os.path.join("img","rock.png")).convert()

bullet_img=pygame.image.load(os.path.join("img","bullet.png")).convert()

pygame.display.set_caption('plane_war') ?# 窗口標(biāo)題為字符串“plane_war”,引號(hào)是英文

hoot_sound=pygame.mixer.Sound(os.path.join("sound","shoot.wav"))
expl_sounds=[pygame.mixer.Sound(os.path.join("sound","expl0.wav")),
? ? ? ? ? ? pygame.mixer.Sound(os.path.join("sound","expl1.wav"))]

pygame.mixer.music.load(os.path.join("sound","background.ogg"))
pygame.mixer.music.set_volume(0.3)

font_name=pygame.font.match_font('微軟雅黑')

def draw_text(surf,text,size,x,y):
? ?font=pygame.font.Font(font_name,size)
? ?text_surface=font.render(text,True,WHITE)
? ?text_rect=text_surface.get_rect()
? ?text_rect.centerx=x
? ?text_rect.top=y
? ?surf.blit (text_surface,text_rect)

class Player(pygame.sprite.Sprite): ?# 設(shè)置一個(gè)類(lèi),繼承父類(lèi)pygame.sprite.Sprite

? ?def __init__(self): ?# 設(shè)置初始化內(nèi)容

? ? ? ?pygame.sprite.Sprite.__init__(self)

? ? ? ?self.image = pygame.transform.scale(player_img,(50,40))

? ? ? ?self.image.set_colorkey((BLACK))

? ? ? ?self.rect = self.image.get_rect() ?# 獲取長(zhǎng)方形的矩形位置

? ? ? ?self.rect.centerx = WIDTH/2 ?# 設(shè)置長(zhǎng)方形中心在x軸的中心

? ? ? ?self.rect.bottom = HEIGHT-20 ?# 設(shè)置長(zhǎng)方形的底部在y軸的580位置

? ? ? ?self.speedx = 8 ?# 設(shè)置每次移動(dòng)的像素(px)

? ? ? ?self.health=5

? ?def update(self): ?# 定義update函數(shù)

? ? ? ?key_pressed = pygame.key.get_pressed() ?# 獲取鍵盤(pán)狀態(tài)

? ? ? ?# 判斷鍵盤(pán)狀態(tài)

? ? ? ?if key_pressed[pygame.K_RIGHT]: ?# 判斷鍵盤(pán)右鍵是否按下

? ? ? ? ? ?self.rect.x += self.speedx

? ? ? ?if key_pressed[pygame.K_LEFT]: ?# 判斷鍵盤(pán)左鍵是否按下

? ? ? ? ? ?self.rect.x -= self.speedx

? ? ? ?# 判斷位置

? ? ? ?if self.rect.right > WIDTH: ?# 如果右邊的位置大于寬

? ? ? ? ? ?self.rect.right = WIDTH

? ? ? ?if self.rect.left < 0: ?# 如果左邊小于寬

? ? ? ? ? ?self.rect.left = 0

? ?def shoot(self):

? ? ? ?bullet = Bullet(self.rect.centerx+7, self.rect.centery)
? ? ? ?all_sprites.add(bullet)
? ? ? ?bullets.add(bullet)
? ? ? ?hoot_sound.play()
? ? ? ?bullet = Bullet(self.rect.centerx-7 , self.rect.centery)
? ? ? ?all_sprites.add(bullet)
? ? ? ?bullets.add(bullet)
? ? ? ?hoot_sound.play()
? ?def k_shoot(self):
? ? ? ?for i in range(100):
? ? ? ? ? ?偏差 = random.randrange(-250, 250)
? ? ? ? ? ?bullet = Bullet(self.rect.centerx+偏差, self.rect.centery)
? ? ? ? ? ?all_sprites.add(bullet)
? ? ? ? ? ?bullets.add(bullet)
? ? ? ? ? ?hoot_sound.play()

class Rock(pygame.sprite.Sprite ): ?# 設(shè)置一個(gè)類(lèi),繼承父類(lèi)pygame.sprite.Sprite

? ?def __init__(self ?): ?# 設(shè)置初始化內(nèi)容
? ? ? ?pygame.sprite.Sprite.__init__(self)
? ? ? ?self.image_origin=rock_img
? ? ? ?self.image_origin.set_colorkey(BLACK)
? ? ? ?self.image=self.image_origin.copy()
? ? ? ?self.image = pygame.transform.scale(rock_img, (big_or_small, big_or_small))

? ? ? ?self.image.set_colorkey((BLACK))

? ? ? ?self.rect = self.image.get_rect() ?# 獲取長(zhǎng)方形的矩形位置
? ? ? ?# 隨機(jī)生成位置和速度

? ? ? ?try:

? ? ? ? ? ?self.rect.x = random.randint(0, WIDTH-self.rect.x)
? ? ? ? ? ?self.rect.y = random.randint(-40, -10)
? ? ? ? ? ?self.speedy = random.randint(2, 10) ?# 設(shè)置每次移動(dòng)的像素(px)
? ? ? ? ? ?self.speedx = random.randint(-3, 3)

? ? ? ?except:

? ? ? ? ? ?self.rect.x = random.randint(0, 470)
? ? ? ? ? ?self.rect.y = -10
? ? ? ? ? ?self.speedy = random.randint(2, 10)
? ? ? ? ? ?self.speedx = random.randint(-3, 3)

? ? ? ?self.total_degree=0
? ? ? ?self.rot_degree=random.randrange(-3,3)
? ?def rotate(self):
? ? ? ?self.total_degree+=3
? ? ? ?self.total_degree%=360
? ? ? ?self.image=pygame.transform.rotate(self.image_origin,self.rot_degree)
? ? ? ?center=self.rect.center
? ? ? ?self.rect=self.image.get_rect()
? ? ? ?self.rect.center=center
? ?def update(self): ?# 定義update函數(shù)

? ? ? ?self.rect.y += self.speedy ?# y軸增加

? ? ? ?self.rect.x += self.speedx ?# x軸增加

? ? ? ?if self.rect.top > HEIGHT or self.rect.left > WIDTH or self.rect.right < 0:

? ? ? ? ? ?try:

? ? ? ? ? ? ? ?self.rect.x = random.randint(0, WIDTH-self.rect.x)

? ? ? ? ? ? ? ?self.rect.y = random.randint(-40, -10)

? ? ? ? ? ? ? ?self.speedy = random.randint(2, 10) ?# 設(shè)置每次移動(dòng)的像素(px)

? ? ? ? ? ? ? ?self.speedx = random.randint(-3, 3)

? ? ? ? ? ?except:

? ? ? ? ? ? ? ?self.rect.x = random.randint(0, 470)

? ? ? ? ? ? ? ?self.rect.y = -10

? ? ? ? ? ? ? ?self.speedy = random.randint(2, 10)

? ? ? ? ? ? ? ?self.speedx = random.randint(-3, 3)

class Bullet(pygame.sprite.Sprite): ?# 設(shè)置一個(gè)類(lèi),繼承父類(lèi)pygame.sprite.Sprite

? ?def __init__(self, x, y): ?# 設(shè)置初始化內(nèi)容

? ? ? ?pygame.sprite.Sprite.__init__(self)

? ? ? ?self.image =pygame.transform.scale(bullet_img,(5,20)) ?# 創(chuàng)建一個(gè)寬10,高20的長(zhǎng)方形,數(shù)值為數(shù)組
? ? ? ?self.image.set_colorkey(BLACK)

? ? ? ?self.rect = self.image.get_rect()
? ? ? ?self.rect.x = x
? ? ? ?self.rect.y = y

? ? ? ?self.speedy = -10 ?# 速度-10

? ?def update(self): ?# 定義update函數(shù)

? ? ? ?self.rect.y += self.speedy ?# y軸增加

? ? ? ?if self.rect.bottom < 0:

? ? ? ? ? ?self.kill() ?# 隱藏


all_sprites = pygame.sprite.Group() ?# 創(chuàng)建一個(gè)所有精靈列表

rocks = pygame.sprite.Group()

bullets = pygame.sprite.Group()

player = Player()

all_sprites.add(player)
pygame.mixer.music.play(-1)

for i in range(8):

? ?rock = Rock()

? ?all_sprites.add(rock)

? ?rocks.add(rock)

? ?big_or_small = random.randint(30, 70)

running = True ?# 設(shè)running為布爾值True,表示真

# 主循環(huán)

while running:

? ?clock.tick(FPS) ?# 設(shè)置幀數(shù)為60/s
? ?big_or_small = random.randint(30, 70)
? ?for event in pygame.event.get(): ?# 判斷窗口狀態(tài)

? ? ? ?if event.type == pygame.QUIT: ?# 檢測(cè)窗口是否關(guān)閉

? ? ? ? ? ?running = False ?# 如果是,將running設(shè)為False,停止判斷

? ? ? ?elif event.type == pygame.KEYDOWN:

? ? ? ? ? ?if event.key == pygame.K_SPACE:
? ? ? ? ? ? ? ?player.shoot()
? ? ? ? ? ?if event.key==pygame.K_k:
? ? ? ? ? ? ? ?player.k_shoot()

? ?# 更新游戲

? ?all_sprites.update()

? ?hits_rockandbullet = pygame.sprite.groupcollide(
? ? ? ?rocks, bullets, True, True) ?# 檢測(cè)子彈與隕石是否碰撞
? ?for hit in hits_rockandbullet:
? ? ? ?# 打中一個(gè)再生成一個(gè)
? ? ? ?random.choice(expl_sounds).play()
? ? ? ?r = Rock()
? ? ? ?all_sprites.add(r)
? ? ? ?rocks.add(r)
? ? ? ?small_big_or_small=+int(big_or_small/15 )
? ? ? ?分?jǐn)?shù)+=random.randint(1,2)+small_big_or_small
? ? ? ?print("你的分?jǐn)?shù)是%d分"%分?jǐn)?shù))
? ?# 顯示畫(huà)面
? ?hits_player_and_bullet=pygame.sprite.spritecollide(player,rocks,False)
? ?if hits_player_and_bullet:
? ? ? ?running = False ?# True

? ?screen.fill(BLACK)
? ?screen.blit(background_img,(0,0))
? ?all_sprites.draw(screen)
? ?draw_text(screen,str(分?jǐn)?shù)),18,WIDTH/2,0)
? ?pygame.display.update() ?# 窗口刷新


pygame.quit()


"""

部分內(nèi)容進(jìn)行了刪減

"""

飛機(jī)大戰(zhàn)1.0(改編于編程候老師)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
大悟县| 屏东县| 天门市| 孝义市| 津市市| 婺源县| 康保县| 长海县| 济阳县| 昌黎县| 克什克腾旗| 天津市| 阿鲁科尔沁旗| 辉县市| 苍南县| 西藏| 上林县| 石台县| 鸡西市| 大理市| 阜康市| 英吉沙县| 阳谷县| 彩票| 巴青县| 普格县| 西安市| 化州市| 曲沃县| 酒泉市| 富裕县| 资兴市| 尖扎县| 东台市| 洞口县| 洛浦县| 朔州市| 修文县| 嘉荫县| 长沙县| 灵武市|