'''
作者:ISDF
功能:孩子游戏
版本:3.0
日期:03/29/2019
'''
'''
作者:ISDF
功能:孩子游戏
版本:4.0
日期:03/27/2024
'''
import pygame
from pygame.locals import *
import sys
from itertools import cycle
import random
SCREENWIDTH=550
SCREENHEIGHT=150
FPS=45
class Marie:
def __init__(self):
self.rect = pygame.Rect(0,0,0,0)
self.jumpState= False
self.jumpHeight = 130
self.lowest_y = 140
self.jumpValue = 0
self.marieIndex=0
self.marieIndexGen=cycle([0,1,2])
self.adventure_img=(pygame.image.load("image/zyp1.png").convert_alpha(),
pygame.image.load("image/zyp1.png").convert_alpha(),
pygame.image.load("image/zyp1.png").convert_alpha(),
)
self.jump_audio=pygame.mixer.Sound('audio/zyp.mp3')
self.rect.size=self.adventure_img[0].get_size()
self.x=50
self.y=self.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 MyMap:
def __init__(self, x, y):
self.bg = pygame.image.load("image/bg.png").convert_alpha()
self.x = x
self.y = y
def map_rolling(self):
if self.x < -550:
self.x = 560
else:
self.x -= 5
def map_update(self):
SCREEN.blit(self.bg, (self.x, self.y))
class Obstacle():
score=1
move=5
obstacle_y=150
def __init__(self):
self.rect=pygame.Rect(0,0,0,0)
self.tyrannosaurus=pygame.image.load("image/konglong1.png").convert_alpha()
self.griffin=pygame.image.load("image/daguaishou.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/zyp22.mp3")
r=random.randint(0,1)
if r==0:
self.image=self.tyrannosaurus
self.move=15
self.obstacle_y=100
else:
self.image=self.griffin
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 mainGame():
score=0
over=False
global SCREEN,FPSCOLOCK
pygame.init()
FPSCOLOCK=pygame.time.Clock()
SCREEN=pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT))
addobstacleTimer=0
list=[]
bg1=MyMap(0,0)
bg2=MyMap(560,0)
marie=Marie()
while True:
for event in pygame.event.get():
if over == False:
bg1.map_update()
bg1.map_rolling()
marie.move()
marie.draw_marie()
if addobstacleTimer >= 1300:
r = random.randint(0, 100)
if r > 40:
obstacle = Obstacle()
list.append(obstacle)
addobstacleTimer = 0
for i in range(len(list)):
list[i].obstacle_move()
list[i].draw_obstacle()
pygame.display.update()
# pygame.event.get()
bg2.map_update()
bg2.map_rolling()
marie.move()
marie.draw_marie()
if addobstacleTimer >= 1300:
r = random.randint(0, 100)
if r > 40:
obstacle = Obstacle()
list.append(obstacle)
addobstacleTimer = 0
for i in range(len(list)):
list[i].obstacle_move()
list[i].draw_obstacle()
pygame.display.update()
# pygame.event.get()
addobstacleTimer += 20
if event.type==KEYDOWN and event.key==K_SPACE:
pygame.display.update()
if marie.rect.y>=marie.lowest_y:
# marie.jump_audio.play()
# obstacle.score_audio.play()
marie.jump()
if event.type == QUIT:
pygame.quit()
sys.exit()
FPSCOLOCK.tick(FPS)
if __name__ == '__main__':
mainGame()
结果: