second commit

main
xu'pei'yu 6 months ago
parent 253725899d
commit 872c5aa054

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (jiemian_test)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (test1)" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/xpy_game.iml" filepath="$PROJECT_DIR$/.idea/xpy_game.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.12 (test1)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,2 +1,11 @@
# xpy_game
游戏介绍:
游戏开始时,一群外星 人出现在天空中,他们在屏幕中向下移动。玩家的任务是射杀这些外星人。
玩家将所有外星人都消灭干净后,将出现一群新的外星人,他们移动的速度更快。只要有外星人撞到了玩家的飞船或到达了屏幕底部,玩家就损失一艘飞船。
玩家损失三艘飞船后,游戏结束。
玩法介绍:
左右键移动
空格键发射子弹

@ -22,9 +22,6 @@ class Alien(Sprite):
# 存储外星人的准确位置
self.x = float(self.rect.x)
# 增加代码
self.max_hp = ai_settings.alien_max_hp # 外星人最大生命值
self.hp = ai_settings.alien_max_hp # 外星人当前生命值
def draw(self):
"""在指定位置绘制外星人"""
self.screen.blit(self.image, self.rect)
@ -40,4 +37,4 @@ class Alien(Sprite):
def update(self):
"""向右移动外星人"""
self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
self.rect.x = self.x
self.rect.x = self.x

@ -11,13 +11,16 @@ from scoreboard import Scoreboard
def run_game():
# 初始化pygame、设置和屏幕对象
pygame.init()
# 设置声音模块的初始化参数:这里设定了声音的频率、大小、通道和缓冲区。
pygame.mixer.init(frequency=22050, size=16, channels=2, buffer=512)
# 创建一个Settings类的实例ai_settings用于存储游戏的各种设置
ai_settings = Settings()
# 设置游戏窗口大小以及初始化屏幕对象
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
pygame.mixer.music.load('bgm/bgm.mp3')
# 加载并循环播放背景音乐
pygame.mixer.music.load('bgm/bgm.ogg')
pygame.mixer.music.play(-1)
pygame.image.load("images/bg.jpg").convert()
# 创建Play按钮
play_botton = Button(ai_settings, screen, "Play")
# 创造一艘飞船

Binary file not shown.

Binary file not shown.

@ -1,23 +1,16 @@
# 开发日期: 2024/4/26
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""一个对飞船发射的子弹进行管理的类"""
def __init__(self, ai_settings, screen, ship):
"""在飞船所处的位置创建一个子弹对象"""
super(Bullet, self).__init__()
self.screen = screen
# 加载子弹图像,并获取其外接矩形
# self.image = pygame.image.load('images/bullet.png')
# self.rect = self.image.get_rect()
# self.rect.centerx = ship.rect.centerx
# self.rect.top = ship.rect.top
#
# # 存储用小数表示的子弹位置
# self.y = float(self.rect.y)
#
# self.speed_factor = ai_settings.bullet_speed_factor
# 加载子弹图像,并获取其外接矩形
self.image = pygame.image.load('images/bullet.png')
self.sound = pygame.mixer.Sound('bgm/bullet.wav')
self.rect = self.image.get_rect()
@ -25,6 +18,9 @@ class Bullet(Sprite):
self.rect.top = ship.rect.top
# 存储用小数表示的子弹位置
self.y = float(self.rect.y)
"""设置子弹的伤害"""
self.damage = ai_settings.bullet_damage
# 从游戏设置对象中获取子弹的速度
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):

@ -13,8 +13,6 @@ class Button():
self.height = 50
self.button_color = (0, 255, 50)
self.text_color = (255, 255, 255)
# print(pygame.font.get_fonts())
# self.font = pygame.font.SysFont("arial", 48)
self.font = pygame.font.SysFont(None, 48)
# 创建按钮的rect对象并使其居中
self.rect = pygame.Rect(0, 0, self.width, self.height)

@ -1,24 +0,0 @@
import pygame
from pygame.examples.video import x, y
class Explosion:
def __init__(self, ai_setting, screen):
self.screen = screen
self.image = pygame.image.load('images/explosion.png')
self.rect = self.image.get_rect()
self.visible = False
self.stop_time = ai_setting.explosion_time
self.stop = ai_setting.explosion_time
def create(self):
self.visible = True
self.stop = self.stop_time
self.rect.centerx = x
self.rect.centery = y
def update(self):
self.stop -= 1
if self.stop == 0:
self.visible = False
def display(self):
if self.visible:
self.screen.blit(self.image, self.rect)

@ -42,19 +42,6 @@ def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bull
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)
# 增加代码
def text_objects(text, font):
textSurface = font.render(text, True, (0, 0, 0))
return textSurface, textSurface.get_rect()
def message_display(text, ai_settings, screen, stats, sb, play_button):
largeText = pygame.font.Font('comicsansms', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((ai_settings.screen_width / 2), (ai_settings.screen_height / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(3)
#
@ -216,14 +203,13 @@ def update_screen(ai_settings,screen, stats, sb, ship, aliens, bullets, play_but
# 在飞船和外星人后面重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
ship.blit()
aliens.draw(screen)
# 显示得分
sb.show_score()
# 如果游戏处于非活动状态就绘制Play按钮
if not stats.game_active:
play_button.draw_button()
#continue_buton.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()

@ -18,15 +18,20 @@ class GameStats():
self.score = 0
self.level = 1
def save_high_score(self):
# 以二进制写入模式打开名为"high_score.pk1"的文件
f = open("high_score.pk1", 'wb')
# 使用pickle模块将high_score的字符串表现形式写入文件
pickle.dump(str(self.high_score), f, 0)
f.close()
def load_high_score(self):
# 以二进制读取模式打开名为"high_score.pk1"的文件
f = open("high_score.pk1", 'rb')
try:
# 从文件中加载(反序列化)数据
str_high_score = pickle.load(f)
self.high_score = int(str_high_score)
except EOFError:
# 如果文件意外结束EOFError或没有文件可加载则设置 self.high_score 为 0
self.high_score = 0
finally:
f.close()

@ -1,3 +1,3 @@
V240
V0
p0
.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 833 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

@ -2,6 +2,7 @@ import pygame.font
from pygame.sprite import Group
from ship import Ship
class Scoreboard():
"""显示得分信息类"""
@ -29,7 +30,7 @@ class Scoreboard():
rounded_score = int(round(self.stats.score, -1))
score_str = "{:,}".format(rounded_score)
self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color)
#将得分放在屏幕右下角
# 将得分放在屏幕右下角
self.score_rect = self.score_image.get_rect()
self.score_rect.right = self.screen_rect.right - 20
self.score_rect.top = 20

@ -15,11 +15,17 @@ class Settings():
self.ship_limit = 2
# 子弹设置
"""子弹伤害"""
self.bullet_damage = 80
self.bullets_allowed = 5
self.bullet_speed_factor = 2
# 外星人设置
self.fleet_drop_speed = 10
"""修改"""
self.alien_max_hp = 160.0
self.fleet_drop_speed = 7
# 以什么样的速度加快游戏节奏
self.speedup_scale = 1.1
@ -36,7 +42,6 @@ class Settings():
# fleet_direction为1表示向右移动为-1表示向左移动
self.fleet_direction = 1
self.alien_points = 30
def increase_speed(self):
@ -45,5 +50,4 @@ class Settings():
self.ship_speed_factor *= self.speedup_scale
self.bullet_speed_factor *= self.speedup_scale
self.alien_speed_factor *= self.speedup_scale
self.alien_points = int(self.alien_points * self.score_scale)

@ -1,6 +1,7 @@
import pygame
from pygame.sprite import Sprite
class Ship(Sprite):
def __init__(self, ai_settings, screen):
"""初始化飞船并设置其初始位置"""
@ -26,7 +27,7 @@ class Ship(Sprite):
def update(self):
"""根据移动标志调整飞船的位置"""
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
@ -37,6 +38,6 @@ class Ship(Sprite):
"""让飞船在屏幕居中"""
self.center = self.screen_rect.centerx
def blitme(self):
def blit(self):
""""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)

@ -1,35 +1,39 @@
import tkinter as tk
import alien_invasion
from PIL import Image, ImageTk
class GameWelcomeScreen(tk.Tk):
class GameWelcomeScreen(tk.Tk):
def __init__(self):
super().__init__()
self.title("欢迎界面")
self.geometry("1300x750") # 设置窗口大小
# self.set_background_image()
# 添加标签和按钮
self.welcome_label = tk.Label(self, text="WELCOM TO \nThe Alien Invasion", font=("comicsansms", 50), foreground="lightblue")
self.welcome_label = tk.Label(self, text="WELCOM TO \nThe Alien Invasion", font=("comicsansms", 50),
foreground="lightblue")
self.welcome_label.pack(pady=50) # 使用pack布局管理器
self.start_button = tk.Button(self, text="进入游戏", font=("comicsansms", 20), command=self.start_button, background="lightblue")
self.start_button = tk.Button(self, text="进入游戏", font=("comicsansms", 20), command=self.start_button,
background="lightblue")
self.start_button.pack(pady=20)
self.quit_button = tk.Button(self, text="退出游戏", font=("comicsansms", 20), command=self.quit, background="lightgreen")
self.quit_button = tk.Button(self, text="退出游戏", font=("comicsansms", 20), command=self.quit,
background="lightgreen")
self.quit_button.pack(pady=20)
self.about_button = tk.Button(self, text="关于游戏", font=("comicsansms", 20), command=self.about_button, background="lightyellow")
self.about_button = tk.Button(self, text="关于游戏", font=("comicsansms", 20), command=self.about_button,
background="lightyellow")
self.about_button.pack(pady=20)
self.mainloop()
def set_image_as_background(self, image_path):
img = Image.open(image_path)
img = img.resize((self.root.winfo_reqwidth(), self.root.winfo_reqheight()), Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(img)
self.canvas = tk.Canvas(self.root, width=self.root.winfo_reqwidth(), height=self.root.winfo_reqheight())
self.canvas.pack(fill="both", expand=True)
# 在Canvas上放置图片
self.canvas.create_image(0, 0, image=photo, anchor="nw")
self.canvas.image = photo
# def set_background_image(self):
# # 加载图片
# bg_image = tk.PhotoImage(file="images/bg.gif")
# # 创建一个标签来承载图片,并设置到最底层
# bg_label = tk.Label(self, image=bg_image)
# bg_label.place(x=0, y=0, relwidth=1, relheight=1)
# # 确保图片保持引用否则它会被Python的垃圾回收机制销毁
# bg_label.image = bg_image
def start_button(self):
print("开始游戏")
self.withdraw() # 点击开始游戏后隐藏开始界面
alien_invasion.run_game()
def about_button(self):
@ -39,14 +43,14 @@ class GameWelcomeScreen(tk.Tk):
root.geometry("400x300")
tk.Label(root, text="这是一款使用Pygame包来开发的2D游戏。"
"\n它在玩家每消灭一群向下移动的外星人后,将玩家提高一个等级。\n每射杀一个外星人获得的相应的分数,等级越高,"
"\n游戏的节奏越快,难度越大,击杀每个外星人获得的分数就越高。\n"
"玩家的任务就是击落每个从屏幕上方移动下来的外星人,\n当外星人碰到飞船或者屏幕底部时该飞船被摧毁,\n"
"每个玩家可拥有三艘飞船,当三艘飞船都被摧毁时结束游戏\n得出最高分。\n\n\n"
"游戏使用:左右键移动飞船,空格键发射子弹",
"\n游戏的节奏越快,难度越大,击杀每个外星人获得的分数就越高。\n"
"玩家的任务就是击落每个从屏幕上方移动下来的外星人,\n当外星人碰到飞船或者屏幕底部时该飞船被摧毁,\n"
"每个玩家可拥有三艘飞船,当三艘飞船都被摧毁时结束游戏\n得出最高分。\n\n\n"
"游戏使用:左右键移动飞船,空格键发射子弹",
font=("comicsansms", 10), foreground='black').pack(pady=20)
root.mainloop()
if __name__ == "__main__":
app = GameWelcomeScreen()
app.mainloop()
app.mainloop()

@ -1,59 +0,0 @@
python
复制代码
import tkinter as tk
from PIL import Image, ImageTk
class TkinterBackground:
def __init__(self, root, image_path):
self.root = root
self.set_image_as_background(image_path)
def set_image_as_background(self, image_path):
# 加载图片
img = Image.open(image_path)
# 确保图片与窗口大小匹配,或者你可以调整图片大小
img = img.resize((self.root.winfo_reqwidth(), self.root.winfo_reqheight()), Image.Resampling.LANCZOS)
# 使用PIL的ImageTk将PIL Image转换为Tkinter的PhotoImage
photo = ImageTk.PhotoImage(img)
# 创建一个Canvas来放置图片
self.canvas = tk.Canvas(self.root, width=self.root.winfo_reqwidth(), height=self.root.winfo_reqheight())
self.canvas.pack(fill="both", expand=True)
# 在Canvas上放置图片
self.canvas.create_image(0, 0, image=photo, anchor="nw")
# 保持对PhotoImage的引用否则它会被Python的垃圾回收机制回收
self.canvas.image = photo
# 使用示例
root = tk.Tk()
root.title("Image Background")
# 设置窗口大小(可选,你可以根据需要设置)
root.geometry("400x300")
# 创建一个TkinterBackground的实例并设置图片背景
background = TkinterBackground(root, "path_to_your_image.jpg") # 替换为你的图片路径
# 运行Tkinter事件循环
root.mainloop()
Loading…
Cancel
Save