小学生python游戏开发pygame--类及框架
共同设置参数游戏精灵类
游戏类代码实现及升级
升级用的player类
总结
源码获取
共同设置参数
共同设置,放在一个文件中:
# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/10/27 17:39
# 文件名称: setconf.py
# 开发工具: PyCharm
WIDTH = 600
HEIGHT = 800
TITLE = '我的游戏'
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LENGHUI = (128,138,135)
FPS = 60
# 加速度,摩擦力,重力
PLAYER_JIASUDU = 0.5
PLAYER_MUOCALI = -0.12
PLAYER_ZHONGLI = 0.8
# 平台列表
PLATEFORM_LIST =[(0,HEIGHT-30,WIDTH,30),(WIDTH/3,HEIGHT-280,WIDTH/5,20),
(50,200,180,20),(350,320,280,20),(450,100,80,20),(0,120,380,20)]
# 游戏图片
SPRITSHEET = 'renwu.png'
游戏精灵类
玩家1
import pygame
from leigame.shezhi import *
import pygame
from pygame.locals import *
# 类中的X、Y、position用于设置精灵的位置
class Player(pygame.sprite.Sprite):
def __init__(self):
# 调用父类的初始化方法
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20,20))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = WIDTH/2,HEIGHT/2
def update(self):
# 帧变动
self.rect.x +=5
if self.rect.left>WIDTH:
self.rect.left=0
玩家升级2
import pygame
from leigame.shezhi import *
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
def __init__(self):
# 调用父类的初始化方法
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20,20))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = WIDTH/2,HEIGHT/2
def update(self):
# 帧变动
keys = pygame.key.get_pressed()
if keys:
self.rect.left -=5
if keys:
self.rect.left +=5
if self.rect.left>WIDTH:
self.rect.left=0
游戏类代码实现及升级
游戏主类效果
import pygame as pg
from leigame.shezhi import *
class Game():
def __init__(self):
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.font = pg.font.Font('fonts\\simhei.ttf', 30)
self.clock = pg.time.Clock()
# 设置游戏名
pg.display.set_caption(TITLE)
def new(self):
self.run()
def update(self):
pass
def draw(self):
pass
def events(self):
for event in pg.event.get():
# 判断是否退出(点×)
if event.type == pg.QUIT:
# 退出函数
exit()
def show_start_screen(self):
pass
def show_go_screen(self):
pass
def run(self):
self.running=True
while self.running:
# self.clock.tick(FPS)
self.events()
self.update()
self.draw()
g = Game()
g.show_start_screen()
g.new()
g.show_go_screen()
pg.quit()
升级后的
# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/10/30 11:48
# 文件名称: gameclass.py
# 开发工具: PyCharm
import pygame as pg
from leigame.shezhi import *
from leigame.player import *
import random
from os import path
from leigame.pybutton import *
class Game():
def __init__(self):
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
self.running = True
pg.display.set_caption(TITLE)# 设置游戏名
def load_data(self):# 17:51计
self.dir = path.dirname(__file__)
img_dir = path.join(self.dir,'images')
self.spritesheet = Spritesheet(path.join(img_dir,SPRITSHEET))
def new(self):
self.score = 0
self.all_sprites = pg.sprite.Group()
self.load_data()
self.wanjia = Player(self)
self.all_sprites.add(self.wanjia)
self.all_plateforms = pg.sprite.Group()
for plat in PLATEFORM_LIST:
p = Plateform(*plat)
self.all_plateforms.add(p)
self.all_sprites.add(p)
self.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
self.all_sprites.update()
hits = pg.sprite.spritecollide(self.wanjia,self.all_plateforms,False)
if hits:
self.wanjia.pos.y = hits.rect.top
self.wanjia.vel.y = 0
# 向上移动的相机
if self.wanjia.rect.top <= HEIGHT/4:
self.wanjia.pos.y += abs(self.wanjia.vel.y)
for plat in self.all_plateforms:
plat.rect.y += abs(self.wanjia.vel.y)
if plat.rect.top>=HEIGHT:
plat.kill()
self.score += 10
# 游戏结束
if self.wanjia.rect.bottom > HEIGHT:
for sp in self.all_sprites:
sp.rect.y-= max(self.wanjia.vel.y,10)
self.playing = False
# 平台随机
while len(self.all_plateforms)<=6:
wd = random.randrange(55,280)
p = Plateform(random.randrange(0,WIDTH-wd),random.randrange(-170,-80),
wd,20)
self.all_plateforms.add(p)
self.all_sprites.add(p)
def draw(self):
self.screen.fill(LENGHUI)
self.all_sprites.draw(self.screen)
self.draw_text(str(self.score),RED,WIDTH/2,10,40)
pg.display.update()
# pass
def events(self):
for event in pg.event.get():
# 判断是否退出(点×)
if event.type == pg.QUIT:
exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
self.wanjia.jump()
def do_click1(self,btn):
pygame.display.set_caption(&#39;点击了 %s,id为 %s&#39; % (btn._text, btn.ctl_id))
btn.text = &#39;点击过&#39;
def do_click2(self,btn):
btn.visible = False
def do_click3(self,btn):
pygame.quit()
exit()
def show_start_screen(self):
if not self.running:
return
# 开始屏
self.screen.fill(BLACK)
self.draw_text(TITLE,GREEN,WIDTH/2,HEIGHT/4,60)
self.draw_text(&#39;按在右移动,空格跳&#39;, GREEN, WIDTH / 2, HEIGHT/ 2)
self.draw_text(&#39;按任意建开始&#39;,GREEN,WIDTH/2,HEIGHT*3/4)
pg.display.flip()
self.wait_for_key()
def show_go_screen(self):
if not self.running:
return
self.screen.fill(BLACK)
self.draw_text(&#39;游戏结束&#39;, GREEN, WIDTH / 2, HEIGHT / 4, 60)
self.draw_text(&#39;分数:&#39;+str(self.score),GREEN, WIDTH / 2, HEIGHT / 2)
self.draw_text(&#39;按任意建重新开始&#39;, GREEN, WIDTH / 2, HEIGHT * 3 / 4)
pg.display.flip()
self.wait_for_key()
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
exit()
if event.type == pg.KEYUP:
waiting = False
def draw_text(self,text,color,x,y,size=30):
font = pg.font.Font(&#39;fonts\\simhei.ttf&#39;, size)
text_surface = font.render(text,True,color)
text_rect =text_surface.get_rect()
text_rect.midtop = (x,y)
self.screen.blit(text_surface,text_rect)
g = Game()
while g.running:
g.show_start_screen()
g.new()
g.show_go_screen()
pg.quit()
升级用的player类
# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/10/30 12:12
# 文件名称: player.py
# 开发工具: PyCharm
import pygame
from leigame.shezhi import *
import pygame
from pygame.locals import *
vec = pygame.math.Vector2
class Spritesheet:
def __init__(self, filename):
self.spritesheet = pygame.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.blit(self.spritesheet, (0, 0), (x, y, width, height))
# image = pygame.transform.scale(image,(width //2,height//2))# 图片缩小/ //整除
image.set_colorkey(WHITE)
return image
# 类中的X、Y、position用于设置精灵的位置
class Player(pygame.sprite.Sprite):
def __init__(self,game):
# 调用父类的初始化方法
pygame.sprite.Sprite.__init__(self)
self.game=game
# self.image = pygame.Surface((30,30))
# self.image.fill(RED)
self.image = self.game.spritesheet.get_image(168,67,58,70)
# self.image.
# self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.center = WIDTH/2,HEIGHT/2
self.pos = vec(WIDTH/2,HEIGHT/2) #精灵位置
self.vel = vec(0,0)# 速度
self.acc = vec(0,0)# 加速度
def update(self):
# 帧变动
self.acc =vec(0, PLAYER_ZHONGLI) # 加速度,y可以模仿重力
keys = pygame.key.get_pressed()
if keys:
self.acc.x -= PLAYER_JIASUDU
self.image = pygame.transform.flip(self.game.spritesheet.get_image(168, 67, 58, 70), flip_x=True,
flip_y=False)
if keys:
self.acc.x +=PLAYER_JIASUDU
self.image = self.game.spritesheet.get_image(168, 67, 58, 70)
self.acc.x +=self.vel.x*PLAYER_MUOCALI # 摩擦力
self.vel += self.acc
self.pos +=self.vel + 0.5*self.acc
font = pygame.font.Font(&#39;fonts\\simhei.ttf&#39;, 28)
text_surface = font.render(&#39;玩家&#39;,True,RED)
text_rect = text_surface.get_rect()
text_rect.midtop = self.pos
if self.pos.x >WIDTH:
self.pos.x=0
if self.pos.x <0:
self.pos.x=WIDTH
self.rect.midbottom = self.pos
def jump(self):
# 只有站在平台上才能跳
self.rect.y += 1
hits = pygame.sprite.spritecollide(self, self.game.all_plateforms,False)
self.rect.y -=1
if hits:
self.vel.y = -20
self.image = self.game.spritesheet.get_image(70, 35, 40, 60)
# self.image.set_colorkey(WHITE)
else:
self.image = self.game.spritesheet.get_image(168,67,58,70)
# self.image.set_colorkey(WHITE)
class Plateform(pygame.sprite.Sprite):
def __init__(self,x,y,w,h):
# 调用父类的初始化方法
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((w,h))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
总结
通过此次的pygame游戏类的整理,对相关知识有了进一步的了解与认识。
源码获取
可关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主
今天是以此模板持续更新此育儿专栏的第 10 /50次。
可以关注我,点赞我、评论我、收藏我啦。
页:
[1]