from pygame.image import load
import pygame
def globalExit(event):
if e.type == pygame.QUIT:
return False
else:
return True
class Gameobject:
def __init__(self, Name, Class, Color, Texture, Screen, X, Y, With, Height, Animation=False, Health=False,
Damage=False, Speed=0, Def='inf', Prana='none', Bitmap=False, Map='none'):
self.name = Name
self.Class = Class
self.color = Color
self.texture = load(Texture)
self.bitmap = Bitmap
self.animation = Animation
self.health = Health
self.damage = Damage
self.speed = Speed
self.defense = Def
self.prana = Prana
self.screen = Screen
self.x = X
self.y = Y
self.With = With
self.Height = Height
self.map = Map
def render(self):
self.screen.blit(self.texture, (self.x, self.y))
def animation(self):
if type(self.animation) is dict:
return True
else:
return False
def move(self, event):
"""События нажатий клавиш"""
print(event)
def moveDOWN(self, event):
keys = {'w': False, 'a': False, 's': False, 'd': False}
if event.type == pygame.KEYDOWN:
k = chr(event.key)
if k in keys:
keys[k] = True
elif event.type == pygame.KEYUP:
k = chr(event.key)
if k in keys:
keys[k] = False
if keys['w']:
if keys['d']:
if self.x < 600 - self.With:
self.x -= 1
if self.y > 0:
self.y -= 1
if keys['a']:
if self.x > 0:
self.x -= 1
if keys['d']:
if self.x < 600 - self.With:
self.x += 1
if keys['s']:
if self.y < 190 - self.Height:
self.y += 1
# if event.type == pygame.KEYDOWN:
# if (event.key == pygame.K_a or event.key == pygame.K_LEFT) and self.x > 0:
# self.x -= 1
# elif (event.key == pygame.K_w or event.key == pygame.K_UP) and self.y > 0:
# self.y -= 1
# elif (event.key == pygame.K_d or event.key == pygame.K_RIGHT) and self.x < 600 - self.With:
# self.x += 1
# elif (event.key == pygame.K_s or event.key == pygame.K_DOWN) and self.y < 190 - self.Height:
# self.y += 1
window = pygame.display.set_mode((600, 400))
main = pygame.Surface((600, 190))
main1 = pygame.Surface((600, 190))
player = Gameobject(Name='Player',
Class='gamePlayer',
Color=(0, 0, 0),
Texture='image/1.png',
Screen=main,
X=0,
Y=0,
Height=85,
With=50)
done = True
pygame.key.set_repeat(5, 2)
while done:
main.fill((50, 50, 50))
main1.fill((50, 50, 50))
player.render()
for e in pygame.event.get():
done = globalExit(e)
player.moveDOWN(e)
window.blit(main, (0, 0))
window.blit(main1, (0, 210))
pygame.display.flip()
pygame.time.delay(5)
for e in pygame.event.get():
done = globalExit(e)
player.moveDOWN(e)
def moveDOWN(self, event):
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_a or event.key == pygame.K_LEFT) and self.x > 0:
self.x -= 1
elif (event.key == pygame.K_w or event.key == pygame.K_UP) and self.y > 0:
self.y -= 1
elif (event.key == pygame.K_d or event.key == pygame.K_RIGHT) and self.x < 600 - self.With:
self.x += 1
elif (event.key == pygame.K_s or event.key == pygame.K_DOWN) and self.y < 190 - self.Height:
self.y += 1