Skip to content
Snippets Groups Projects
Commit 963d2012 authored by Leo Moe's avatar Leo Moe
Browse files

worked on button-presses, doesnt work well yet

parent fda6b132
No related branches found
No related tags found
No related merge requests found
I figure it could be cool to make a map in which two characters move randomly.
The first plan is to have the map which is a class that has both a grid and a few tuples that are these dots.
\ No newline at end of file
Now I have a few squares that move around. In addition to this, I should have my own character that moves around.
Then, I will make a log-in screen.
I am guessing that I make a class that is Drawing and has the function draw.
I dont know how it will do with sizes and stuff like that.
It isn't all that easy to know.
I may have to learn how inheritance works in python.
Link in here: https://www.w3schools.com/python/python_inheritance.asp
At some point, I will have a character that moves around and a score.
Then, I will be able to start playing and things will be even cooler.
\ No newline at end of file
No preview for this file type
......@@ -11,12 +11,13 @@ class Map:
self.xSize=xSize
self.ySize=ySize
self.playerArray=[self.randomPosition() for n in range(playerAmount)]
self.playerPos=self.randomPosition()
#this feels like short code but it also makes sense.
def randomPosition(self)->tuple[int,int]:
xPos=randint(0,self.xSize-1)
yPos=randint(0,self.ySize-1)
#I add the -1 because randint adds the last (which is stupid but whatever)
#I add the -1 because randint adds the last number in the range.
return (xPos,yPos)
def positionLegal(self,y,x):#I say y before x for practical reasons.
......@@ -25,16 +26,25 @@ class Map:
#I havent because this is very readable.
#the goal is that the update just calls this one for everything in the self.playerArray
def update(self):
def update(self,move=-1):
movements=[(0,1),(0,-1),(1,0),(-1,0)]#no particular order of these.
if move>=0:
newPos=add(self.playerPos,movements[move])
if self.positionLegal(newPos[0],newPos[1]):
self.playerPos=newPos
for ind,point in enumerate(self.playerArray):
position=add(point,choice(movements))
while not self.positionLegal(position[0],position[1]):#worst-case: O(inf).
position=add(point,choice(movements))
self.playerArray[ind]=position
#Damn I am guessing that this method has a lot of bad about it lol at least in running time.
#I chose this algorithm because I think removing things from the list is a bad idea.
def getPlayers(self)->list[tuple[int,int]]:
return self.playerArray
#I think the next goal is to build a character that moves around as well.
#To do this, I need the map to store the tuple as well.
#I could even build a login screen and make that one work well too.
\ No newline at end of file
......@@ -2,6 +2,9 @@ import pygame as pg
from random import randint
import Map
def randomColor():return (randint(0,255),randint(0,255),randint(0,255),randint(0,255))
def clearScreen(screen:pg.Surface):
......@@ -28,6 +31,7 @@ def displayMap(screen:pg.Surface,map:Map.Map):
pg.draw.rect(screen,"brown",pg.Rect(x*xWidth,y*yWidth,xWidth,yWidth))
for tuple in map.getPlayers():
pg.draw.rect(screen,"green",pg.Rect(tuple[0]*xWidth,tuple[1]*yWidth,xWidth,yWidth))
pg.draw.rect(screen,"cyan",pg.Rect(map.playerPos[0]*xWidth,map.playerPos[1],xWidth,yWidth))
......@@ -41,11 +45,39 @@ def run():
done=False
while not done:
for event in pg.event.get():
#print(event.type)
#this is some really garbage code but I am working on it at least.
if event.type==pg.QUIT:
done=True
if event.type==pg.KEYDOWN:
print("button pressed!")
print(event.key)
if event.key==1073741903:
print("right")
aMap.update(0)
if event.key==1073741904:
print("left")
aMap.update(1)
if event.key==1073741906:
print("up")
aMap.update(2)
if event.key==1073741905:
print("down")
aMap.update(3)
#I know that this isn't the most readable code and I will work on it.
#Something I need to think about:
#I think if's are probably bad but I dont know. I need to know more about this.
else: aMap.update(-1)
#I think I like the codes and they make sense with the positions in the arrays.
#The challenge is just that it isn't consistent.
displayMap(screen,aMap)
aMap.update()
clock.tick(1)
pg.display.update()
run()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment