@ -0,0 +1,75 @@
|
||||
#Function:定义一些精灵类
|
||||
import random
|
||||
import pygame
|
||||
|
||||
'''墙类'''
|
||||
class Wall(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y, width, height, color, **kwargs):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.image = pygame.Surface([width, height])
|
||||
self.image.fill(color)
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.left = x
|
||||
self.rect.top = y
|
||||
|
||||
'''食物类'''
|
||||
class Food(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y, width, height, color, bg_color, **kwargs):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.image = pygame.Surface([width, height])
|
||||
self.image.fill(bg_color)
|
||||
self.image.set_colorkey(bg_color)
|
||||
pygame.draw.ellipse(self.image, color, [0, 0, width, height])
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.left = x
|
||||
self.rect.top = y
|
||||
|
||||
'''角色类'''
|
||||
class Player(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y, role_image_path):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.role_name = role_image_path.split('/')[-1].split('.')[0]
|
||||
self.base_image = pygame.image.load(role_image_path).convert()
|
||||
self.image = self.base_image.copy()
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.left = x
|
||||
self.rect.top = y
|
||||
self.prev_x = x
|
||||
self.prev_y = y
|
||||
self.base_speed = [30, 30]
|
||||
self.speed = [0, 0]
|
||||
self.is_move = False
|
||||
self.tracks = []
|
||||
self.tracks_loc = [0, 0]
|
||||
'''改变速度方向'''
|
||||
def changeSpeed(self, direction):
|
||||
if direction[0] < 0:
|
||||
self.image = pygame.transform.flip(self.base_image, True, False)
|
||||
elif direction[0] > 0:
|
||||
self.image = self.base_image.copy()
|
||||
elif direction[1] < 0:
|
||||
self.image = pygame.transform.rotate(self.base_image, 90)
|
||||
elif direction[1] > 0:
|
||||
self.image = pygame.transform.rotate(self.base_image, -90)
|
||||
self.speed = [direction[0] * self.base_speed[0], direction[1] * self.base_speed[1]]
|
||||
return self.speed
|
||||
'''更新角色位置'''
|
||||
def update(self, wall_sprites, gate_sprites):
|
||||
if not self.is_move:
|
||||
return False
|
||||
x_prev = self.rect.left
|
||||
y_prev = self.rect.top
|
||||
self.rect.left += self.speed[0]
|
||||
self.rect.top += self.speed[1]
|
||||
is_collide = pygame.sprite.spritecollide(self, wall_sprites, False)
|
||||
if gate_sprites is not None:
|
||||
if not is_collide:
|
||||
is_collide = pygame.sprite.spritecollide(self, gate_sprites, False)
|
||||
if is_collide:
|
||||
self.rect.left = x_prev
|
||||
self.rect.top = y_prev
|
||||
return False
|
||||
return True
|
||||
'''生成随机的方向'''
|
||||
def randomDirection(self):
|
||||
return random.choice([[-0.5, 0], [0.5, 0], [0, 0.5], [0, -0.5]])
|
@ -0,0 +1,118 @@
|
||||
import tkinter as tk #标准GUI库
|
||||
from tkinter import messagebox #显示消息框
|
||||
from PIL import ImageTk #显示图像
|
||||
|
||||
# 存储注册成功用户名及密码
|
||||
success = []
|
||||
|
||||
# 主窗口
|
||||
window = tk.Tk()
|
||||
window.title("Please Log In")
|
||||
window.geometry("900x660")
|
||||
|
||||
# 背景画布
|
||||
canvas = tk.Canvas(window,width=900,height=500)
|
||||
image_file = ImageTk.PhotoImage(file="resources/title2.jpg")
|
||||
image = canvas.create_image(0,0,anchor="nw",image=image_file)
|
||||
canvas.pack()
|
||||
|
||||
# 用户名,用户密码文本
|
||||
name_lable = tk.Label(window,text="UserName:",font=(15))
|
||||
password_lable = tk.Label(window,text="Password:",font=(15))
|
||||
name_lable.place(x=300,y=510)
|
||||
password_lable.place(x=300,y=550)
|
||||
|
||||
# 用户名,用户密码输入框
|
||||
nameval = tk.StringVar()
|
||||
passwordval = tk.StringVar()
|
||||
name_entry = tk.Entry(window,textvariable=nameval,font=(12))
|
||||
password_entry = tk.Entry(window,textvariable=passwordval,show="*",font=(12))
|
||||
name_entry.place(x=400,y=515)
|
||||
password_entry.place(x=400,y=555)
|
||||
|
||||
# 登录按钮触发函数
|
||||
def sign_in_f():
|
||||
user_si_name = name_entry.get()
|
||||
user_si_pass = password_entry.get()
|
||||
if user_si_name in success:
|
||||
i = success.index(user_si_name)
|
||||
if success[i+1] == user_si_pass:
|
||||
start_game()
|
||||
else:
|
||||
tk.messagebox.showinfo(title="登录提示",message="密码错误")
|
||||
else:
|
||||
result = tk.messagebox.askquestion(title="登录提示",message="用户名不存在,是否立即注册?")
|
||||
if result == "yes":
|
||||
sign_up_f()
|
||||
else:
|
||||
pass
|
||||
|
||||
def start_game():
|
||||
# 导入游戏主界面
|
||||
import main
|
||||
# 关闭登录界面
|
||||
window.destroy()
|
||||
# 显示游戏主界面
|
||||
main.login()
|
||||
|
||||
# 注册按钮触发函数
|
||||
def sign_up_f():
|
||||
# 用户注册界面
|
||||
singn_up_w = tk.Tk()
|
||||
singn_up_w.title("用户注册")
|
||||
singn_up_w.geometry("600x400")
|
||||
|
||||
# 拥护注册 用户名,密码,确认密码文本
|
||||
su_name_lable = tk.Label(singn_up_w,text="UserName:",font=(12))
|
||||
su_pass_lable = tk.Label(singn_up_w,text="Password:",font=(12))
|
||||
su_cpass_lable = tk.Label(singn_up_w,text="Confirm Password:",font=(12))
|
||||
su_name_lable.place(x=95,y=50)
|
||||
su_pass_lable.place(x=95,y=150)
|
||||
su_cpass_lable.place(x=95,y=250)
|
||||
|
||||
# 用户注册 用户名,密码,确认密码输入框
|
||||
su_name_val = tk.StringVar()
|
||||
su_pass_val = tk.StringVar()
|
||||
su_cpass_val = tk.StringVar()
|
||||
su_name_entry = tk.Entry(singn_up_w,textvariable=su_name_val,width=20,font=(12))
|
||||
su_pass_entry = tk.Entry(singn_up_w,textvariable=su_pass_val,width=20,show="*",font=(12))
|
||||
su_cpass_entry = tk.Entry(singn_up_w,textvariable=su_cpass_val,width=20,show="*",font=(12))
|
||||
su_name_entry.place(x=270,y=50)
|
||||
su_pass_entry.place(x=270,y=150)
|
||||
su_cpass_entry.place(x=270,y=250)
|
||||
|
||||
# 用户在注册页面点击注册按钮触发的函数
|
||||
def su_conf_b():
|
||||
su_username = su_name_entry.get()
|
||||
su_userpass = su_pass_entry.get()
|
||||
su_usercpass = su_cpass_entry.get()
|
||||
if su_userpass == su_usercpass:
|
||||
tk.messagebox.showinfo(title="注册提示",message="注册成功,请登录")
|
||||
success.append(su_username)
|
||||
success.append(su_userpass)
|
||||
singn_up_w.destroy()
|
||||
else:
|
||||
tk.messagebox.showinfo(title="注册提示",message="两次输入的密码不同,请重新输入")
|
||||
|
||||
# 用户在注册页面点击取消按钮触发的函数
|
||||
def su_cancel_b():
|
||||
result = tk.messagebox.askquestion(title="放弃注册",message="你真的要放弃注册吗?")
|
||||
if result == "yes":
|
||||
singn_up_w.destroy()
|
||||
else:
|
||||
pass
|
||||
|
||||
# 用户注册 注册,取消按钮
|
||||
su_confirm_button = tk.Button(singn_up_w,text="Confirm",command=su_conf_b)
|
||||
su_cancle_button = tk.Button(singn_up_w,text="Cancel",command=su_cancel_b)
|
||||
su_confirm_button.place(x=170,y=330)
|
||||
su_cancle_button.place(x=370,y=330)
|
||||
|
||||
# 登录,注册按钮
|
||||
sign_in_button = tk.Button(window,text="吃豆启动!",command=sign_in_f)
|
||||
sign_up_button = tk.Button(window,text="注册",command=sign_up_f)
|
||||
sign_in_button.place(x=350,y=600)
|
||||
sign_up_button.place(x=470,y=600)
|
||||
|
||||
|
||||
window.mainloop()
|
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 924 B |
After Width: | Height: | Size: 91 KiB |
@ -0,0 +1,70 @@
|
||||
import pygame
|
||||
import random
|
||||
|
||||
# 初始化Pygame库
|
||||
pygame.init()
|
||||
|
||||
# 游戏窗口的宽度和高度
|
||||
width = 800
|
||||
height = 600
|
||||
|
||||
# 创建游戏窗口
|
||||
window = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption("吃豆") #游戏窗口标题
|
||||
|
||||
# 定义各种颜色
|
||||
black = (0, 0, 0)
|
||||
white = (255, 255, 255)
|
||||
yellow = (255, 255, 0)
|
||||
red = (255, 0, 0)
|
||||
|
||||
# 定义吃豆人的初始位置和速度
|
||||
pacman_x = width // 2
|
||||
pacman_y = height // 2
|
||||
pacman_speed = 5
|
||||
|
||||
# 定义豆子的数量和大小
|
||||
dot_size = 10
|
||||
dot_count = 100
|
||||
|
||||
# 创建豆子的列表
|
||||
dots = []
|
||||
for i in range(dot_count):
|
||||
dot_x = random.randint(0, width - dot_size)
|
||||
dot_y = random.randint(0, height - dot_size)
|
||||
dots.append((dot_x, dot_y))
|
||||
|
||||
# 游戏主循环
|
||||
running = True
|
||||
while running:
|
||||
# 处理事件
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# 获取按键状态
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_LEFT]:
|
||||
pacman_x -= pacman_speed
|
||||
if keys[pygame.K_RIGHT]:
|
||||
pacman_x += pacman_speed
|
||||
if keys[pygame.K_UP]:
|
||||
pacman_y -= pacman_speed
|
||||
if keys[pygame.K_DOWN]:
|
||||
pacman_y += pacman_speed
|
||||
|
||||
# 碰撞检测
|
||||
for dot in dots:
|
||||
dot_x, dot_y = dot #将(x,y)坐标解包到单独的变量
|
||||
if pacman_x < dot_x + dot_size and pacman_x + dot_size > dot_x and pacman_y < dot_y + dot_size and pacman_y + dot_size > dot_y:
|
||||
dots.remove(dot)
|
||||
|
||||
# 渲染画面
|
||||
window.fill(black)
|
||||
pygame.draw.circle(window, yellow, (pacman_x, pacman_y), 20)
|
||||
for dot in dots:
|
||||
pygame.draw.rect(window, white, (dot[0], dot[1], dot_size, dot_size))
|
||||
pygame.display.flip()
|
||||
|
||||
# 退出游戏
|
||||
pygame.quit()
|