前言
大家早好、午好、晚好吖 ❤ ~
一只快乐兔, 来到快乐山,喝了快乐泉,
又到快乐殿,吃了快乐莲,遇到快乐仙,
听了快乐言:快乐很简单,快乐在身边,快乐无极限,快乐在兔年!
新的一天当然是从游戏开始啦!
现在带大家做一款兔子游戏/Rabbit Rush ——
小兔子保卫,赶走獾(敌人)。
准备素材
(一)背景图片素材
(二)来点儿MUSIC:
代码展示
‘’‘配置文件’‘’
模块导入
import os
FPS
FPS = 100
屏幕大小
SCREENSIZE = (640, 480)
游戏图片路径
IMAGE_PATHS = {
'rabbit': os.path.join(os.getcwd(), 'resources/images/dude.png'),
'grass': os.path.join(os.getcwd(), 'resources/images/grass.png'),
'castle': os.path.join(os.getcwd(), 'resources/images/castle.png'),
'arrow': os.path.join(os.getcwd(), 'resources/images/bullet.png'),
'badguy': os.path.join(os.getcwd(), 'resources/images/badguy.png'),
'healthbar': os.path.join(os.getcwd(), 'resources/images/healthbar.png'),
'health': os.path.join(os.getcwd(), 'resources/images/health.png'),
'gameover': os.path.join(os.getcwd(), 'resources/images/gameover.png'),
'youwin': os.path.join(os.getcwd(), 'resources/images/youwin.png')
}
游戏声音路径
SOUNDS_PATHS = {
'hit': os.path.join(os.getcwd(), 'resources/audio/explode.wav'),
'enemy': os.path.join(os.getcwd(), 'resources/audio/enemy.wav'),
'shoot': os.path.join(os.getcwd(), 'resources/audio/shoot.wav'),
'moonlight': os.path.join(os.getcwd(), 'resources/audio/moonlight.wav')
}
‘’‘初始化’‘’
from .interfaces import showEndGameInterface
from .Sprites import BadguySprite, ArrowSprite, BunnySprite
‘’’
Function:
定义游戏开始结束等界面
‘’’
模块导入
import sys
import pygame
‘’‘游戏结束界面’‘’
def showEndGameInterface(screen, exitcode, accuracy, game_images):
font = pygame.font.Font(None, 24)
text = font.render(f"Accuracy: {accuracy}%", True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
while True:
screen.fill(0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if exitcode:
screen.blit(game_images['youwin'], (0, 0))
else:
screen.blit(game_images['gameover'], (0, 0))
screen.blit(text, text_rect)
pygame.display.flip()
‘’’
Function:
定义游戏精灵类
‘’’
‘’‘模块导入’‘’
import math
import pygame
‘’‘定义兔子类’‘’
class BunnySprite(pygame.sprite.Sprite):
def __init__(self, image, position, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 5
self.rotated_position = position
'''移动兔子'''
def move(self, screensize, direction):
if direction == 'left':
self.rect.left = max(self.rect.left-self.speed, 0)
elif direction == 'right':
self.rect.left = min(self.rect.left+self.speed, screensize[0])
elif direction == 'up':
self.rect.top = max(self.rect.top-self.speed, 0)
elif direction == 'down':
self.rect.top = min(self.rect.top+self.speed, screensize[1])
'''画到屏幕上'''
def draw(self, screen, mouse_pos):
angle = math.atan2(mouse_pos[1]-(self.rect.top+32), mouse_pos[0]-(self.rect.left+26))
image_rotate = pygame.transform.rotate(self.image, 360-angle*57.29)
bunny_pos = (self.rect.left-image_rotate.get_rect().width/2, self.rect.top-image_rotate.get_rect().height/2)
self.rotated_position = bunny_pos
screen.blit(image_rotate, bunny_pos)
‘’‘定义弓箭类’‘’
class ArrowSprite(pygame.sprite.Sprite):
def __init__(self, image, position, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.angle = position[0]
self.image = pygame.transform.rotate(image, 360-position[0]*57.29)
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.left, self.rect.top = position[1:]
self.speed = 10
'''更新弓箭'''
def update(self, screensize):
velx = math.cos(self.angle) * self.speed
vely = math.sin(self.angle) * self.speed
self.rect.left += velx
self.rect.top += vely
if self.rect.right < 0 or self.rect.left > screensize[0] or self.rect.top > screensize[1] or self.rect.bottom < 0:
return True
return False
‘’‘定义獾类’‘’
class BadguySprite(pygame.sprite.Sprite):
def __init__(self, image, position, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.left, self.rect.top = position
self.speed = 7
'''更新獾'''
def update(self):
self.rect.left -= self.speed
if self.rect.left < 64:
return True
return False
效果展示
控制鼠标可移动射击方向
连点鼠标键可射出弓箭,按“A” “W” “D” “S” 键可控制兔子移动位置
血条无,游戏结束
小惊喜:
兔年快乐烟花:http://gujiukeji.com/love/2023/
跨年烟花:https://corestudi0.github.io/newyear/#pt_1672488003277
尾语 💝
好了,今天的分享就差不多到这里了!
完整代码、更多资源、疑惑解答直接点击下方名片自取即可。
宁外给大家推荐一个好的教程:
【48小时搞定全套教程!你和大佬只有一步之遥【python教程】
有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!