-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
135 lines (119 loc) · 4.59 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pygame
from pygame.locals import *
import math
import random
import magic
class Player(pygame.sprite.Sprite):
player_speed = 3
playerpos=[100,300]
rotation = 0;
playerrot = 0;
infront = (0,0)
itemHolding = None
halteentfernung = 45
def __init__(self):
#super().__init__()
self.image = pygame.image.load("resources/images/cook1.png")
self.rect = pygame.Rect((self.playerpos[0]-16,self.playerpos[1]-16),(40,40))
def Draw(self):
playerrot = pygame.transform.rotate(self.image , self.rotation)
playerpos1 = (self.playerpos[0]-playerrot.get_rect().width/2, self.playerpos[1]-playerrot.get_rect().height/2)
magic.mapScreen.blit(playerrot, playerpos1)
# pygame.draw.rect(magic.mapScreen, (11,31, 131), self.rect)
def Collision(self):
for o in magic.MTiles:
boxrect = pygame.Rect(o.image.get_rect())
boxrect.topleft = [o.pos[0],o.pos[1]]
if self.rect.colliderect(boxrect):
return True
return False
def Move(self):
spd = self.player_speed
x = self.playerpos[0]
y = self.playerpos[1]
# Move player
if magic.keyPress1 == 1 and y > 0:
y -= spd
self.rotation = 0
self.infront = (0,-1)
elif magic.keyPress1 == 3 and y > 0 and x > 0:
y -= spd
x -= spd
self.rotation = 45
self.infront = (-0.75,-0.75)
elif magic.keyPress1 == 2 and x > 0:
x -= spd
self.rotation = 90
self.infront = (-1,0)
elif magic.keyPress1 == 4 and y < magic.mapHeight:
y += spd
self.rotation = 180
self.infront = (0,1)
elif magic.keyPress1 == 6 and x > 0 and y < magic.mapHeight:
y += spd
x -= spd
self.rotation = 135
self.infront = (-0.75,0.75)
elif magic.keyPress1 == 8 and x < magic.mapWidth:
x += spd
self.rotation = 270
self.infront = (1,0)
elif magic.keyPress1 == 9 and x < magic.mapWidth and y > 0:
y -= spd
x += spd
self.rotation = 315
self.infront = (0.75,-0.75)
elif magic.keyPress1 == 12 and x < magic.mapWidth and y < magic.mapHeight:
y += spd
x += spd
self.rotation = 220
self.infront = (0.75,0.75)
self.rect.topleft = (x-20, y-16) #Collision Rectangle
if not self.Collision():
self.playerpos = [x,y]
def getInfront(self):
pos = (self.playerpos[0]-8 + (self.infront[0]*self.halteentfernung),self.playerpos[1]-8 + (self.infront[1]*self.halteentfernung))
# pygame.draw.rect(magic.mapScreen, (11,31, 131), pygame.Rect(pos,(15,15)))
return pos
def Grapper(self):
# TODO add plate decition
if self.itemHolding == None: # if holding nothing
# Check if Item is in range
for i in magic.MItems:
if i.checkCollision(self.getInfront()) and not i.isOccupied:
# Get that Item
self.itemHolding = i
i.isHold = True
break
for t in magic.MTiles:
if t.checkCollision(i.pos) and i.isHold and not t.isStorage:
t.itemHolding = None
return
if t.checkCollision(self.getInfront()) and hasattr(t, "process") and t.itemHolding != None:
t.process += 1
return
elif(self.itemHolding != None): # if holding something
# Check for Crate
for t in magic.MTiles:
if t.checkCollision(self.getInfront()):
if t.placeCheck and t.itemHolding == None:
# Place the item
self.itemHolding.pos = [t.pos[0]+32,t.pos[1]+32]
t.itemHolding = self.itemHolding
self.itemHolding.isHold = False
self.itemHolding = None
return
else:
return
self.itemHolding.pos = self.getInfront()
self.itemHolding.isHold = False
self.itemHolding = None
# Bilanz
return
def Update(self):
self.Move()
if self.itemHolding is not None:
self.itemHolding.setPos(self.getInfront())
self.Draw()
pos = self.getInfront()
# pygame.draw.rect(magic.mapScreen, (20,50,231), pygame.Rect((pos[0],pos[1]),(20,20)))