Python版pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式
基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式
按空格进入单人模式,按‘t’进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳。
完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码
import pygame
import random
from pygame.locals import *
import sys
import os
FPS = 30
from itertools import cycle
class MyMap():
def __init__(self, x, y,path):
self.bg = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
def map_rolling(self,point):
if self.x < point:
self.x = -(point) 1
else:
self.x -= 3
def map_update(self):
SCREEN.blit(self.bg, (self.x, self.y))
class Stop_Button():
is_start = True
def __init__(self):
self.start_img = pygame.image.load('image/stopbutton.png').convert_alpha()
#self.stop_img = pygame.image.load('image/stop.png').convert_alpha()
def is_select(self):
point_x, point_y = pygame.mouse.get_pos()
w, h = self.start_img.get_size()
in_x = point_x > 60 and point_x < 60 w
in_y = point_y > 20 and point_y < 20 h
return in_x and in_y
class Music_Button():
is_open = True
def __init__(self):
self.open_img = pygame.image.load('image/btn_open.png').convert_alpha()
self.close_img = pygame.image.load('image/btn_close.png').convert_alpha()
self.bg_music = pygame.mixer.Sound('audio/bg_music.wav')
def is_select(self):
point_x, point_y = pygame.mouse.get_pos()
w, h = self.open_img.get_size()
in_x = point_x > 20 and point_x < 20 w
in_y = point_y > 20 and point_y < 20 h
return in_x and in_y
class Marie():
def __init__(self,lowest_y):
# 初始化小玛丽矩形
self.rect = pygame.Rect(0, 0, 0, 0)
self.jumpState = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.marieIndexGen = cycle([0, 1, 2])
# 加载小玛丽图片
self.adventure_img = (
pygame.image.load("image/adventure1.png").convert_alpha(),
pygame.image.load("image/adventure2.png").convert_alpha(),
pygame.image.load("image/adventure3.png").convert_alpha(),
)
self.jump_audio = pygame.mixer.Sound('audio/jump.wav')
self.rect.size = self.adventure_img[0].get_size()
self.x = 50
self.y = lowest_y
self.rect.topleft = (self.x, self.y)
self.mrect = self.adventure_img[0].get_rect()
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y = self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def draw_marie(self):
marieIndex = next(self.marieIndexGen)
SCREEN.blit(self.adventure_img[marieIndex],
(self.x, self.rect.y))
class Marie2():
def __init__(self,lowest_y):
self.rect = pygame.Rect(0, 0, 0, 0)
self.jumpState = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.marieIndexGen = cycle([0, 1, 2, 3])
self.adventure_img = (
pygame.image.load("image/bros1.png").convert_alpha(),
pygame.image.load("image/bros3.png").convert_alpha(),
pygame.image.load("image/bros3.png").convert_alpha(),
pygame.image.load("image/bros4.png").convert_alpha(),
)
self.jump_audio = pygame.mixer.Sound('audio/jump.wav')
self.rect.size = self.adventure_img[0].get_size()
self.x = 80;
self.y = lowest_y;
self.rect.topleft = (self.x, self.y)
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y = self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def draw_marie(self):
marieIndex = next(self.marieIndexGen)
SCREEN.blit(self.adventure_img[marieIndex],
(self.x, self.rect.y))
class Marie3():
def __init__(self,lowest_y):
self.rect = pygame.Rect(0, 0, 0, 0)
self.jumpState = False
self.jumpHeight = 130
self.lowest_y = lowest_y
self.jumpValue = 0
self.marieIndex = 0
self.marieIndexGen = cycle([0, 1, 2, 3])
self.adventure_img = (
pygame.image.load("image/p1.png").convert_alpha(),
pygame.image.load("image/p2.png").convert_alpha(),
pygame.image.load("image/p3.png").convert_alpha(),
pygame.image.load("image/p4.png").convert_alpha(),
)
self.jump_audio = pygame.mixer.Sound('audio/jump.wav')
self.rect.size = self.adventure_img[0].get_size()
self.x = 80;
self.y = lowest_y;
self.rect.topleft = (self.x, self.y)
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -8
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 8
self.rect.y = self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def draw_marie(self):
marieIndex = next(self.marieIndexGen)
SCREEN.blit(self.adventure_img[marieIndex],
(self.x, self.rect.y))
class Marie4():
def __init__(self,lowest_y):
self.jumpState = False
self.runState = False
self.image = (pygame.image.load("image/adventure1.png").convert_alpha(),
pygame.image.load("image/adventure2.png").convert_alpha(),
pygame.image.load("image/adventure3.png").convert_alpha())
self.mrect = self.image[0].get_rect()
self.jumpHeight = 130
self.mrect.y = lowest_y
self.lowest_y = lowest_y
self.jumpValue = 0
self.jump_audio = pygame.mixer.Sound('audio/jump.wav')
self.index = 0
self.list = cycle([0,1,2])
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.mrect.y >= self.lowest_y:
self.jumpValue = -8
if self.mrect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 8
self.mrect.y = self.jumpValue
if self.mrect.y >= self.lowest_y:
self.jumpState = False
def draw_marie(self):
index = next(self.list)
SCREEN.blit(self.image[index],self.mrect)
class Bullet():
def __init__(self):
self.x = 80
self.y = 150
self.bullet_img = pygame.image.load("image/s.png")
self.brect = self.bullet_img.get_rect()
def move(self,x,y):
self.brect.x = x
self.brect.y = y
def draw_Bullet(self):
SCREEN.blit(self.bullet_img,self.brect)
class Obstacle():
score = 1
move = 3
obstacle_y = 150
def __init__(self):
self.rect = pygame.Rect(0, 0, 0, 0)
self.missile = pygame.image.load("image/missile.png").convert_alpha()
self.pipe = pygame.image.load("image/pipe.png").convert_alpha()
self.numbers = (pygame.image.load('image/0.png').convert_alpha(),
pygame.image.load('image/1.png').convert_alpha(),
pygame.image.load('image/2.png').convert_alpha(),
pygame.image.load('image/3.png').convert_alpha(),
pygame.image.load('image/4.png').convert_alpha(),
pygame.image.load('image/5.png').convert_alpha(),
pygame.image.load('image/6.png').convert_alpha(),
pygame.image.load('image/7.png').convert_alpha(),
pygame.image.load('image/8.png').convert_alpha(),
pygame.image.load('image/9.png').convert_alpha())
self.score_audio = pygame.mixer.Sound('audio/score.wav')
r = random.randint(0, 1)
if r == 0:
self.image = self.missile
self.move = 15
self.obstacle_y = 100
else:
self.image = self.pipe
self.rect.size = self.image.get_size()
self.width, self.height = self.rect.size
self.x = 800
self.y = self.obstacle_y
self.rect.center = (self.x, self.y)
def obstacle_move(self):
self.rect.x -= self.move
def draw_obstacle(self):
SCREEN.blit(self.image, (self.rect.x, self.rect.y))
def getScore(self):
self.score
tmp = self.score;
if tmp == 1:
self.score_audio.play()
self.score = 0;
return tmp;
def showScore(self, score,SCREENWIDTH,SCREENHEIGHT):
self.scoreDigits = [int(x) for x in list(str(score))]
totalWidth = 0 # 要显示的所有数字的总宽度
for digit in self.scoreDigits:
# 获取积分图片的宽度
totalWidth = self.numbers[digit].get_width()
# 分数横向位置
Xoffset = (SCREENWIDTH - (totalWidth 30))
for digit in self.scoreDigits:
# 绘制分数
SCREEN.blit(self.numbers[digit], (Xoffset, SCREENHEIGHT * 0.1))
# 随着数字增加改变位置
Xoffset = self.numbers[digit].get_width()
完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhggcjah
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13