Я пытаюсь сделать эту игру, и в данный момент мне удалось получить ее, чтобы игрок не мог двигаться влево или вправо за указанную границу. Однако, когда они прыгают, они могут упасть ниже нижней части экрана. Мне было интересно, если кто-нибудь может помочь сделать это, чтобы этого не произошло. Смотрите код ниже:
import tkinter as tk
from tkinter import *
import pygame as pg
import os
from PIL import ImageTk,Image
#____________________________________________________________________________________________________________________________#
class HoverButton(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, e):
self['background'] = self['activebackground']
def on_leave(self, e):
self['background'] = self.defaultBackground
#____________________________________________________________________________________________________________________________#
class Player():
def __init__(self, playerX, playerY, width, height, vel, screen):
super().__init__()
self.playerX = playerX
self.playerY = playerY
self.width = width
self.height = height
self.screen = screen
self.vel = vel
self.image = pg.image.load("mainchar.png").convert_alpha()
def moveRight(self, vel):
self.playerX += vel
def moveLeft(self, vel):
self.playerX -= vel
#____________________________________________________________________________________________________________________________#
class Enemy():
pass
#____________________________________________________________________________________________________________________________#
def game(MapDesign):
global screen
global SCREEN_WIDTH
global SCREEN_HEIGHT
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 896
pg.init()
screen = pg.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
clock = pg.time.Clock()
pg.mixer.music.load('maingame.mp3')
pg.mixer.music.play(-1)
crashed = False
player = Player(32, 832, 32, 32, 10, screen)
isjump = False
jumpcount = 8
while not crashed:
for event in pg.event.get():
if event.type == pg.QUIT:
crashed = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_x:
crashed = True
q=w=0
level = []
with open(MapDesign, "r") as f:
lines = f.readlines()
for i in lines:
level.append(i)
for row in lines:
for col in row:
if col == 'a':
Block = pg.image.load(os.path.join('floor.png'))
screen.blit(Block, (q, w, q+32, w+32))
if col == 'p':
Block = pg.image.load(os.path.join('bg.png'))
screen.blit(Block, (q, w, q+32, w+32))
if col == 'x':
Block = pg.image.load(os.path.join('wall.png'))
screen.blit(Block, (q, w, q+32, w+32))
if col == 'i':
Block = pg.image.load(os.path.join('gray.png'))
screen.blit(Block, (q, w, q+32, w+32))
q +=32
w +=32
q=0
pressed = pg.key.get_pressed()
if pressed[pg.K_LEFT] and player.playerX > player.vel + 22:
player.moveLeft(player.vel)
print("left")
if pressed[pg.K_RIGHT] and player.playerX < 992 - player.vel - player.width:
player.moveRight(player.vel)
print("right")
## MUST PRESS SPACE TO START FEATURE
if not(isjump):
if pressed[pg.K_SPACE]:
isjump = True
else:
if jumpcount >= -10:
player.playerY -= (jumpcount * abs(jumpcount)) * 0.5
jumpcount -= 1
print(player.playerY)
else:
jumpcount = 10
isjump = False
screen.blit(player.image, (player.playerX, player.playerY))
pg.display.flip()
clock.tick(60)
pg.quit()
quit()
#____________________________________________________________________________________________________________________________#
def level1():
global MapDesign
MapDesign = "map1.txt"
game(MapDesign)
#____________________________________________________________________________________________________________________________#
def level2():
MapDesign = "map2.txt"
game(MapDesign)
#____________________________________________________________________________________________________________________________#
def level3():
MapDesign = "map3.txt"
game(MapDesign)
#____________________________________________________________________________________________________________________________#
def level4():
MapDesign = "map4.txt"
game(MapDesign)
#____________________________________________________________________________________________________________________________#
def info():
info_screen = Toplevel(level_screen)
info_screen.minsize(width = 1056, height = 896)
info_screen.maxsize(width = 1056, height = 896)
info_screen.title("How to play the game")
path1 = "graybig.png"
img1 = ImageTk.PhotoImage(Image.open(path1))
panel1 = tk.Label(info_screen, image = img1)
panel1.pack(side = "bottom", fill = "both", expand = "yes")
info_screen.mainloop()
#____________________________________________________________________________________________________________________________#
def Exit():
pass
#____________________________________________________________________________________________________________________________#
red = "f70d1a"
def SelectionScreen():
global level_screen
level_screen = Tk()
level_screen.minsize(width = 1056, height = 896)
level_screen.maxsize(width = 1056, height = 896)
path2 = "graybig.png"
img2 = ImageTk.PhotoImage(Image.open(path2))
panel2 = tk.Label(level_screen, image = img2)
panel2.pack(side = "bottom", fill = "both", expand = "yes")
Button(level_screen, text = "Level 1", bg = "white", activebackground = "light green", width = 30, height = 3, font = "Aharoni", command = level1).place(x=185, y=200)
Button(level_screen, text = "Level 2", bg = "white", activebackground = "light green", width = 30, height = 3, font = "Aharoni", command = level2).place(x=185, y=350)
Button(level_screen, text = "Level 3", bg = "white", activebackground = "light green", width = 30, height = 3, font = "Aharoni", command = level3).place(x=600, y=200)
Button(level_screen, text = "Level 4", bg = "white", activebackground = "light green", width = 30, height = 3, font = "Aharoni", command = level4).place(x=600, y=350)
Button(level_screen, text = "Info", bg = "white", activebackground = "light grey", width = 30, height = 3, font = "Aharoni", command = info).place(x=185, y=680)
Button(level_screen, text = "Exit", bg = "white", activebackground = "light grey", width = 64, height = 68, font = "Aharoni", command = Exit).place(x=600, y=680)
level_screen.mainloop
SelectionScreen()
'' '