Python中使用Pygame模块写入门级小游戏

Python中使用Pygame模块写入门级小游戏

这是刚开始学习python时候,使用pygame写的一个低端的飞机游戏,后来网上的某某热狗很火(你懂的),就把资源换成了吃热狗和发射热狗。功能没做完,比较懒,后面其实应该加一个敌机爆炸的效果和hot dog数增加的效果会更好。其实就是当发射的热狗碰到敌机的区域坐标的时候就更换一个图片达到效果同时把hot dog加一就行

pygame

一、Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。
二、安装pygame
1、pygame的官方下载地址:http://www.pygame.org/download.shtml,下载一个.whl文件
2、下载安装pip,官方地址:https://pypi.python.org/pypi/pip#downloads
3、cmd后cd到pygame的下载目录下,输入,具体的看自己下载的版本
pip install pygame-1.9.3-cp36-cp36m-win32.whl
4、就可以了
5、或者直接就在pycharm里面添加包最方便
file-setting-添加搜索package,搜索pygame模块安装就行了
三、热狗大战
发射热狗效果图
Python中使用Pygame模块写入门级小游戏_第1张图片
四、代码

import pygame
import time
from pygame.locals import *
import random

class Base(object):  #基类
    def __init__(self,screen_temp,x,y,image_name):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.Plane = pygame.image.load(image_name)
class BasePlane(Base):
    def __init__(self,screen_temp,x,y,image_name):
        Base.__init__(self,screen_temp,x,y,image_name)
        self.bullet_list = []
    def display(self):
        self.screen.blit(self.Plane, (self.x, self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge():
                self.bullet_list.remove(bullet)



class MyPlane(BasePlane):  #面向对象封装类
    def __init__(self,screen_temp):
        BasePlane.__init__(self,screen_temp,100,550,r'D:\Game_FeiJi\images\myplane.jpg') #自己的飞机
    def move_left(self):
        self.x-=5
    def move_right(self):
        self.x+=5
    def move_up(self):
        self.y-=5
    def move_down(self):
        self.y+=5
    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))

class EnemyPlane(BasePlane):  #敌机
    def __init__(self,screen_temp):
        BasePlane.__init__(self, screen_temp, 0, 0, r'D:\Game_FeiJi\images\enemy.jpg')
        self.direction = "right" #用来存储默认的显示方向
    def move(self):
        if self.direction == "right":
            self.x+=3
        elif self.direction == "left":
            self.x-=3
        if self.x>267:
            self.direction = "left"
        elif self.x<0:
            self.direction = "right"
    def fire(self):
        random_num = random.randint(1,100)
        if random_num == 2 or random_num ==20:
            self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))

class BaseBullet(Base):
    def display(self):
        self.screen.blit(self.Plane,(self.x, self.y))

class Bullet(BaseBullet):
    def __init__(self,screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp,x+15,y-12,r'D:\Game_FeiJi\images\bullet.jpg')
    def move(self):
        self.y-=5
    def judge(self):
        if self.y<0:
            return True
        else:
            return False

class EnemyBullet(BaseBullet):
    def __init__(self,screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp, x + 13, y+45, r'D:\Game_FeiJi\images\enemybullet.jpg')
    def move(self):
        self.y+=5
    def judge(self):
        if self.y>620:
            return True
        else:
            return False

def key_control(myplane_temp):
        for event in pygame.event.get():
            #判断是否是点击了退出按钮
            if event.type == QUIT:
                print("exit")
                exit()
            #判断是否是按下了键
            elif event.type == KEYDOWN:
                #检测按键是否是a或者left
                if event.key == K_a or event.key == K_LEFT:
                    print('left')
                    myplane_temp.move_left()
                #检测按键是否是d或者right
                elif event.key == K_d or event.key == K_RIGHT:
                    print('right')
                    myplane_temp.move_right()
                #检测按键是否是w或者up
                if event.key == K_w or event.key == K_UP:
                    print('up')
                    myplane_temp.move_up()
                #检测按键是否是s或者down
                if event.key == K_s or event.key == K_DOWN:
                    print('down')
                    myplane_temp.move_down()
                #检测按键是否是空格键
                elif event.key == K_SPACE:
                    print('space')
                    myplane_temp.fire()

def main():
    screen = pygame.display.set_mode((300,600),0,32)
    pygame.display.set_caption("Test_Game")
    background = pygame.image .load(r'D:\Game_FeiJi\images\background.jpg')
    myplane = MyPlane(screen) #创建一个人物对象
    enemy = EnemyPlane(screen)#创建一个敌人对象
    pygame.init()
    #pygame.key.set_repeat(True)
    myfont = pygame.font.Font(None, 20)
    textImage = myfont.render("Hot Dog:", True, [0, 0, 0])

    while True:
        screen.blit(background,(0,0))
        myplane.display()
        enemy.display()
        enemy.move()
        #enemy.fire()
        pygame.display.update()
        key_control(myplane)
        screen.blit(textImage, (10, 55))
        pygame.display.update()
        time.sleep(0.01)   #延时 每0.01s循环一次
        # 检测我方子弹是否击中敌方飞机


if __name__ == "__main__":
    main()

你可能感兴趣的:(Python)