def mainloop(self): if (GlobalData.displayInitialized==0): self.initDisplay() self.initPlayer() pygame.key.set_repeat(75, 75) TextBox.loadTextImages(3, 3, -1) Battle.loadBattleTextures() Creature.loadCreatureImages() Map.loadTileSet("Exterior_Town1.png", 30, 16) Map.loadTileSet("Interior_Town1.png", 30, 16) Map.loadTileSet("Interior_Cave1.png", 30, 16) self.map = Map.Map("Exterior_Town1", "Exterior_Town1-6", 30, 30) self.maps["Exterior_Town1"] = [self.map] #timer = pygame.time.get_ticks() #timeOffset = 0.00 while not GlobalData.quitFlag: for e in pygame.event.get(): if e.type == QUIT: GlobalData.quitFlag = 1 return self.playerInput(e) pygame.event.pump() self.drawWorld() self.printFPS() self.flipScreenBuffer() self.timer.tick(12)
def getTextBoxes(self,boxes,text,fontN): for box in boxes: b=TextBox(box,text,fontN,self.screen) if not b.isEmpty(): self.text_boxes.append(b)
class Customize: # The customize menu screen def __init__(self, canvasWidth, canvasHeight, player): self.mountain = Animation("Mountain", 2, 15) self.lad = Animation("m-LU", 2, 10) self.lass = Animation("f-RU", 2, 10) self.canvasWidth = canvasWidth self.canvasHeight = canvasHeight self.player = player self.customizeButtons = [ Button(canvasWidth * 9 / 20, canvasHeight * 6 / 7, canvasWidth / 10, canvasHeight / 10, "Save\nChanges", "saveCustomize"), Button(canvasWidth * 4 / 5, canvasHeight / 3, canvasWidth / 10, canvasHeight / 5, "", "toLad"), Button(canvasWidth / 10, canvasHeight / 3, canvasWidth / 10, canvasHeight / 5, "", "toLass"), Button(canvasWidth / 100, canvasHeight / 100, canvasWidth / 25, canvasHeight / 25, "Back", "menu") ] self.namePlate = TextBox(self.canvasWidth * 2 / 5, self.canvasHeight * 3 / 4, self.canvasWidth / 5, self.canvasHeight / 30, self.player.attributes["name"]) def run(self, modeTime): self.mountain.display(self.canvasWidth / 2, self.canvasHeight / 2, self.canvasWidth, self.canvasHeight) fill(255, 0, 0) if self.player.attributes["gender"] == "m": stroke(255) strokeWeight(4) self.customizeButtons[1].run() stroke(0) strokeWeight(1) fill(0, 0, 255) if self.player.attributes["gender"] == "f": stroke(255) strokeWeight(4) self.customizeButtons[2].run() stroke(0) strokeWeight(1) self.lad.display(self.canvasWidth * 4 / 5 + self.canvasWidth / 20, self.canvasHeight / 3 + self.canvasHeight / 10, self.canvasWidth / 10, self.canvasHeight / 5) self.lass.display(self.canvasWidth / 10 + self.canvasWidth / 20, self.canvasHeight / 3 + self.canvasHeight / 10, self.canvasWidth / 10, self.canvasHeight / 5) fill(255) self.customizeButtons[0].run() fill(255) self.customizeButtons[3].run() self.namePlate.display(modeTime)
def getInput(x, y): #Get the users input, for entering values into the board #Get current cell position boxx = int(((x * 9) / WIDTH)) * (CELLSIZE) boxy = int(((y * 9) / HEIGHT)) * (CELLSIZE) textinput = TextBox.TextInput() num = ' ' text = True #Input loop while text: events = py.event.get() for event in events: if event.type == py.QUIT: exit() if textinput.update(events): num = textinput.get_text() #Check if input is valid if not isValidInput(num): num = 0 text = False #Display the user typing window.blit(textinput.get_surface(), (boxx, boxy)) py.display.update() return num
def __init__(self, pos: Point, box_color: Color, background_color: Color, font: Font, font_color: Color, fps: int = 40, ttl: int = 5000): self.__fps = fps self.__ttl = ttl self.__total_ms = 0 self.__hour = 0 self.__minute = 0 self.__second = 0 self.__first = True self.__clock = pygame.time.Clock() is_input = [False, False, False] data = [("Hour", "{0:02}:".format(self.__hour)), ("Minute", "{0:02}:".format(self.__minute)), ("Second", "{0:02}".format(self.__second))] self.__hour_box, self.__minute_box, self.__second_box = \ TextBox.create_array( pos, (font_color, background_color), 0, is_input, data, font) self.__hour_box.set_transparent(False) self.__minute_box.set_transparent(False) self.__second_box.set_transparent(False)
def main(): global fpsLock, window global BASICFONT, BASICFONTSIZE #Initialize Game py.init() window = py.display.set_mode((WIDTH, HEIGHT)) fpsLock = py.time.Clock() BASICFONTSIZE = 30 BASICFONT = py.font.Font('freesansbold.ttf', BASICFONTSIZE) py.display.set_caption('Sudoku Solver') board = initiateBoard() textinput = TextBox.TextInput() #Game Loop while True: mouseClicked = False for event in py.event.get(): if event.type == py.QUIT: py.quit() sys.exit() elif event.type == MOUSEMOTION: mousex, mousey = event.pos elif event.type == MOUSEBUTTONUP: mousex, mousey = event.pos mouseClicked = True elif event.type == py.KEYDOWN: if event.key == K_SPACE: #Solve the current board #Cast all elements of board to int board = [[int(x) for x in y] for y in board] solveSudoku(board) if event.key == K_DELETE: board = initiateBoard() #Input a number if mouseClicked == True: setNumber(mousex, mousey, board) window.fill(WHITE) drawGrid() populateGrid(board) drawBox(mousex, mousey) py.display.update() fpsLock.tick(FPS)
def __initialize_text_boxes(self, font: Font) -> List[TextBox.TextBox]: text_boxes = list() colors = (self.__stage_color, self.__walls_color) position = Point(self.__width + (self.__wall_width) + 10, self.__wall_height) separations = (5, 10) per_row = 2 is_input = Constants.TEXTBOX_MATRIX_IS_INPUT data = Constants.TEXTBOX_MATRIX text_boxes = TextBox.create_matrix(position, colors, separations, per_row, is_input, data, font) position = Point(5, self.__wall_height) is_input = Constants.INSTRUCTIONS_INPUT data = Constants.INSTRUCTIONS_TEXTBOXES text_boxes += TextBox.create_matrix(position, colors, separations, per_row, is_input, data, font) return text_boxes
def __init__(self, canvasWidth, canvasHeight, player): self.mountain = Animation("Mountain", 2, 15) self.lad = Animation("m-LU", 2, 10) self.lass = Animation("f-RU", 2, 10) self.canvasWidth = canvasWidth self.canvasHeight = canvasHeight self.player = player self.customizeButtons = [ Button(canvasWidth * 9 / 20, canvasHeight * 6 / 7, canvasWidth / 10, canvasHeight / 10, "Save\nChanges", "saveCustomize"), Button(canvasWidth * 4 / 5, canvasHeight / 3, canvasWidth / 10, canvasHeight / 5, "", "toLad"), Button(canvasWidth / 10, canvasHeight / 3, canvasWidth / 10, canvasHeight / 5, "", "toLass"), Button(canvasWidth / 100, canvasHeight / 100, canvasWidth / 25, canvasHeight / 25, "Back", "menu") ] self.namePlate = TextBox(self.canvasWidth * 2 / 5, self.canvasHeight * 3 / 4, self.canvasWidth / 5, self.canvasHeight / 30, self.player.attributes["name"])
from __future__ import print_function from TextBox import * if __name__ == "__main__": print("woof") T = TextBox() T.root.mainloop()
def run_game(): pygame.init() setinit = Game_Settings.Settings_of_Show() screen = pygame.display.set_mode( (setinit.screen_width, setinit.screen_height)) pygame.display.set_caption("联机坦克大战") tank1 = Tank.Tank1(screen) tank2 = Tank.Tank2(screen) bullets1 = Group() bullets2 = Group() start_button = Button.Button(screen, 80, 40, (0, 255, 0), (255, 255, 255), "开始游戏", (500, 200)) settings_button = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "游戏设置", (500, 300)) help_button = Button.Button(screen, 80, 40, (105, 194, 49), (255, 255, 255), "游戏帮助", (500, 400)) game_settings = Game_Settings.Settings(tank_speed=5, bullet_speed=1, bullet_allow_num=1) bullet_speed1 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "炮弹速度1", (200, 150)) bullet_speed2 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "炮弹速度2", (500, 150)) bullet_speed3 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "炮弹速度3", (800, 150)) tank_speed1 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "坦克速度1", (200, 300)) tank_speed2 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "坦克速度2", (500, 300)) tank_speed3 = Button.Button(screen, 80, 40, (164, 247, 151), (255, 255, 255), "坦克速度3", (800, 300)) login_name_input = TextBox.TextBox(screen, None, None, 20, "隶书", (0, 0, 0), (255, 255, 255), (500, 300), "输入用户名") password_input = TextBox.TextBox(screen, None, None, 20, "隶书", (0, 0, 0), (255, 255, 255), (500, 300), "输入密码") sound_shoot = pygame.mixer.Sound("music//shoot.wav") flag = None login_name = "wsde" ''' while flag!=login_name: screen.fill(setinit.bg_color) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: flag = login_name_input.key_down_text(event) print(flag) login_name_input.draw_textbox() pygame.display.flip() flag = None password: str = "mmnf" print(password) while flag != password: screen.fill(setinit.bg_color) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: flag= password_input.key_down_text(event) password_input.draw_textbox() pygame.display.flip() ''' # 游戏主界面 while True: screen.fill(setinit.bg_color) flag = game_button(start_button, settings_button, help_button) if flag == 0: settings_button.draw() start_button.draw() help_button.draw() if flag == -1: pygame.display.flip() screen.fill(setinit.bg_color) #print("-1") pygame.display.flip() #print(game_settings.tank_speed, game_settings.bullet_speed) score_of_two = Score.Score(screen, tank1, tank2, bullets1, bullets2, setinit) # 进入游戏 main_flag = False while not main_flag: screen.fill(setinit.bg_color) score_of_two.score_change() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() tank1_control_event(game_settings, tank1, bullets1, screen, sound_shoot) tank2_control_event(game_settings, tank2, bullets2, screen, sound_shoot) score_of_two.score_draw() for i in bullets1.copy(): if i.rect.centerx <= 0 or i.rect.centerx >= 1000: bullets1.remove(i) elif i.rect.centery >= 600 or i.rect.centery <= 0: bullets1.remove(i) for i in bullets1.sprites(): i.draw_bull() for i in bullets2.copy(): if i.rect.centerx <= 0 or i.rect.centerx >= 1000: bullets2.remove(i) elif i.rect.centery >= 600 or i.rect.centery <= 0: bullets2.remove(i) for i in bullets2.sprites(): i.draw_bull() update_screen(tank1, bullets1, tank2, bullets2) main_flag = score_of_two.who_win() pygame.display.flip() score_of_two.who_win() time.sleep(10) if flag == -2: pygame.display.flip() screen.fill(setinit.bg_color) # print("-2") temp_settings = -1 while True: # print("1") if temp_settings == -1: screen.fill(setinit.bg_color) # print("设定难度") bullet_speed1.draw() bullet_speed2.draw() bullet_speed3.draw() tank_speed1.draw() tank_speed2.draw() tank_speed3.draw() temp_settings: int = settings_mode_ui( game_settings, bullet_speed1, bullet_speed2, bullet_speed3, tank_speed1, tank_speed2, tank_speed3) pygame.display.flip() # print(temp_settings) if temp_settings == 1: screen.fill(setinit.bg_color) break if temp_settings == 2: screen.fill(setinit.bg_color) break if temp_settings == 3: screen.fill(setinit.bg_color) break if temp_settings == 4: screen.fill(setinit.bg_color) break if temp_settings == 5: screen.fill(setinit.bg_color) break if temp_settings == 6: screen.fill(setinit.bg_color) break if flag == -3: pygame.display.flip() screen.fill(setinit.bg_color) # print("-2") help_flag = -1 jump_end_button = Button.Button(screen, 40, 50, (164, 247, 151), (255, 255, 255), "结束", (800, 300)) while True: if help_flag == -1: screen.fill(setinit.bg_color) font = pygame.font.SysFont("隶书", 20) text = font.render("服务器端上线后,客户端才可以登录", True, (255, 255, 255), (0, 255, 0)) text_rect = text.get_rect() text_rect.center = screen.get_rect().center screen.blit(text, text_rect) jump_end_button.draw() help_flag = end_help(jump_end_button) pygame.display.flip() if help_flag == 1: break pygame.display.flip()
import pygame import TextBox pygame.init() width, height = 800, 700 screen = pygame.display.set_mode((width, height)) textbox1 = TextBox.TextBox((0, 0, 100, 35)) textbox2 = TextBox.TextBox((0, 50, 100, 35)) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if textbox1.active: textbox1.text_input(event) if textbox2.active: textbox2.text_input(event) textbox1.test_collide(event) textbox2.test_collide(event) screen.fill((0, 0, 0)) textbox1.draw(textbox1.text, screen) textbox2.draw(textbox2.text, screen) pygame.display.flip() clock.tick(60)
def __init__(self, screen_width, screen_height): self.screen = Screen(self, screen_width, screen_height) self.screen_width = screen_width self.screen_height = screen_height start = Entity(0, 0, "", "", 45, 0) start.setSprite("sprites/startingScreen.txt", ) start.drawArena(self.screen) self.screen.print() self.textBox = TextBox(0, 15) self.isOverworld = True # False if the current stage is the Arena self.overworld = Overworld(self, width=40, height=11, overworld_x=60, overworld_y=4) self.player = Player( overworld_x=81, overworld_y=9, sprites_path="sprites/player.txt", overworldChar="P", arena_x=50, arena_y=10, defensePower=100, evade=0.2, health=1000, crit=0.2, moveset=[ Attack(name='Heavy Attack', damage=450, hitChance=0.3, statusEffect=StatusEffect( name="Shock", duration=6, damagePerTurn=40, sprite_path="sprites/shockEffect.csv")), Attack(name='Regular Attack', damage=150, hitChance=0.7, statusEffect=StatusEffect( name="Poison", duration=4, damagePerTurn=20, sprite_path="sprites/poisonEffect.csv")), Attack(name='Light Attack', damage=60, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=6, damagePerTurn=40, sprite_path="sprites/spiceEffect.csv")) ]) # array containing all monsters self.monsters = [ Monster(overworld_x=84, overworld_y=11, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=20, health=1000, evade=0.1, crit=0.3, moveset=[ Attack(name='Ultimate attack', damage=300, hitChance=0.9999, statusEffect=StatusEffect( name="Poison", duration=4, damagePerTurn=37, sprite_path="sprites/poisonEffect.csv")), Attack(name='Spice Attack', damage=160, hitChance=0.7, statusEffect=StatusEffect( name="Shock", duration=6, damagePerTurn=8, sprite_path="sprites/shockEffect.csv")), Attack(name='Light Attack', damage=120, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=3, damagePerTurn=14, sprite_path="sprites/spiceEffect.csv")) ]), #devil pepper ascci Monster(overworld_x=74, overworld_y=8, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=10, health=600, evade=0.1, crit=0.6, moveset=[ Attack(name='Ultimate attack', damage=250, hitChance=0.9999, statusEffect=StatusEffect( name="Spice", duration=6, damagePerTurn=38, sprite_path="sprites/spiceEffect.csv")), Attack(name='Regular Attack', damage=240, hitChance=0.6), Attack(name='Light Attack', damage=200, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=3, damagePerTurn=6, sprite_path="sprites/spiceEffect.csv")) ]), #fire pepper assci orsomething Monster(overworld_x=88, overworld_y=8, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=50, health=1500, evade=0.1, crit=0.3, moveset=[ Attack(name='Ultimate attack', damage=100, hitChance=0.9999, statusEffect=StatusEffect( name="Spice", duration=4, damagePerTurn=78, sprite_path="sprites/spiceEffect.csv")), Attack(name='Regular Attack', damage=50, hitChance=0.7, statusEffect=StatusEffect( name="Shock", duration=4, damagePerTurn=70, sprite_path="sprites/shockEffect.csv")), Attack(name='Light Attack', damage=30, hitChance=1, statusEffect=StatusEffect( name="Poison", duration=5, damagePerTurn=68, sprite_path="sprites/poisonEffect.csv")) ]) ] self.playerHealthBar = HealthBar(self.player) self.healthpot = [ HealthPot(overworld_x=78, overworld_y=11, overworldChar="+", ASCII=["+"], health=600), HealthPot(overworld_x=84, overworld_y=7, overworldChar="+", ASCII=["+"], health=600) ] #Counters for stats self.damageInflicted = 0 self.damageReceived = 0 self.score = 0 self.arena = None self.reset = False self.exit = False self.loop()