首页 » 技术分享 » Python 是男人就坚持30秒

Python 是男人就坚持30秒

 


 
 闲着没事写了一个学生时代的游戏,是男人就坚持30秒,用了两天时间写好。

之后想写一个塔防游戏,目前思路没有想好。

有附件,欢迎下载。

更新一下版本,增加秒数计时器:

import pygame
from sys import exit
import GuaiWuA
import Hero
import datetime

pygame.init()   # 初始化
screen = pygame.display.set_mode((800,600)) # 定义窗体大小
pygame.display.set_caption('盛世再见')  # 游戏名称

running = True  # 游戏主循环标记
clock = pygame.time.Clock() # 定义帧数

# 顶
ding1 = pygame.draw.rect(screen,(99, 105, 20),(0, 20, 390,10))   # 绘制地图
ding2 = pygame.draw.rect(screen,(99, 105, 20),(410, 20, 390,10))
# 底
di1 = pygame.draw.rect(screen,(99, 105, 20),(0, 590, 390,10))
di2 = pygame.draw.rect(screen,(99, 105, 20),(410, 590, 390,10))
# 左
zuo1 = pygame.draw.rect(screen,(99, 105, 20),(0, 10, 10,280))
zuo2 = pygame.draw.rect(screen,(99, 105, 20),(0, 310, 10,280))
# 右
you1 = pygame.draw.rect(screen,(99, 105, 20),(790, 10, 10,280))
you2 = pygame.draw.rect(screen,(99, 105, 20),(790, 310, 10,290))
collidelist = [ding1,ding2,di1,di2,zuo1,zuo2,you1,you2]

hero = Hero.Hero(screen)    # 创建英雄
i = 0   # 碰撞一次+1        # 怪物碰撞墙体次数
index = 0                   # 怪物是否与墙体碰撞
guai = GuaiWuA.GuaiWuA(screen,0) # pygame.draw.rect(screen, (255, 255, 255), (400, 300, 10,10)) # 初始化一个怪
guaiList = [guai]   #初始化怪list
pygame.event.set_allowed([pygame.KEYDOWN, pygame.KEYUP])    # 设置监听事件
gameOver = False    # 游戏是否结束

starttime = datetime.datetime.now() # 计时
while running:
    clock.tick(60)  #设置帧数
    for event in pygame.event.get():    # 事件队列
        if event.type == pygame.QUIT:   # 退出事件
            exit();                     # 退出程序
        elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:    # 判断类型
            hero.moveHero(event)    # 移动英雄
    hero.drawHero()     # 绘制英雄至最新位置
    for gx in guaiList:     # 迭代怪物列表
        pygame.draw.rect(screen, (0, 0, 0), gx.guai)    # 覆盖前一路径的方形,将颜色设置成与背景一样
        index = gx.guai.collidelist(collidelist)    # 检测碰撞
        if index != 0:  # 大于-1代表碰撞
            i += 1      # 碰撞次数+1
        gx.setIndex(index)  # 设置怪的移动轨迹
        if (gx.flag):   # 判断怪是否超出游戏窗口
            idx = guaiList.index(gx)    # 超出游戏窗口获取序列
            guaiList.pop(idx)           # 根据序列清除超出窗口的元素
        if gx.collide(hero.hero):       # 怪物英雄碰撞检测
            i = -9                      # 设置出怪索引为-9
            gameOver = True             # 游戏结束标记
            hero.flag = False
    if gameOver:
        guaiList = []                   # 清空怪物序列
    else:
        pygame.draw.rect(screen, (0, 0, 0), (20, 0, 60, 20))
        endtime = datetime.datetime.now()
        currentSeconds = (endtime - starttime).seconds
        score_font = pygame.font.Font(None, 36)
        score_text = score_font.render(str(currentSeconds), True, (128, 128, 128))
        text_rect = score_text.get_rect()
        text_rect.topleft = [20, 0]
        screen.blit(score_text, text_rect)

    if i % 8 == 0:                      # 怪碰撞8次墙体生成一个新的怪
        guaiList.append(GuaiWuA.GuaiWuA(screen,0))  # 生成一个怪

    pygame.display.update()             # 刷新窗口

    # pygame.display.flip()

 

import pygame

class Hero():
    speed = [0, 0]
    hero = None
    screen = None
    flag = True
    def __init__(self,screen):
        self.screen = screen
        self.hero = pygame.draw.rect(screen, (255, 127, 63), (20, 20, 10, 10))

    def moveHero(self,event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.speed[1] = self.speed[1] - 3
                if self.speed[1] < -5:
                    self.speed[1] = -5
            elif event.key == pygame.K_DOWN:
                self.speed[1] = self.speed[1] + 3
                if self.speed[1] > 5:
                    self.speed[1] = 5
            elif event.key == pygame.K_LEFT:
                self.speed[0] = self.speed[0] - 3
                if self.speed[0] < -5:
                    self.speed[0] = -5
            elif event.key == pygame.K_RIGHT:
                self.speed[0] = self.speed[0] + 3
                if self.speed[0] > 5:
                    self.speed[0] = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                self.speed[1] = 0
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                self.speed[0] = 0

    def drawHero (self):
        if self.speed[0] != 0 or self.speed[1] != 0:
            pygame.draw.rect(self.screen, (0, 0, 0), self.hero)
        if self.flag:
            self.hero = self.hero.move(self.speed)
            pygame.draw.rect(self.screen, (255, 127, 63), self.hero)

 

import pygame
import random

class GuaiWuA():
    offsetX = 0
    offsetY = 0
    screen = None
    guai = None
    flag = False
    index = 0
    def __init__(self,screen,index):
        self.offsetX = random.randint(-3, 3)
        self.offsetY = random.randint(-3, 3)
        self.index = index
        self.screen = screen
        self.guai = pygame.draw.rect(self.screen, (255, 255, 255), (400, 300, 5, 5))

    def setIndex(self,index):
        self.index = index
        self.setOffet()
        self.guai = self.guai.move(self.offsetX, self.offsetY)
        xPont = self.guai.x
        yPont = self.guai.y
        if xPont > 810 or yPont > 610 or xPont < -10 or yPont < -10:
            self.flag = True
        pygame.draw.rect(self.screen, (255, 255, 255), self.guai)

    def collide(self,hero):
        gameOver = self.guai.colliderect(hero)
        return gameOver


    def setOffet(self):
        if self.index == 0:
            self.offsetX = random.randint(-3, 0)
            self.offsetY = random.randint(0, 3)
        elif self.index == 1:
            self.offsetX = random.randint(-3, 0)
            self.offsetY = random.randint(0, 3)
        elif self.index == 2:
            self.offsetX = random.randint(0, 3)
            self.offsetY = random.randint(-3, 0)
        elif self.index == 3:
            self.offsetX = random.randint(0, 3)
            self.offsetY = random.randint(-3, 0)
        elif self.index == 4:
            self.offsetX = random.randint(0, 3)
            self.offsetY = random.randint(0, 3)
        elif self.index == 5:
            self.offsetX = random.randint(0, 3)
            self.offsetY = random.randint(0, 3)
        elif self.index == 6:
            self.offsetX = random.randint(-3, 0)
            self.offsetY = random.randint(-3, 0)
        elif self.index == 7:
            self.offsetX = random.randint(-3, 0)
            self.offsetY = random.randint(-3, 0)

 以上是全部代码,第一篇代码为主逻辑,第二篇为英雄逻辑,第三篇为怪逻辑。

转载自原文链接, 如需删除请联系管理员。

原文链接:Python 是男人就坚持30秒,转载请注明来源!

2