一.学习目标:
1.掌握用Python写自己的小游戏。
2.掌握面向对象编程语言的特点。
3。掌握Python基础
二.学习内容:
1.Python文件操作。
2.Python 类的定义与使用
3.Python 模块
三.效果展示:
QQ录屏20230307231804
四.代码演示:
import pygame
import random
from pygame.locals import *
# 定义坦克类
class Tank(object):
def __init__(self, screen):
self.x = 150
self.y = 540
self.screen = screen
self.bullet_list= []
self.picture_name = 'Hero2.png'
self.picture = pygame.image.load(self.picture_name)
pass
def display(self):
needdelList = []
self.screen.blit(self.picture, (self.x, self.y))
for item in self.bullet_list:
if item.judge():
needdelList.append(item)
pass
pass
for bullet in needdelList:
needdelList.remove(bullet)
pass
for bullet_d in self.bullet_list:
bullet_d.display() # 显示子弹位置
bullet_d.move() # 下次再看到为子弹的下一个位置
pass
def move_left(self):
if self.x > 0:
self.x -= 10
pass
def move_right(self):
if self.x < 270:
self.x += 10
pass
def move_up(self):
if self.y > 0:
self.y -= 10
pass
def move_down(self):
if self.y < 540:
self.y += 10
pass
def she_bullet(self):
new_bullet = Bullet(self.x,self.y,self.screen)
self.bullet_list.append(new_bullet)
pass
# 定义敌人类
class EmeyTank(object):
def __init__(self,screen):
self.x = 0
self.y = 2
self.screen = screen
self.bullet_list = []
self.picture_name = 'emey.png'
self.picture = pygame.image.load(self.picture_name)
self.direction = 'Right'
pass
def display(self):
self.screen.blit(self.picture, (self.x, self.y))
needdelList = []
self.screen.blit(self.picture, (self.x, self.y))
for item in self.bullet_list:
if item.judge():
needdelList.append(item)
pass
pass
for bullet in needdelList:
needdelList.remove(bullet)
pass
for bullet_d in self.bullet_list:
bullet_d.display() # 显示子弹位置
bullet_d.move() # 下次再看到为子弹的下一个位置
pass
pass
def she_bullet(self):
x = random.randint(1, 200)
if x == 3:
new_bullet = EmeyBullet(self.x, self.y, self.screen)
self.bullet_list.append(new_bullet)
pass
pass
def move(self):
if self.direction == 'Right':
self.x += 1
pass
elif self.direction == 'Left':
self.x -= 1
pass
if self.x > 370:
self.direction = 'Left'
pass
elif self.x < 20:
self.direction = 'Right'
pass
# 定义敌人子弹类
class EmeyBullet(object):
def __init__(self, x, y, screen):
self.x = x
self.y = y + 10
self.screen = screen
self.picture_name = 'Hero2bullet.png'
self.picture = pygame.image.load(self.picture_name)
pass
def display(self):
self.screen.blit(self.picture, (self.x, self.y))
pass
def move(self):
self.y += 2
pass
def judge(self):
if self.y > 600:
return True
else:
return False
pass
# 定义子弹类
class Bullet(object):
def __init__(self, x, y, screen):
self.x = x + 13
self.y = y - 20
self.screen = screen
self.picture_name = 'HeroBlut.png'
self.picture = pygame.image.load(self.picture_name)
pass
def display(self):
self.screen.blit(self.picture, (self.x, self.y))
pass
def move(self):
self.y -= 1
pass
def judge(self):
if self.y < 0:
return True
else:
return False
pass
# 控制键盘移动
def key_move(hero1):
event_list = pygame.event.get()
for item in event_list:
if item.type == QUIT:
print('退出')
exit()
pass
elif item.type == KEYDOWN:
if item.key == K_LEFT or item.type == K_a:
print('左移')
hero1.move_left()
pass
elif item.key == K_RIGHT or item.key == K_d:
print('右移')
hero1.move_right()
pass
elif item.key == K_DOWN:
print('下移')
hero1.move_down()
pass
elif item.key == K_UP:
print('上移')
hero1.move_up()
elif item.key == K_0:
print('发射子弹')
hero1.she_bullet()
# 设置声音
def mixer_music():
pygame.mixer.init()
game_music_name = 'Plane1.wav'
pygame.mixer.music.load(game_music_name)
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
def main():
# 设置背景
screen = pygame.display.set_mode((400, 600), 0, 32)
hero1 = Tank(screen=screen)
emey1 = EmeyTank(screen)
background = pygame.image.load('Onely.jpg')
pygame.display.set_caption("Onely44Plane")
mixer_music()
while True:
screen.blit(background, (0, 0))
hero1.display()
emey1.display()
emey1.move()
emey1.she_bullet()
key_move(hero1)
pygame.display.update()
if __name__ == '__main__':
main()