一 ,学习目标
1, 掌握简单的python类的使用
2, 熟练pygame模块的使用
3 , 体会面向对象编程特点
二 , 学习要求
1 , 具备python基础语法知识
2 , 掌握python模块的导入方式
3 , 触类旁通用python写其他小游戏
三 , 效果展示
12QQ录屏20230604225243
四 , 代码演示
import random
import math
import pygame
from pygame.locals import *
User_x,User_y,User_Stepx, User_Stepy = 600, 500, 0, 0
score = 0
# font = pygame.font.Font('freesansbold', 32)
# 创建敌人类
class enemy():
def __init__(self):
self.x = random.randint(10, 1100)
self.y = random.randint(0, 300)
self.step = 1
self.pic = pygame.image.load('D:\CAdvace\PythonGame\emey1.png')
pass
def reset(self):
self.x = random.randint(10, 1100)
self.y = random.randint(0, 200)
pass
pass
enemies = []
bullets = []
for item in range(1,6):
enemies.append(enemy())
pass
# 显示分数
def show_score(screen):
global font
text = f'score : {score}'
font_render = font.render(text, True, (0, 225, 0))
screen.blit(font_render, (10, 10))
# 创建子弹类
class bullet():
def __init__(self):
self.x = User_x + 30
self.y = User_y
self.pic = pygame.image.load('D:\CAdvace\PythonGame\selfbullet.png')
self.step = random.randint(1,4)
pass
def hit(self):
global score
for e in enemies:
if distance(self.x, self.y, e.x, e.y) < 30:
bullets.remove(self)
hit_sound.play()
score += 1000
print(f'{score}')
e.reset()
pass
# 显示子弹
def show_bullet(screen):
for b in bullets:
screen.blit(b.pic, (b.x, b.y))
b.hit()
b.y -= b.step
if b.y < 0:
bullets.remove(b)
pass
pass
pass
# 计算欧式距离
def distance(b1_x,b1_y,e_x,e_y):
distancex = b1_x - e_x
distancey = b1_y - e_y
d = math.sqrt(distancex*distancex + distancey*distancey)
return d
# 判断键盘事件
def key_event():
global User_Stepx
global User_Stepy
for key_item in pygame.event.get():
if key_item.type == QUIT:
exit()
if key_item.type == KEYDOWN:
if key_item.key == K_LEFT:
User_Stepx = -3
pass
elif key_item.key == K_RIGHT:
User_Stepx = 3
pass
elif key_item.key == K_SPACE:
print('fire a bullet !')
bullets.append(bullet())
pass
elif key_item.key == K_DOWN:
User_Stepy = 3
pass
elif key_item.key == K_UP:
User_Stepy = -3
pass
if key_item.type == KEYUP:
User_Stepx = 0
User_Stepy = 0
pass
pass
# 移动玩家(判断出界)
def move_player():
global User_x, User_y, User_Stepx, User_Stepy
User_x += User_Stepx
if User_x > 1100:
User_x =1100
pass
if User_x < 10:
User_x = 10
pass
User_y += User_Stepy
if User_y < 300:
User_y = 300
pass
if User_y > 500:
User_y = 500
pass
# 设置游戏音效
def game_music():
global hit_sound
pygame.mixer.init()
pygame.mixer.music.load('d:\CAdvace\PythonGame\game_voice.wav')
pygame.mixer.music.set_volume(0.5) # 设置游戏音大小(0-1)
pygame.mixer.music.play(-1)
hit_sound = pygame.mixer.Sound('D:\CAdvace\PythonGame\hit.mp3')
pass
# 设置射中音效
# 显示敌人
def show_enemy(screen):
for e in enemies:
screen.blit(e.pic, (e.x, e.y))
e.x += e.step
if(e.x > 1100 or e.x < 0):
e.step *= -1
e.y += 30
pass
pass
# 移动敌人
# 设置主函数
def main():
global User_Stepx, User_Stepy
screen = pygame.display.set_mode((1200, 700), 0 , 32)
pygame.display.set_caption('Onely44')
icon = pygame.image.load('d:\CAdvace\PythonGame\game_icon.png')
pygame.display.set_icon(icon)
background = pygame.image.load('d:\CAdvace\PythonGame\game_background.jpg')
UserSpaceship = pygame.image.load('D:\CAdvace\PythonGame\SpaceShip.png')
game_music()
while True:
screen.blit(background,(0,0))
screen.blit(UserSpaceship,(User_x, User_y))
show_bullet(screen=screen)
# show_score(screen=screen)
show_enemy(screen=screen)
key_event()
move_player()
pygame.display.update()
pass
if __name__ == '__main__':
main()
pass