飛機(jī)可以帶pos機(jī)

 新聞資訊2  |   2023-07-26 17:58  |  投稿人:pos機(jī)之家

網(wǎng)上有很多關(guān)于飛機(jī)可以帶pos機(jī),python飛機(jī)大戰(zhàn)源代碼的知識(shí),也有很多人為大家解答關(guān)于飛機(jī)可以帶pos機(jī)的問(wèn)題,今天pos機(jī)之家(www.shbwcl.net)為大家整理了關(guān)于這方面的知識(shí),讓我們一起來(lái)看下吧!

本文目錄一覽:

1、飛機(jī)可以帶pos機(jī)

飛機(jī)可以帶pos機(jī)

具體的代碼:

settings配置

import pygame

class Settings(object):

"""設(shè)置常用的屬性"""

def __init__(self):

self.bgImage = pygame.image.load(\'img/background.png\') # 背景圖

self.bgImagewidth="360px",height="auto" />

self.bgImageHeight = self.bgImage.get_rect()[3] # 背景圖高

self.START=pygame.image.load("img/start.png")

self.pause=pygame.image.load("img/pause.png")

self.gameover=pygame.image.load("img/gameover.png")

self.heroImages = ["img/hero.gif",

"img/hero1.png", "img/hero2.png"] # 英雄機(jī)圖片

self.airImage = pygame.image.load("img/enemy0.png") # airplane的圖片

self.beeImage = pygame.image.load("img/bee.png") # bee的圖片

self.herobullet=pygame.image.load("img/bullet.png")# 英雄機(jī)的子彈

飛行物類

import abc

class FlyingObject(object):

"""飛行物類,基類"""

def __init__(self, screen, x, y, image):

self.screen = screen

self.x = x

self.y = y

self.width="360px",height="auto" />

self.height = image.get_rect()[3]

self.image = image

@abc.abstractmethod

def outOfBounds(self):

"""檢查是否越界"""

pass

@abc.abstractmethod

def step(self):

"""飛行物移動(dòng)一步"""

pass

def shootBy(self, bullet):

"""檢查當(dāng)前飛行物是否被子彈bullet(x,y)擊中"""

x1 = self.x

x2 = self.x + self.width="360px",height="auto" />

y1 = self.y

y2 = self.y + self.height

x = bullet.x

y = bullet.y

return x > x1 and x < x2 and y > y1 and y < y2

def blitme(self):

"""打印飛行物"""

self.screen.blit(self.image, (self.x, self.y))

英雄機(jī)

from flyingObject import FlyingObject

from bullet import Bullet

import pygame

class Hero(FlyingObject):

"""英雄機(jī)"""

index = 2 # 標(biāo)志位

def __init__(self, screen, images):

# self.screen = screen

self.images = images # 英雄級(jí)圖片數(shù)組,為Surface實(shí)例

image = pygame.image.load(images[0])

x = screen.get_rect().centerx

y = screen.get_rect().bottom

super(Hero,self).__init__(screen,x,y,image)

self.life = 3 # 生命值為3

self.doubleFire = 0 # 初始火力值為0

def isDoubleFire(self):

"""獲取雙倍火力"""

return self.doubleFire

def setDoubleFire(self):

"""設(shè)置雙倍火力"""

self.doubleFire = 40

def addDoubleFire(self):

"""增加火力值"""

self.doubleFire += 100

def clearDoubleFire(self):

"""清空火力值"""

self.doubleFire=0

def addLife(self):

"""增命"""

self.life += 1

def sublife(self):

"""減命"""

self.life-=1

def getLife(self):

"""獲取生命值"""

return self.life

def reLife(self):

self.life=3

self.clearDoubleFire()

def outOfBounds(self):

return False

def step(self):

"""動(dòng)態(tài)顯示飛機(jī)"""

if(len(self.images) > 0):

Hero.index += 1

Hero.index %= len(self.images)

self.image = pygame.image.load(self.images[int(Hero.index)]) # 切換圖片

def move(self, x, y):

self.x = x - self.width="360px",height="auto" />

self.y = y - self.height / 2

def shoot(self,image):

"""英雄機(jī)射擊"""

xStep=int(self.width="360px",height="auto" />

yStep=20

if self.doubleFire>=100:

heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+2*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]

self.doubleFire-=3

return heroBullet

elif self.doubleFire<100 and self.doubleFire > 0:

heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]

self.doubleFire-=2

return heroBullet

else:

heroBullet=[Bullet(self.screen,image,self.x+2*xStep,self.y-yStep)]

return heroBullet

def hit(self,other):

"""英雄機(jī)和其他飛機(jī)"""

x1=other.x-self.width="360px",height="auto" />

x2=other.x+self.width="360px",height="auto" />

y1=other.y-self.height/2

y2=other.y+self.height/2+other.height

x=self.x+self.width="360px",height="auto" />

y=self.y+self.height

return x>x1 and x<x2 and y>y1 and y<y2

enemys

import abc

class Enemy(object):

"""敵人,敵人有分?jǐn)?shù)"""

@abc.abstractmethod

def getScore(self):

"""獲得分?jǐn)?shù)"""

pass

award

import abc

class Award(object):

"""獎(jiǎng)勵(lì)"""

DOUBLE_FIRE = 0

LIFE = 1

@abc.abstractmethod

def gettype(self):

"""獲得獎(jiǎng)勵(lì)類型"""

pass

if __name__ == \'__main__\':

print(Award.DOUBLE_FIRE)

airplane

import random

from flyingObject import FlyingObject

from enemy import Enemy

class Airplane(FlyingObject, Enemy):

"""普通敵機(jī)"""

def __init__(self, screen, image):

x = random.randint(0, screen.get_rect()[2] - 50)

y = -40

super(Airplane, self).__init__(screen, x, y, image)

def getScore(self):

"""獲得的分?jǐn)?shù)"""

return 5

def outOfBounds(self):

"""是否越界"""

return self.y < 715

def step(self):

"""移動(dòng)"""

self.y += 3 # 移動(dòng)步數(shù)

Bee

import random

from flyingObject import FlyingObject

from award import Award

class Bee(FlyingObject, Award):

def __init__(self, screen, image):

x = random.randint(0, screen.get_rect()[2] - 60)

y = -50

super(Bee, self).__init__(screen, x, y, image)

self.awardType = random.randint(0, 1)

self.index = True

def outOfBounds(self):

"""是否越界"""

return self.y < 715

def step(self):

"""移動(dòng)"""

if self.x + self.width="360px",height="auto" />

self.index = False

if self.index == True:

self.x += 3

else:

self.x -= 3

self.y += 3 # 移動(dòng)步數(shù)

def getType(self):

return self.awardType

主類

```python

import pygame

import sys

import random

from setting import Settings

from hero import Hero

from airplane import Airplane

from bee import Bee

from enemy import Enemy

from award import Award

START=0

RUNNING=1

PAUSE=2

GAMEOVER=3

state=START

sets = Settings()

screen = pygame.display.set_mode(

(sets.bgImagewidth="360px",height="auto" />

hero=Hero(screen,sets.heroImages)

flyings=[]

bullets=[]

score=0

def hero_blitme():

"""畫(huà)英雄機(jī)"""

global hero

hero.blitme()

def bullets_blitme():

"""畫(huà)子彈"""

for b in bullets:

b.blitme()

def flyings_blitme():

"""畫(huà)飛行物"""

global sets

for fly in flyings:

fly.blitme()

def score_blitme():

"""畫(huà)分?jǐn)?shù)和生命值"""

pygame.font.init()

fontObj=pygame.font.Font("SIMYOU.TTF", 20) #創(chuàng)建font對(duì)象

textSurfaceObj=fontObj.render(u\'生命值:%d\分?jǐn)?shù):%d\火力值:%d\'%(hero.getLife(),score,hero.isDoubleFire()),False,(135,100,184))

textRectObj=textSurfaceObj.get_rect()

textRectObj.center=(300,40)

screen.blit(textSurfaceObj,textRectObj)

def state_blitme():

"""畫(huà)狀態(tài)"""

global sets

global state

if state==START:

screen.blit(sets.start, (0,0))

elif state==PAUSE:

screen.blit(sets.pause,(0,0))

elif state== GAMEOVER:

screen.blit(sets.gameover,(0,0))

def blitmes():

"""畫(huà)圖"""

hero_blitme()

flyings_blitme()

bullets_blitme()

score_blitme()

state_blitme()

def nextone():

"""生成敵人"""

type=random.randint(0,20)

if type<4:

return Bee(screen,sets.beeImage)

elif type==5:

return Bee(screen,sets.beeImage) #本來(lái)準(zhǔn)備在寫(xiě)幾個(gè)敵機(jī)的,后面沒(méi)找到好看的圖片就刪了

else:

return Airplane(screen,sets.airImage)

flyEnteredIndex=0

def enterAction():

"""生成敵人"""

global flyEnteredIndex

flyEnteredIndex+=1

if flyEnteredIndex@==0:

flyingobj=nextOne()

flyings.append(flyingobj)

shootIndex=0

def shootAction():

"""子彈入場(chǎng),將子彈加到bullets"""

global shootIndex

shootIndex +=1

if shootIndex % 10 ==0:

heroBullet=hero.shoot(sets.heroBullet)

for bb in heroBullet:

bullets.append(bb)

def stepAction():

"""飛行物走一步"""

hero.step()

for flyobj in flyings:

flyobj.step()

global bullets

for b in bullets:

b.step()

def outOfBoundAction():

"""刪除越界的敵人和飛行物"""

global flyings

flyingLives=[]

index=0

for f in flyings:

if f.outOfBounds()==True:

flyingLives.insert(index,f)

index+=1

flyings=flyingLives

index=0

global bullets

bulletsLive=[]

for b in bullets:

if b.outOfBounds()==True:

bulletsLive.insert(index,b)

index+=1

bullets=bulletsLive

j=0

def bangAction():

"""子彈與敵人碰撞"""

for b in bullets:

bang(b)

def bang(b):

"""子彈與敵人碰撞檢測(cè)"""

index=-1

for x in range(0,len(flyings)):

f=flyings[x]

if f.shootBy(b):

index=x

break

if index!=-1:

one=flyings[index]

if isinstance(one,Enemy):

global score

score+=one.getScore() # 獲得分?jǐn)?shù)

flyings.remove(one) # 刪除

if isinstance(one,Award):

type=one.getType()

if type==Award.DOUBLE_FIRE:

hero.addDoubleFire()

else:

hero.addLife()

flyings.remove(one)

bullets.remove(b)

def checkGameOverAction():

if isGameOver():

global state

state=GAMEOVER

hero.reLife()

def isGameOver():

for f in flyings:

if hero.hit(f):

hero.sublife()

hero.clearDoubleFire()

flyings.remove(f)

return hero.getLife()<=0

def action():

x, y = pygame.mouse.get_pos()

blitmes() #打印飛行物

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

if event.type == pygame.MOUSEBUTTONDOWN:

flag=pygame.mouse.get_pressed()[0] #左鍵單擊事件

rflag=pygame.mouse.get_pressed()[2] #右鍵單擊事件

global state

if flag==True and (state==START or state==PAUSE):

state=RUNNING

if flag==True and state==GAMEOVER:

state=START

if rflag==True:

state=PAUSE

if state==RUNNING:

hero.move(x,y)

enterAction()

shootAction()

stepAction()

outOfBoundAction()

bangAction()

checkGameOverAction()

def main():

pygame.display.set_caption("飛機(jī)大戰(zhàn)")

while True:

screen.blit(sets.bgImage, (0, 0)) # 加載屏幕

action()

pygame.display.update() # 重新繪制屏幕

# time.sleep(0.1) # 過(guò)0.01秒執(zhí)行,減輕對(duì)cpu的壓力

if name == \'main\':

main()

```寫(xiě)這個(gè)主要是練習(xí)一下python,把基礎(chǔ)打?qū)嵭?。pygame的文本書(shū)寫(xiě)是沒(méi)有換行,得在新建一個(gè)對(duì)象去寫(xiě),為了簡(jiǎn)單我把文本都書(shū)寫(xiě)在了一行。

以上就是關(guān)于飛機(jī)可以帶pos機(jī),python飛機(jī)大戰(zhàn)源代碼的知識(shí),后面我們會(huì)繼續(xù)為大家整理關(guān)于飛機(jī)可以帶pos機(jī)的知識(shí),希望能夠幫助到大家!

轉(zhuǎn)發(fā)請(qǐng)帶上網(wǎng)址:http://www.shbwcl.net/newsone/90293.html

你可能會(huì)喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 babsan@163.com 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。