用户:
蓦然回首7wkR查看:4 回复:3 评论:4 创建时间:2022-08-10T17:27:37
【作品展示】

【作品介绍】
重新发布下
【作品源代码】
#!/usr/bin/env python3
# -*-coding:utf-8-*-
import pygame
import easygui
import sys
import random
class Constant:
SCREEN_HEIGHT = 700 #设置舞台的高为700
SCREEN_WIGHT = 700 #设置舞台的宽为700
BLACK = (0, 0, 0) #设置黑色
RED = (255, 0, 0) #设置红色
SCORE = 0 #初始化分数为0
SPEED = 1 #初始化敌人的速度为1
DRIVER = False #初始化DRIVER成就为false
ZOMBIE = False #初始化ZOMBIE成就为false
BLIND_PERSON = False #初始化BLIND_PERSON成就为false
BOLD = False #初始化BOLD成就为false
CHESHEN = False #初始化CHESHEN为false
TUI = True
TUICHU = 0
BEGIN = 0
#定义敌人类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Enemy.png") #导入图片
self.surf = pygame.Surface((60, 96))
self.rect = self.surf.get_rect(top=0, left=random.randint(0, 624))
def move(self):
self.rect.move_ip(0, Constant.SPEED)
#如果敌人超出了屏幕的高,则回到y=0
if self.rect.top >= Constant.SCREEN_HEIGHT:
self.rect.top = 0
self.rect.left = random.randint(0, 624) #将敌人的x坐标,在舞台内随机出现
Constant.SCORE += 1 #分数加一
#定义玩家类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Player.png") #导入玩家角色
self.surf = pygame.Surface((20, 83))
self.rect = self.surf.get_rect(top=601, left=332) #初始化玩家的位置
def move(self):
pressed_key = pygame.key.get_pressed()
if pressed_key[pygame.K_w] and self.rect.top >= 0: #如果按下w,并且不超出边界,则往上走
self.rect.move_ip(0, -5)
if pressed_key[pygame.K_s] and self.rect.bottom <= Constant.SCREEN_HEIGHT: #如果按下s,并且不超出边界,则往下走
Constant.TUI = False
self.rect.move_ip(0, 5)
if pressed_key[pygame.K_a] and self.rect.left >= 0: #如果按下a,并且不超出边界,则往左走
self.rect.move_ip(-5, 0)
if pressed_key[pygame.K_d] and self.rect.right <= 680: #如果按下d,并且不超出边界,则往右走
self.rect.move_ip(5, 0)
if pressed_key[pygame.K_f]:
pygame.quit()
sys.exit()
#定义金币类
class Gold(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Gold.png") #导入金币图片
self.surf = pygame.Surface((48, 48))
self.rect = self.surf.get_rect(top=random.randint(0, 652), left=random.randint(0, 652))
#定义喵类
class Bomb(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Bomb.png") #导入喵图片
self.surf = pygame.Surface((96, 96))
self.rect = self.surf.get_rect(top=random.randint(0, 600), left=random.randint(0, 600))
#定义豌豆射手类
class Pea_shooter(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Pea_shooter.png")
self.surf = pygame.Surface((39, 63))
self.rect = self.surf.get_rect(top=200, left=0)
#定义豌豆类
class Pea(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Pea.png")
self.surf = pygame.Surface((37, 37))
self.rect = self.surf.get_rect(top=200, left=55)
def move(self):
self.rect.move_ip(5, 0)
if self.rect.left >= Constant.SCREEN_WIGHT:
self.rect.left = 55
self.rect.top = 200
#定义问号类
class Questions_mark(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Question_mark.png")
self.surf = pygame.Surface((53, 53))
self.rect = self.surf.get_rect(top=random.randint(0, 喵7), left=random.randint(0, 喵7))
#定义小点类
class Dian(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("小点.jpg")
self.rect = self.image.get_rect(top=0, left=0)
def move(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.rect.center = (mouseX, mouseY)
#定义游戏开始类
class Game_begin(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Game_begin.png")
self.rect = self.image.get_rect(center=(350, 450))
#定义游戏规则类
class Game_rule(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Game_rule.png")
self.rect = self.image.get_rect(center=(604, 617))
#定义游戏类
class Game:
def __init__(self):
tubiao = pygame.image.load("逆行飙车logo.png")
pygame.init() #初始化pygame模块
size = Constant.SCREEN_HEIGHT, Constant.SCREEN_WIGHT #将舞台大小设置成700,700
self.screen = pygame.display.set_mode(size) #创建舞台
pygame.display.set_caption("逆行飙车") #将游戏命名为“逆行飙车“
pygame.display.set_icon(tubiao) #将游戏logo设置为逆行飙车logo.png
big_font = pygame.font.SysFont("微软雅黑", 60) #将big_font字体设置成“微软雅黑”,字号为60
self.big_font = pygame.font.SysFont("叶根友毛笔行书2.0版 常规", 60)
self.喵all_font = pygame.font.SysFont("微软雅黑", 20) #将喵all_font字体设置成“微软雅黑”,字号为20
self.game_over = big_font.render("游戏结束", True, Constant.BLACK) #将game_over文本为“游戏结束”,不透明,颜色黑
self.driver = self.喵all_font.render("成就:老司机啦(分数极高,20分诶)", True, Constant.BLACK)
self.zombie = self.喵all_font.render("成就:我是...僵尸?!(被豌豆打中了)", True, Constant.BLACK)
self.blind_person = self.喵all_font.render("成就:车盲(分数太低啦)", True, Constant.BLACK)
self.bold = self.喵all_font.render("成就:真儿胆大(居然敢往上边儿挺)", True, Constant.BLACK)
self.cheshen = self.喵all_font.render("成就:全民车神(分数达到了惊人的40分)", True, Constant.BLACK)
self.tui = self.喵all_font.render("成就:永不后退的兵", True, Constant.BLACK)
self.clock = pygame.time.Clock() #设置clock时钟
self.FPS = 70 #将帧率设置为70
self.background = pygame.image.load("Background.jpg") #导入背景
self.background1 = pygame.image.load("Background.png")
self.SPEED_UP = pygame.USEREVENT + 1
pygame.time.set_timer(self.SPEED_UP, 1000)
self.MOVE_UP = pygame.USEREVENT + 2
pygame.time.set_timer(self.MOVE_UP, 3000)
self.MOVE1_UP = pygame.USEREVENT + 3
pygame.time.set_timer(self.MOVE1_UP, 3000)
self.MOVE2_UP = pygame.USEREVENT + 4
pygame.time.set_timer(self.MOVE2_UP, 1000)
#运行
def run(self):
player = Player() #初始化Player类
enemy = Enemy() #初始化Enemy类
gold = Gold() #初始化Gold类
喵 = Bomb() #初始化Bomb类
pea_shooter = Pea_shooter()
pea = Pea()
questions_mark = Questions_mark()
dian = Dian()
game_begin = Game_begin()
game_rule = Game_rule()
enemies_list = [enemy, pea] #定
enemies = pygame.sprite.Group() #义
for sprite in enemies_list: #4
enemies.add(sprite) #个
#精
babies_list = [gold] #灵
babies = pygame.sprite.Group() #组
for sprite in babies_list: #
babies.add(sprite) #enemies
#babies
喵s_list = [喵] #喵s
喵s = pygame.sprite.Group() #all_sprites
for sprite in 喵s_list: #
喵s.add(sprite) #
#
peas_list = [pea] #
peas = pygame.sprite.Group()
for sprite in peas_list:
peas.add(peas_list)
questions_marks_list = [questions_mark]
questions_marks = pygame.sprite.Group()
for sprite in questions_marks_list:
questions_marks.add(sprite)
game_begins_list = [game_begin]
game_begins = pygame.sprite.Group()
for sprite in game_begins_list:
game_begins.add(sprite)
game_rules_list = [game_rule]
game_rules = pygame.sprite.Group()
for sprite in game_rules_list:
game_rules.add(sprite)
#以
all_sprites_list = [player, enemy, pea] #下
all_sprites = pygame.sprite.Group() #精
for sprite in all_sprites_list: #灵
all_sprites.add(sprite) #组
pygame.mixer.Sound("Background.ogg").play(-1)
traffic_collision_sound = pygame.mixer.Sound("traffic_collision.wav")
button_sound = pygame.mixer.Sound("Button.wav")
while True:
self.screen.blit(self.background1, (0, 0))
self.screen.blit(game_begin.image, game_begin.rect)
self.screen.blit(game_rule.image, game_rule.rect)
begin = self.big_font.render("逆行飙车", True, Constant.BLACK)
self.screen.blit(begin, (220, 250))
dian.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and pygame.sprite.spritecollideany(dian, game_begins):
button_sound.play()
Constant.BEGIN = 1
break
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and pygame.sprite.spritecollideany(dian, game_rules):
button_sound.play()
easygui.msgbox("游戏规则:\n\t1.w向上,s向下,a向左,d向右,f退出,t重启\n\t2.碰到金币加2分,碰到喵扣3分,碰到问号是?\n\t3.努力躲开白色小车和豌豆,它们会使您失败\n\t4.您有可能触发成就,至于如何触发,需要您自己去探索\n\t5.分数就显示在左上角\n\t6.敌人的速度会随着时间的流逝而加快", "逆行飙车", "我知道了")
if Constant.BEGIN:
break
pygame.display.update()
self.clock.tick(self.FPS)
#游戏主循环
while True:
self.screen.blit(self.background, (0, 0)) #将background打印在坐标(0, 0)处
self.screen.blit(gold.image, gold.rect)
self.screen.blit(喵.image, 喵.rect)
self.screen.blit(pea_shooter.image, pea_shooter.rect)
self.screen.blit(questions_mark.image, questions_mark.rect)
for sprite in all_sprites:
self.screen.blit(sprite.image, sprite.rect)
sprite.move()
scores = self.喵all_font.render(str(Constant.SCORE), True, Constant.BLACK)
self.screen.blit(scores, (10, 10)) #将得分打印在坐标(10, 10)处
scores_copy = "分数是" + str(Constant.SCORE)
scores_copy_blit = self.喵all_font.render(scores_copy, True, Constant.BLACK)
if Constant.SCORE >= 20:
Constant.BLIND_PERSON = False
Constant.DRIVER = True
if pygame.sprite.spritecollideany(player, peas):
Constant.ZOMBIE = True
if Constant.SCORE <= 0 and pygame.sprite.spritecollideany(player, enemies):
Constant.DRIVER = False
Constant.CHESHEN = False
Constant.BLIND_PERSON = True
if player.rect.top <= 0:
Constant.BOLD = True
if Constant.SCORE >= 40:
Constant.CHESHEN = True
for event in pygame.event.get():
#当鼠标点击到右上角的叉叉处,清楚所有的pygame库,并且退出程序
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#当按下T键,游戏重启
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
easygui.msgbox("游戏已重启", "逆行飙车", "我知道了")
Constant.SCORE = 0
Constant.SPEED = 1
Constant.DRIVER = False #初始化DRIVER成就为false
Constant.ZOMBIE = False #初始化ZOMBIE成就为false
Constant.BLIND_PERSON = False #初始化BLIND_PERSON成就为false
Constant.BOLD = False #初始化BOLD成就为false
Constant.CHESHEN = False #初始化CHESHEN为false
Constant.TUI = True
player.rect.top = 601
player.rect.left = 332
enemy.rect.top = 0
enemy.rect.left = random.randint(0, 624)
pea.rect.top = 200
pea.rect.left = 55
#每过一秒,敌人的速度便增加0.25
if event.type == self.SPEED_UP:
Constant.SPEED += 0.25
#每过三秒,金币便更换一个位置
if event.type == self.MOVE_UP:
gold.rect.top = random.randint(0, 652)
gold.rect.left = random.randint(0, 652)
#每过三秒,喵便更换一个位置
if event.type == self.MOVE1_UP:
喵.rect.top = random.randint(0, 600)
喵.rect.left = random.randint(0, 600)
#每过0.5秒,问号便更换一个位置
if event.type == self.MOVE2_UP:
questions_mark.rect.top = random.randint(0, 630)
questions_mark.rect.left = random.randint(0, 630)
#当玩家碰到敌人
if pygame.sprite.spritecollideany(player, enemies):
traffic_collision_sound.play()
while True:
self.screen.fill(Constant.RED)
self.screen.blit(self.game_over, (240, 200))
self.screen.blit(scores_copy_blit, (210, 370))
if Constant.DRIVER == True:
self.screen.blit(self.driver, (210, 400))
if Constant.ZOMBIE == True:
self.screen.blit(self.zombie, (210, 430))
if Constant.BLIND_PERSON == True:
self.screen.blit(self.blind_person, (210, 400))
if Constant.BOLD == True:
self.screen.blit(self.bold, (210, 460))
if Constant.CHESHEN == True:
self.screen.blit(self.cheshen, (210, 490))
if Constant.TUI == True:
self.screen.blit(self.tui, (210, 520))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#当按下T键,游戏重启
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
Constant.TUICHU = 1
easygui.msgbox("游戏已重启", "逆行飙车", "我知道了")
self.screen.blit(self.background, (0, 0))
self.screen.blit(gold.image, gold.rect)
self.screen.blit(喵.image, 喵.rect)
self.screen.blit(pea_shooter.image, pea_shooter.rect)
self.screen.blit(questions_mark.image, questions_mark.rect)
for sprite in all_sprites:
self.screen.blit(sprite.image, sprite.rect)
sprite.move()
Constant.SCORE = 0
Constant.SPEED = 1
Constant.DRIVER = False #初始化DRIVER成就为false
Constant.ZOMBIE = False #初始化ZOMBIE成就为false
Constant.BLIND_PERSON = False #初始化BLIND_PERSON成就为false
Constant.BOLD = False #初始化BOLD成就为false
Constant.CHESHEN = False #初始化CHESHEN为false
Constant.TUI = False
player.rect.top = 601
player.rect.left = 332
enemy.rect.top = 0
enemy.rect.left = random.randint(0, 624)
pea.rect.top = 200
pea.rect.left = 55
break
#当按下f键,删除所有pygame库,并且退出游戏
if event.key == pygame.K_f:
pygame.quit()
sys.exit()
if Constant.TUICHU:
break
pygame.display.update()
self.clock.tick(self.FPS)
#当玩家碰到金币
if pygame.sprite.spritecollideany(player, babies):
gold.rect.top = random.randint(0, 652) #将金币的y坐标设置成0到652中的随机整数
gold.rect.left = random.randint(0, 652) #将金币的x坐标设置成0到652中的随机整数
Constant.SCORE += 2 #分数加2
#当玩家碰到了喵
if pygame.sprite.spritecollideany(player, 喵s):
喵.rect.top = random.randint(0, 600) #将喵的y坐标设置成0到600中的随机整数
喵.rect.left = random.randint(0, 600) #将喵的x坐标设置成0到600中的随机整数
Constant.SCORE -= 3 #分数减3
#当玩家碰到了问号
if pygame.sprite.spritecollideany(player, questions_marks):
player.rect.top = random.randint(0, 601)
player.rect.left = random.randint(0, 6喵)
#当敌人碰到了问号
if pygame.sprite.spritecollideany(enemy, questions_marks):
enemy.rect.top = random.randint(0, 588)
enemy.rect.left = random.randint(0, 624)
#当豌豆碰到了问号
if pygame.sprite.spritecollideany(pea, questions_marks):
pea.rect.top = random.randint(0, 659)
pea.rect.left = random.randint(0, 659)
Constant.TUICHU = 0
pygame.display.update() #刷新界面
self.clock.tick(self.FPS)
if __name__ == "__main__":
game = Game()
game.run()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn