未加星标

gamingdirectional: Create the about scene for pygame project

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In this article we are going to create an about scene which will introduce the game, provides game instructions as well as gives credit to the game creator. The about page will also have a back button which will lead the player back to the main home page.

We will create this about scene under the start scene class and we will also change the home page buttons to three, which is the play button, about button and the score button, we will leave the score button alone now and come back later to create the score scene. For now we will only deal with the about scene.

First let us edit the start scene class by including in more buttons.

from BgSprite import BgSprite
from GameSprite import GameSprite
from pygame.locals import *
from pygame import math as mt
import pygame
class StartScene(object):
def __init__(self, scene):
self.scene = scene
self.play_button = 'Asset/play.png'
self.about_button = 'Asset/about.png'
self.score_button = 'Asset/score.png'
self.home_button = 'Asset/back.png'
self.button_image = 'Asset/button_play.png'
self.bg_image = 'Asset/start.png'
self.over_image = 'Asset/ove.png'
self.next_image = 'Asset/next.png'
self.win_image = 'Asset/winn.png'
self.general_image = 'Asset/general.png'
self.about_page = 'Asset/about_page.png'
self.bg_rect = Rect(0, 0, 660, 660)
self.button_rect = Rect(0, 0, 306, 112)
self.home_button_rect = Rect(0, 0, 200, 53)
self.back_button_rect = Rect(0, 0, 89, 74)
self.sprite = BgSprite(self.bg_image, self.bg_rect)
self.sprite_about = BgSprite(self.about_page, self.bg_rect)
self.sprite_win = BgSprite(self.win_image, self.bg_rect)
self.sprite_over = BgSprite(self.over_image, self.bg_rect)
self.sprite_next = BgSprite(self.next_image, self.bg_rect)
self.sprite_button = GameSprite(self.button_image, self.button_rect)
self.sprite_play_button = GameSprite(self.play_button, self.home_button_rect)
self.sprite_about_button = GameSprite(self.about_button, self.home_button_rect)
self.sprite_score_button = GameSprite(self.score_button, self.home_button_rect)
self.sprite_home_button = GameSprite(self.home_button, self.back_button_rect)
self.about_surface = self.sprite_about.getImage() # get the about sprite surface
self.win_surface = self.sprite_win.getImage() # get the win sprite surface
self.next_surface = self.sprite_next.getImage() # get the next level sprite surface
self.over_surface = self.sprite_over.getImage() # get the game over sprite surface
self.surface = self.sprite.getImage() # get the start scene sprite surface
self.button_surface = self.sprite_button.getImage() # get the button sprite surface
self.play_button_surface = self.sprite_play_button.getImage() # get the button sprite surface
self.about_button_surface = self.sprite_about_button.getImage() # get the button sprite surface
self.score_button_surface = self.sprite_score_button.getImage() # get the button sprite surface
self.home_button_surface = self.sprite_home_button.getImage() # get the button sprite surface
self.draw_pos = mt.Vector2(0, 0)
self.draw_button_pos = mt.Vector2(177, 274)
self.draw_play_button_pos = mt.Vector2(229, 200)
self.draw_about_button_pos = mt.Vector2(229, 263)
self.draw_score_button_pos = mt.Vector2(229, 328)
self.draw_back_button_pos = mt.Vector2(0, 586)
def draw(self, state):
if(state == 0):self.scene.blit(self.surface, self.draw_pos) # draw a start scene spriteself.scene.blit(self.play_button_surface, self.draw_play_button_pos) # draw a button spriteself.scene.blit(self.about_button_surface, self.draw_about_button_pos) # draw a button spriteself.scene.blit(self.score_button_surface, self.draw_score_button_pos) # draw a button sprite
elif(state == 2):self.scene.blit(self.over_surface, self.draw_pos) # draw a game over sprite
elif (state == 3):self.scene.blit(self.next_surface, self.draw_pos) # draw a next level sprite
elif(state == 4):self.scene.blit(self.win_surface, self.draw_pos) # draw a win sprite
elif (state == 5):self.scene.blit(self.about_surface, self.draw_pos) # draw a about spriteself.scene.blit(self.home_button_surface, self.draw_back_button_pos) # draw a button sprite
if(state == 2 or state == 3 or state == 4):self.scene.blit(self.button_surface, self.draw_button_pos) # draw a button sprite
pygame.display.flip()

Next we will edit the game manager page by including in an extra state, the about state.

from Player import Player
from Background import Background
from EnemyManager import EnemyManager
from Overlap import Overlap
from ExplosionManager import ExplosionManager
from Score import Score
from StartScene import StartScene
from pygame.locals import *
from LevelManager import LevelManager
import pygame
class GameManager(object):
def __init__(self, scene):
self.scene = scene
self.start_scene = StartScene(scene)
self.load_music()
self.play_music()
self.overlap_manager = Overlap()
self.level_manager = LevelManager(self)
self.setup(self.level_manager.get_level())
#game state
self.LOAD = 0
self.GAME = 1
self.OVER = 2
self.NEXT = 3
self.WIN = 4
self.ABOUT = 5
self.state = self.LOAD
def setup(self, game_level):
self.game_level = game_level
self.score_manager = Score(self.scene)
self.background = Background(self.scene)
self.player = Player(self.scene)
self.enemy_manager = EnemyManager(self.scene, self.player, game_level)
self.explosion_manager = ExplosionManager(self.scene)
def loop(self):
if(self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT):self.start_scene.draw(self.state)
elif(self.state == self.GAME):self.update()self.draw()
def isAreaClick(self, pos):
if (self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT):self.rect = Rect(177, 274, 306, 112) # the position of the play button on the sceneself.rect_play = Rect(229, 200, 200, 53) # the position of the play button on the home sceneself.rect_about = Rect(229, 263, 200, 53) # the position of the about button on the home sceneself.rect_back = Rect(0, 586, 89, 74) # the position of the back button on the home scenex, y = posif(self.rect.collidepoint(x, y) and (self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN)): self.state = self.GAMEelif(self.rect_play.collidepoint(x,y) and self.state == self.LOAD): self.state = self.GAMEelif (self.rect_about.collidepoint(x, y) and self.state == self.LOAD): self.state = self.ABOUTelif (self.rect_back.collidepoint(x, y) and self.state == self.ABOUT): self.state = self.LOAD
def load_music(self):
pygame.mixer_music.load('Music/winternight.ogg')
def play_music(self):
pygame.mixer_music.play(-1) #play the music infinite time
def set_player_x(self, _x):
if (self.state == self.GAME):self.player.setX(_x)
def set_player_y(self, _y):
if (self.state == self.GAME):self.player.setY(_y)
def set_missile_strike(self, strike):
if (self.state == self.GAME):self.player.setStrike(strike)
def update(self):
self.player.update()
self.enemy_manager.update()
self.isOverlap()
self.explosion_manager.explosion_update()
# check for player, enemy, missiles overlap
def isOverlap(self):
self.overlap_manager.isOverlap(self.player, self.enemy_manager, self.explosion_manager, self.score_manager, self)
def draw(self):
self.background.draw()
self.player.draw()
self.enemy_manager.draw()
self.explosion_manager.draw()
self.score_manager.draw()
pygame.display.flip()

This is what the about scene looks like…

http://gamingdirectional.com/wp-content/uploads/2018/12/game_strike-5.mp4

We will further decorate those scenes in the next chapter so stay tune.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:gamingdirectional: Create the about scene for pygame project
本站链接:https://www.codesec.net/view/620862.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(137)