Python pygame Detect if mouse is over non transparent part of surface - python

I am trying to make a UI for my game and there are some curves to the UI. Now I can detect collision between two surfaces. I can detect by pixel between two sprites, but it seems mouse detection by pixel is alluding me. Basically I want to detect when the mouse is over the UI and then ignore everything below that while getting the UI.
This is a picture of what I have so far. If you notice the pink square the mouse is over the GUI while the yellow selector box is over a tile. The yellow selector is a box frame over a tile.
I am using pygame with openGL but at this point I am looking for ANY solution to this. I can adapt pretty easily as I am not new to programming and pretty much looking for any solution.
Also I would post the code but to much code to post so if thing specific is needed let me know.
One thing to note is that the GUI is flexable in that the upper left area will slide in and out. Also the white is just placeholder so final colors are not used and would be difficult to check. Is it possible to get the surface elements under the mouse when clicked by z order?
Texture
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
class Texture(object):
image = None
rect = None
src = ''
x = 0
y = 0
'''
zOrder Layers
0 - background
1 -
2 -
3 - Tile Selector
s - Tiles
5 -
6 -
7 - Panels
8 - Main Menu
9 - GUI Buttons
10 -
'''
def __init__(self, src):
self.src = src
self.image = pygame.image.load(src)
self.image.set_colorkey(pygame.Color(255,0,255,0))
self.rect = self.image.get_rect()
texdata = pygame.image.tostring(self.image,"RGBA",0)
# create an object textures
self.texid = glGenTextures(1)
# bind object textures
glBindTexture(GL_TEXTURE_2D, self.texid)
# set texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
# Create texture image
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,self.rect.w,self.rect.h,0,GL_RGBA,GL_UNSIGNED_BYTE,texdata)
self.newList = glGenLists(2)
glNewList(self.newList, GL_COMPILE)
glBindTexture(GL_TEXTURE_2D, self.texid)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex3f(0, 0 ,0)
glTexCoord2f(0, 1); glVertex3f(0, self.rect.h, 0)
glTexCoord2f(1, 1); glVertex3f(self.rect.w, self.rect.h, 0)
glTexCoord2f(1, 0); glVertex3f(self.rect.w, 0, 0)
glEnd()
glEndList()
def getImg(self):
return self.image
def getPos(self):
rect = self.getImg().get_rect()
pos = dict(x=self.x,y=self.y,w=rect[2],h=rect[3])
return pos
def draw(self,x,y,rotate=0):
glLoadIdentity()
self.x = int(x)
self.y = int(y-self.rect.h+32)
glTranslatef(x,y-self.rect.h+32,0)
glPushAttrib(GL_TRANSFORM_BIT)
glMatrixMode(GL_TEXTURE)
glLoadIdentity()
glRotatef(rotate,0,0,1)
glPopAttrib()
if glIsList(self.newList):
glCallList(self.newList)
gui Class
import hashlib, string, pygame
from classes.texture import Texture
'''
Created on Jun 2, 2013
#author: Joel
'''
class gui(object):
INSTANCES = 0 # Count of instances of buildings
ID = 0 # Building ID
TYPE = 0 # Building type
NAME = '' # name of Building
DESCRIPTION = '' # Description of building
IMAGE = '' # Image name of building
zOrder = 0
clickable = True
def __init__(self, Game, name = 'Building', description = '', image = 'panel'):
self.INSTANCES += 1
self.setName(name)
self.setDescription(description)
self.setImage(Game, Game.SETTING["DIR"]["IMAGES"] + Game.SETTING["THEME"] + '\\gui\\'+image+'.png')
self.setType(name.lower())
self.setZ(6)
def getDescription(self):
return self.DESCRIPTION
def setDescription(self, description):
self.DESCRIPTION = description
def getID(self):
return self.ID
def setID(self, i):
allchr = string.maketrans('','')
nodigits = allchr.translate(allchr, string.digits)
s = hashlib.sha224(i).hexdigest()
s = s.translate(allchr, nodigits)
self.ID = s[-16:]
def getImage(self):
return self.IMAGE
def setImage(self, Game, i):
self.IMAGE = Texture(Game.CWD + '\\' + i)
def getName(self):
return self.NAME
def setName(self, name):
self.NAME = name
def getType(self):
return self.TYPE
def setType(self, t):
self.TYPE = t
def click(self, x, y):
if pygame.mouse.get_pressed()[0] == 1:
if x > self.x and x < (self.x + self.rect.w):
if y > self.y and y < (self.y + self.rect.h):
print("Clicked: " + str(self.x) + ', ' + str(self.y) + ', ' + str(self.rect.w) + ', ' + str(self.rect.y))
def getClickable(self):
return self.clickable
def setClickable(self, c):
self.clickable = c
def getZ(self):
return self.zOrder
def setZ(self, z):
self.zOrder = z

You could create a mask of the UI (this would be easiest if the UI is contained in one surface which is then applied to the screen surface), and set the threshold of the mask to the appropriate value so that your transparent pixels are set to 0 in the mask.
http://www.pygame.org/docs/ref/mask.html#pygame.mask.from_surface
With the mask object's get_at((x,y)) function you can test if a specific pixel of the mask is set (a non-zero value is returned if the pixel is set).
http://www.pygame.org/docs/ref/mask.html#pygame.mask.Mask.get_at
If you pass in the mouse's position, you can verify that it is over a visible part of the UI if you receive a non-zero value.

Two possible answers:
1) Statically create a 2D array of True or False that is as big as the screen - True if clicking here would click on the UI, False if clicking here would not click on the UI. Test clicks against the position in this array.
2) Use the 'paint and check' algorithm (don't recall the real name). You know how when you draw to the screen you draw the background, then background objects, then foreground objects? You can use a similar trick to detect what object you have clicked on - draw the background in one solid colour, each object in another solid colour, each UI element in another solid colour, etc... and as long as each solid colour is unique, you can test what colour pixel is under the cursor in this buffer and use it to determine what was visible and clicked on by the mouse.

Okay I am thinking of this as the best option rather then some of the alternatives. Will keep everyone up to date if this works or not.
global click variable to store data in a dict
Objects have layer variable ranging from 1 to ? from lowest to greatest layer(similar to html zIndex)
Primary Loop
reset the global click var
click event get position
loop over clickable objects to get everything under mouse
loop over everything under mouse to get highest layer
Return for global click var
run click code in object.
Layer organization currently which can be modified.
zOrder Layers
background
na
Tiles
Tile Selector
na
na
Panels
Main Menu
GUI Buttons
na
Loop
for i in range(len(self.OBJECTS)):
#img = Texture(see op)
img = self.OBJECTS[i].IMAGE
print(img)
e = None
if self.OBJECTS[i].zOrder == 4: # is isometric image
# tx and ty are translated positions for screen2iso. See Below
if ((self.tx >= 0 and self.tx < self.SETTING['MAP_WIDTH']) and (self.ty >= 0 and self.ty < self.SETTING['MAP_HEIGHT'])):
# map_x and map_y are starting points for the map its self.
ix, iy = self.screen2iso(
(x - (self.map_x + (self.SETTING['TILE_WIDTH'] / 2))),
(y - (self.map_y))
)
imgx, imgy = self.screen2iso(
(img.x - (self.map_x + (self.SETTING['TILE_WIDTH'] / 2))),
(img.y - (self.map_y))
)
if (imgx+2) == ix:
if (imgy+1) == iy:
e = self.OBJECTS[i]
else:
continue
else:
continue
else: # Not an isometric image
if x > img.x and x < (img.x + img.rect[2]):
if y > img.y and y < (img.y + img.rect[3]):
#is click inside of visual area of image?
if self.getCordInImage(x, y, self.OBJECTS[i].IMAGE):
if self.getAlphaOfPixel(self.OBJECTS[i]) != 0:
e = self.OBJECTS[i]
else:
continue
else:
continue
else:
continue
if e != None:
if self.CLICKED['zOrder'] < e.getZ():
self.CLICKED['zOrder'] = e.getZ()
self.CLICKED['e'] = e
else:
continue
else:
continue
getCordInImage
def getCordInImage(self, x, y, t):
return [x - t.x, y - t.y]
getAlphaOfPixel
def getAlphaOfPixel(self, t):
mx,my = pygame.mouse.get_pos()
x,y = self.getCordInImage(mx,my,t.IMAGE)
#mask = pygame.mask.from_surface(t.IMAGE.image)
return t.IMAGE.image.get_at([x,y])[3]
screen2iso
def screen2iso(self, x, y):
x = x / 2
xx = (y + x) / (self.SETTING['TILE_WIDTH'] / 2)
yy = (y - x) / (self.SETTING['TILE_WIDTH'] / 2)
return xx, yy
iso2screen
def iso2screen(self, x, y):
xx = (x - y) * (self.SETTING['TILE_WIDTH'] / 2)
yy = (x + y) * (self.SETTING['TILE_HEIGHT'] / 2)
return xx, yy

Related

How to use time.sleep() to draw circles Tkinter?

I wrote a program to explore Tkinter & try out object-oriented programming. My goal is to draw concentric circles, starting with the outside and moving in.
The drawing works fine, but my time-delay between circles isn't working. I can see the count-down (with print) but it doesn't draw anything until after the count-down ends.
Possibly this is related to the creation of the object? Nothing happens until the object is finished being created? IDK.
Here's my code:
import tkinter as tk
import time
root = tk.Tk()
size = 1000
myCanvas = tk.Canvas(root, bg="white", height=size, width=size)
# draw circle
class Circle:
def __init__(self, rt, dia, color, x=0, y=0):
self.rt = rt
self.dia = dia
self.color = color
self.x = x # center cord x
self.y = y # center cord y
def draw_circle(self):
r = self.dia / 2
up_left = (self.x - r, self.y + r)
low_right = (self.x + r, self.y - r)
cord = up_left + low_right
self.rt.create_oval(cord, fill=self.color, outline="")
coord2 = 0, 300, 300, 0
#arc = myCanvas.create_oval(coord2, fill="blue")
def PickColor(r, g, b):
r = r % 250
g = g % 250
b = b % 250
return('#%02x%02x%02x' % (r, g, b))
class ConcentricCircles:
def __init__(self, rt, quantity):
self.rt = rt
self.quantity = quantity
def draw_circles(self):
q = self.quantity
circles = []
i = 0
for c in range(q, 1, -1):
time.sleep(0.005)
incr = size/(1.5*q-0.001*c*c*c)
print(c)
circles += [Circle(self.rt, incr*c, PickColor(110, 15*c^3-c^2, 300*c^5-c^4), size/2, size/2)]
circles[i].draw_circle()
i += 1
self.rt.pack()
a = ConcentricCircles(myCanvas, 30).draw_circles()
root.mainloop()
Here's what it draws:
When you use the sleep() function, the application suspends updates to the GUI. This means that the drawing of circles is also suspended. But you can force the application to update the GUI before it continues with update_idletasks(), see example below. I chose to make the update in the Circle.draw_circle() function:
def draw_circle(self):
r = self.dia / 2
up_left = (self.x - r, self.y + r)
low_right = (self.x + r, self.y - r)
cord = up_left + low_right
self.rt.create_oval(cord, fill=self.color, outline="")
self.rt.update_idletasks() # Updates the canvas
When you use sleep() the application is busy all the time it sleeps. You might want to research the after() function which schedules a function call but does not lock the app.

Click in specified place without moving mouse error

So I have been looking for multiple ways to perform a "click" without actually moving the mouse. After hours of searching, I came upon these two pages:
ctypes mouse_events
and
https://schurpf.com/python-mouse-control/
where there's some code that can apparently perform a click without moving the mouse.
The code seems to come from the same person, but https://schurpf.com/python-mouse-control/ seems more up to date with the "Added functions".
I tried fixing it for a while but didn't get anywhere and am stuck with the following script
import win32gui, win32api, win32con, ctypes
class Mouse:
"""It simulates the mouse"""
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
MOUSEEVENTF_LEFTUP = 0x0004 # left button up
MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
SM_CXSCREEN = 0
SM_CYSCREEN = 1
def _do_event(self, flags, x_pos, y_pos, data, extra_info):
"""generate a mouse event"""
x_calc = 65536 * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
y_calc = 65536 * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)
def _get_button_value(self, button_name, button_up=False):
"""convert the name of the button into the corresponding value"""
buttons = 0
if button_name.find("right") >= 0:
buttons = self.MOUSEEVENTF_RIGHTDOWN
if button_name.find("left") >= 0:
buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
if button_name.find("middle") >= 0:
buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
if button_up:
buttons = buttons << 1
return buttons
def move_mouse(self, pos):
"""move the mouse to the specified coordinates"""
(x, y) = pos
old_pos = self.get_position()
x = x if (x != -1) else old_pos[0]
y = y if (y != -1) else old_pos[1]
self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)
def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
"""push a button of the mouse"""
self.move_mouse(pos)
self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)
def click(self, pos=(-1, -1), button_name= "left"):
"""Click at the specified placed"""
## self.move_mouse(pos)
self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)
def double_click (self, pos=(-1, -1), button_name="left"):
"""Double click at the specifed placed"""
for i in xrange(2):
self.click(pos, button_name)
def get_position(self):
"""get mouse position"""
return win32api.GetCursorPos()
#-----------------------------------------------------------------------------------------
#Added functions
#-----------------------------------------------------------------------------------------
def invisible_click(self,pos=(-1, -1), button_name="left"):
"""Click in specified place without moving mouse"""
xcur,ycur = win32gui.GetCursorPos()
ctypes.windll.user32.SetCursorPos(pos[0],pos[1])
self.click(pos,button_name)
ctypes.windll.user32.SetCursorPos(xcur,ycur)
def invisible_click_rel(self,handle,pos, button_name="left"):
"""Click in window coordinates without moving mouse"""
#get window info
xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle)
xcur,ycur = win32gui.GetCursorPos()
ctypes.windll.user32.SetCursorPos(pos[0]+xleft,pos[1]+ytop)
self.click((pos[0]+xleft,pos[1]+ytop),button_name)
ctypes.windll.user32.SetCursorPos(xcur,ycur)
if __name__ == '__main__':
p = (500,500)
print (win32gui.GetForegroundWindow())
mouse = Mouse()
mouse.invisible_click(p)
What I am trying to do is trigger the invisible_click function of the Mouse class so that it performs an "invisible click" at position 500,500.
The error I am getting with the code above is
File "C:\Users\MyName\Desktop\test1del\Future.py", line 21, in _do_event
return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: Don't know how to convert parameter 2
Sorry if it's something obvious that I am missing, I would really appreciate the help!
Thanks for reading
File "C:\Users\MyName\Desktop\test1del\Future.py", line 21, in
_do_event
return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info) ctypes.ArgumentError: argument 2: : Don't know how to convert parameter 2
This error message indicate that parameters x_calc and y_calc passed in function mouse_event have wrong type. They are float but unsigned int required .
Try the following code:
import math
#...
x_calc = math.floor(65536 * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1)
y_calc = math.floor(65536 * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1)
Note: mouse_event function has been superseded. Use SendInput instead.

Displaying movement tiles for a Python game

I am trying to create a simple tactical RPG in python, similar to Fire Emblem or Advanced Wars, but in ASCII. I have been following this tutorial, which uses the libtcod module, and modifying the tutorial code for my own purposes
Right now I am struck trying to display movement tiles for each character. Basically I want the game to display the tiles each character can move to, whenever the user clicks his mouse over that character. I have created a function (get_total_squares) that generates the coordinates of the tiles to be highlighted based on the character's coordinates and movement range (hard-coded to 5, for now) and a function (show_move_tiles) to highlight these tiles. To record the click coordinates I have another function target_tile and a function to see if the coordinates match with a character's (target_character)
The program runs without errors, but clicking the mouse over a character does nothing (no tiles are shown).
Here is the code:
import libtcodpy as libtcod
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
#size of the map
MAP_WIDTH = 80
MAP_HEIGHT = 43
LIMIT_FPS = 20 #20 frames-per-second maximum
color_dark_wall = libtcod.Color(0, 0, 50)
color_dark_ground = libtcod.Color(5, 50, 0)
color_move_tile = libtcod.Color (100, 0, 0)
class Tile:
#a tile of the map and its properties
def __init__(self, blocked, block_sight = None):
self.blocked = blocked
#by default, if a tile is blocked, it also blocks sight
if block_sight is None: block_sight = blocked
self.block_sight = block_sight
def target_tile():
#return the position of a tile left-clicked, or (None,None) if right-clicked.
global key, mouse
while True:
libtcod.console_flush()
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse)
render_all()
(x, y) = (mouse.cx, mouse.cy)
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:
return (None, None) #cancel if the player right-clicked or pressed Escape
if mouse.lbutton_pressed:
return (x, y)
print "works!"
def target_character():
#Shows movement tiles if character is on click-coordinate
(x, y) = target_tile()
#return the first clicked character
for obj in objects:
if obj.x == x and obj.y == y:
obj.show_move_tiles()
def make_map():
global map
#fill map with "unblocked" tiles
map = [[ Tile(False)
for y in range(MAP_HEIGHT) ]
for x in range(MAP_WIDTH) ]
map[30][22].blocked = True
map[30][22].block_sight = True
map[50][22].blocked = True
map[50][22].block_sight = True
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, color):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx, dy):
#move by the given amount
if not map[self.x + dx][self.y + dy].blocked:
self.x += dx
self.y += dy
def draw(self):
#set the color and then draw the character that represents this object at its position
libtcod.console_set_default_foreground(con, self.color)
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def show_move_tiles(self):
global mouse
global color_move_tile
(x,y) = (mouse.cx, mouse.cy)
coord_in_range = get_total_squares(5, self.x, self.y)
for [x,y] in coord_in_range:
if [x,y] is not [self.x, self.y]:
libtcod.console_set_char_background(con, x, y, color_move_tile, libtcod.BKGND_SET)
def clear(self):
#erase the character that represents this object
libtcod.console_put_char_ex(con, self.x, self.y, '.', libtcod.white, libtcod.black)
def handle_keys():
#key = libtcod.console_check_for_keypress() #real-time
global key #turn-based
if key.vk == libtcod.KEY_ENTER and key.lalt:
#Alt+Enter: toggle fullscreen
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
elif key.vk == libtcod.KEY_ESCAPE:
return True #exit game
def get_total_squares(dist, playerx, playery):
coord_in_range = []
for x in range(-dist,dist+1):
for y in range(-dist, dist+1):
if abs(x)+abs(y) <= dist:
coord_in_range.append([playerx+x, playery+y])
return coord_in_range
def render_all():
global color_dark_wall
global color_dark_ground
#go through all tiles, and set their background color
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
wall = map[x][y].block_sight
if wall:
libtcod.console_put_char_ex(con, x, y, '#', libtcod.white, libtcod.black)
else:
libtcod.console_put_char_ex(con, x, y, '.', libtcod.white, libtcod.black)
#draw all objects in the list and show movement if mouse hovers over player
for item in objects:
item.draw()
if mouse.cx == item.x and mouse.cy == item.y:
item.show_move_tiles()
#blit the contents of "con" to the root console
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
#############################################
# Initialization & Main Loop
#############################################
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
libtcod.sys_set_fps(LIMIT_FPS)
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
#create object representing the player
player = Object(30, 15, '#', libtcod.white)
#create an NPC
npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '#', libtcod.yellow)
#the list of objects with those two
objects = [npc, player]
make_map()
mouse = libtcod.Mouse()
key = libtcod.Key()
while not libtcod.console_is_window_closed():
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse)
#render the screen
render_all()
target_tile()
libtcod.console_flush()
#erase all objects at their old locations, before they move
for object in objects:
object.clear()
#handle keys and exit game if needed
exit = handle_keys()
if exit:
break
EDIT: I solved the previous problem of the movement tiles not showing up when I hovered the mouse over the character, but have updated the problem with a new one: trying to toggle the movement tiles on and off with a mouse click

Pygame Scrolling Map

I'm making a rogue-like and I trying to make a camera follow the player as he moves by the map.
I was able to make the Draw function only happens when the tiles are inside the camera viewport[show in gray], but I can't make the camera stay in the corner of the window.
This is how it is:
And this is How it should be:
Is there a way to 'crop' the screen Surface, or perhaps copy only whats inside the camera viewport and blit it in the screen again.
Probably I'm doing this the hard way.
I'm iterating over the whole map, creating a rectangle for each tile, and checking if it's inside the Camera Viewport Rect using the '.contains()'.
EDIT:
This is how I'm drawing the map:
for x in xrange(mymap.width):
for y in xrange(mymap.height):
lit = field_of_view.lit(x, y)
visited = field_of_view.visited(x, y)
graphic = mymap.tileAt(x, y).graphic
if lit:
color = mymap.tileAt(x, y).color
elif visited:
color = GRAY
else:
color = BLACK
renderedgraphic = myfont.render(graphic, 1, color)
screen.blit(renderedgraphic, (x*TILESIZE, y*TILESIZE))
I do the same thing for the player, monsters, items and etc, but everything in it's own classmethod.
My camera is set like this:
class Camera(Object):
def __init__(self, x, y):
graphic = ''
Object.__init__(self, graphic, x, y)
self.rect = pygame.Rect(x, y, CAMERA_WIDTH * 2 + 5, CAMERA_HEIGHT * 2 + 5)
def update(self):
self.x = PLAYER.x
self.y = PLAYER.y
startx = ((self.x * TILESIZE) - CAMERA_WIDTH) + 5
starty = ((self.y * TILESIZE) - CAMERA_HEIGHT) + 5
self.rect = pygame.Rect(startx, starty, CAMERA_WIDTH * 2 + 5, CAMERA_HEIGHT * 2 + 5)
So I tried what user3762084 said.
in short:
for x in xrange(mymap.width):
for y in xrange(mymap.height):
... # do fov stuff
tile = Tile.At(x, y) # get tile instance
if tile:
tile.update() # update it's relative position
screen.blit(renderedgraphic, (tile.relX * TILESIZE, tile.relX * TILESIZE)) # blit the postion to it's relative position
This is what happens:
It's all squished in the side of the window. And if the player moves it all goes black.
What I have done before to make a scrolling environment was to give each object its coordinates in the world, and then give your camera a position. When you are drawing, you then calculate the position of each object on the screen based on the object's actual coordinates and the camera's coordinates.
class Tile:
def __init__(self, x, y, other_variables):
self.x = x
self.y = y
# relative vars for later
self.relX = None
self.relY = None
# then your camera should have a position as well as its width and height:
class Camera:
def __init__(self, x, y, width, height):
# assign those variables here
# your drawing function:
for tile in tiles:
tile.relX = tile.x - camera.x
tile.relY = tile.y - camera.y
# blit your tiles to the screen at the relative coordinates
In addition, you could also implement checks to see if the tile is completely outside of the camera space and not draw those tiles.
Ok I got what I was looking for, I did a litte hack tho. So if anyone have a better way to do this.
I changede the way my map was drawing
I took the Camera Rect positions [topleft and bottomright];
Converted it to World Position;
Iterate over it, with a enumerator too;
Did any lit/visited FOG calcunations with X and Y;
And Blited in the screen using the enumerators 'i' and 'j'.
Here's the code:
topleft = Map.toWorld(camera.rect.topleft)
bottomright = Map.toWorld(camera.rect.bottomright)
for i, x in enumerate(xrange(topleft[0], bottomright[0])):
for j, y in enumerate(xrange(topleft[1], bottomright[1])):
tile = mymap.tileAt(x, y)
object = [obj for obj in Object.OBJECTS if obj.pos == (x,y)]
if tile:
lit = field_of_view.lit(x, y)
visited = field_of_view.visited(x, y)
graphic = tile.graphic
if lit:
color = tile.color
elif visited:
color = GRAY
else:
color = BLACK
renderedgraphic = myfont.render(ch, 1, graphic)
screen.blit(renderedgraphic, Map.toScreen((i + 1, j)))
if object:
Draw.drawObject(object[0], Map.toScreen((i + 1, j)))
The only issue now is when the player is at the sides of the map it shows a black border.

Display a certain element in a list

Right now our code creates a grid starting at the top left and filling in rows and columns from left to right, row by row. Currently, there are a bunch of images it can pick from. It is set using a handful of IF statements that picks between shapes and rareshapes. What I am trying to figure out how to do is change the code so instead of it picking a random rareshape, I can decide what rareshape spawns. Still new to Python and finding a lot of little things that make sense to me from other languages don't work in Python so its throwing me off a little.
EDIT:
Here is the full code. Credit for the base code written by cactusbin and revised by Gareth Rees.
import pygame, random, time, sys
from pygame.locals import *
import itertools
import os
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
SHAPE_WIDTH = 64 # Width of each shape (pixels).
SHAPE_HEIGHT = 64 # Height of each shape (pixels).
PUZZLE_COLUMNS = 10 # Number of columns on the board.
PUZZLE_ROWS = 11 # Number of rows on the board.
MARGIN = 118 # Margin around the board (pixels).
WINDOW_WIDTH = PUZZLE_COLUMNS * SHAPE_WIDTH + 2 * MARGIN + 485
WINDOW_HEIGHT = PUZZLE_ROWS * SHAPE_HEIGHT + 2 * MARGIN - 150
FONT_SIZE = 60
TEXT_OFFSET = MARGIN + 950
# Map from number of matches to points scored.
MINIMUM_MATCH = 10
EXTRA_LENGTH_POINTS = .1
RANDOM_POINTS = .3
DELAY_PENALTY_SECONDS = 1
DELAY_PENALTY_POINTS = 0
FPS = 30
EXPLOSION_SPEED = 15 # In frames per second.
SPIN_SPEED = 15
REFILL_SPEED = 10 # In cells per second.
VERTICAL = False
class Cell(object):
"""
A cell on the board, with properties:
`image` -- a `Surface` object containing the sprite to draw here.
`offset` -- vertical offset in pixels for drawing this cell.
"""
def __init__(self, image):
self.offset = 0.0
self.image = image
def tick(self, dt):
self.offset = max(0.0, self.offset - dt * REFILL_SPEED)
class Board(object):
"""
A rectangular board of cells, with properties:
`w` -- width in cells.
`h` -- height in cells.
`size` -- total number of cells.
`board` -- list of cells.
`matches` -- list of matches, each being a list of exploding cells.
`refill` -- list of cells that are moving up to refill the board.
`score` -- score due to chain reactions.
"""
def __init__(self, width, height):
self.explosion = [pygame.image.load('images/explosion{}.png'.format(i))
for i in range(1, 7)]
self.spin = [pygame.image.load('images/powerframe{}.png'.format(i))
for i in range (1, 12)]
self.image_color = {}
self.shapes = []
self.rareshapes = []
colors = 'red blue yellow'
letters = 'acgtu'
for c in colors.split():
im = pygame.image.load('images/{}.png'.format(c))
self.shapes.append(im)
self.image_color[im] = c
for l in letters:
im = pygame.image.load('rareimages/{}{}.png'.format(c, l))
self.rareshapes.append(im)
self.image_color[im] = l
self.background = pygame.image.load("images/bg.png")
self.blank = pygame.image.load("images/blank.png")
self.x = pygame.image.load("images/x.png")
self.w = width
self.h = height
self.size = width * height
self.board = [Cell(self.blank) for _ in range(self.size)]
self.matches = []
self.refill = []
self.score = 0.0
self.spin_time = 15
def randomize(self):
"""
Replace the entire board with fresh shapes.
"""
rare_shapes = [1, 9, 23, 27, 40, 42, 50, 56, 70, 81, 90]
for i in range(self.size):
if i in rare_shapes:
self.board[i] = Cell(random.choice(self.rareshapes))
else:
self.board[i] = Cell(random.choice(self.shapes))
def pos(self, i, j):
"""
Return the index of the cell at position (i, j).
"""
assert(0 <= i < self.w)
assert(0 <= j < self.h)
return j * self.w + i
def busy(self):
"""
Return `True` if the board is busy animating an explosion or a
refill and so no further swaps should be permitted.
"""
return self.refill or self.matches
def tick(self, dt):
"""
Advance the board by `dt` seconds: move rising blocks (if
any); otherwise animate explosions for the matches (if any);
otherwise check for matches.
"""
if self.refill:
for c in self.refill:
c.tick(dt)
self.refill = [c for c in self.refill if c.offset > 0]
if self.refill:
return
elif self.matches:
self.explosion_time += dt
f = int(self.explosion_time * EXPLOSION_SPEED)
if f < len(self.explosion):
self.update_matches(self.explosion[f])
return
self.update_matches(self.blank)
self.refill = list(self.refill_columns())
self.explosion_time = 0
self.matches = self.find_matches()
def draw(self, display):
"""
Draw the board on the pygame surface `display`.
"""
display.blit(self.background, (0, 0))
for i, c in enumerate(self.board):
display.blit(c.image,
(MARGIN + SHAPE_WIDTH * (i % self.w),
MARGIN + SHAPE_HEIGHT * (i // self.w - c.offset) - 68))
display.blit(self.x, (995, 735))
display.blit(self.x, (1112, 735))
display.blit(self.x, (1228, 735))
def swap(self, cursor):
"""
Swap the two board cells covered by `cursor` and update the
matches.
"""
i = self.pos(*cursor)
b = self.board
b[i], b[i+1] = b[i+1], b[i]
self.matches = self.find_matches()
def find_matches(self):
"""
Search for matches (lines of cells with identical images) and
return a list of them, each match being represented as a list
of board positions.
"""
def lines():
for j in range(self.h):
yield range(j * self.w, (j + 1) * self.w)
for i in range(self.w):
yield range(i, self.size, self.w)
def key(i):
return self.image_color.get(self.board[i].image)
def matches():
for line in lines():
for _, group in itertools.groupby(line, key):
match = list(group)
if len(match) >= MINIMUM_MATCH:
yield match
self.score = self.score + 1
return list(matches())
def update_matches(self, image):
"""
Replace all the cells in any of the matches with `image`.
"""
for match in self.matches:
for position in match:
self.board[position].image = image
def refill_columns(self):
"""
Move cells downwards in columns to fill blank cells, and
create new cells as necessary so that each column is full. Set
appropriate offsets for the cells to animate into place.
"""
for i in range(self.w):
target = self.size - i - 1
for pos in range(target, -1, -self.w):
if self.board[pos].image != self.blank:
c = self.board[target]
c.image = self.board[pos].image
c.offset = (target - pos) // self.w
target -= self.w
yield c
offset = 1 + (target - pos) // self.w
for pos in range(target, -1, -self.w):
c = self.board[pos]
c.image = random.choice(self.shapes)
c.offset = offset
yield c
class Game(object):
"""
The state of the game, with properties:
`clock` -- the pygame clock.
`display` -- the window to draw into.
`font` -- a font for drawing the score.
`board` -- the board of cells.
`cursor` -- the current position of the (left half of) the cursor.
`score` -- the player's score.
`last_swap_ticks` --
`swap_time` -- time since last swap (in seconds).
"""
def __init__(self):
pygame.init()
pygame.display.set_caption("Nucleotide")
self.clock = pygame.time.Clock()
self.display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT),
DOUBLEBUF)
self.board = Board(PUZZLE_COLUMNS, PUZZLE_ROWS)
self.font = pygame.font.Font(None, FONT_SIZE)
def start(self):
"""
Start a new game with a random board.
"""
self.board.randomize()
self.cursor = [0, 0]
self.score = 0.0
self.swap_time = 125
def quit(self):
"""
Quit the game and exit the program.
"""
pygame.quit()
sys.exit()
def play(self):
"""
Play a game: repeatedly tick, draw and respond to input until
the QUIT event is received.
"""
self.start()
while True:
self.draw()
dt = min(self.clock.tick(FPS) / 1000, 1 / FPS)
self.swap_time -= dt
for event in pygame.event.get():
if event.type == KEYUP:
self.input(event.key)
elif event.type == QUIT:
self.quit()
elif self.swap_time == 0:
self.quit()
self.board.tick(dt)
def input(self, key):
"""
Respond to the player pressing `key`.
"""
if key == K_q:
self.quit()
elif key == K_RIGHT and self.cursor[0] < self.board.w - 2:
self.cursor[0] += 1
elif key == K_LEFT and self.cursor[0] > 0:
self.cursor[0] -= 1
elif key == K_DOWN and self.cursor[1] < self.board.h - 1:
self.cursor[1] += 1
elif key == K_UP and self.cursor[1] > 0:
self.cursor[1] -= 1
elif key == K_SPACE and not self.board.busy():
self.swap()
def swap(self):
"""
Swap the two cells under the cursor and update the player's score.
"""
self.board.swap(self.cursor)
def draw(self):
self.board.draw(self.display)
self.draw_score()
self.draw_time()
if VERTICAL == False:
self.draw_cursor()
elif VERTICAL == True:
self.draw_cursor2()
pygame.display.update()
def draw_time(self):
s = int(self.swap_time)
text = self.font.render(str(int(s/60)) + ":" + str(s%60).zfill(2),
True, BLACK)
self.display.blit(text, (TEXT_OFFSET, WINDOW_HEIGHT - 170))
def draw_score(self):
total_score = self.score
def draw_cursor(self):
topLeft = (MARGIN + self.cursor[0] * SHAPE_WIDTH,
MARGIN + self.cursor[1] * SHAPE_HEIGHT - 68)
topRight = (topLeft[0] + SHAPE_WIDTH * 2, topLeft[1])
bottomLeft = (topLeft[0], topLeft[1] + SHAPE_HEIGHT)
bottomRight = (topRight[0], topRight[1] + SHAPE_HEIGHT)
pygame.draw.lines(self.display, WHITE, True,
[topLeft, topRight, bottomRight, bottomLeft], 3)
if __name__ == '__main__':
Game().play()
If what you are asking for is a way to more easily specify at which rareshapecount intervals you should place a rare shape instead of a normal shape, the following approach is more readable:
def randomize(self):
"""
Replace the entire board with fresh shapes.
"""
# locations we want to place a rare shape
rare_shapes = [9, 23, 27]
for i in range(self.size):
if i in rare_shapes:
self.board[i] = Cell(random.choice(self.rareshapes))
else:
self.board[i] = Cell (random.choice(self.shapes))
Optionally, you could randomly populate rare_shapes if you don't feel like hardcoding the intervals each time, making for a more varied experience (i.e., if you're designing a game or something similar).
What you mean by "I can decide what rareshape spawns instead of it picking a random rareshape" is unclear to me. Would you care to give more explanations ? Like how you would tell the program which rareshape to use ?
In the meantime, here's a somewhat more pythonic version of your code:
def randomize(self):
"""
Replace the entire board with fresh shapes.
"""
specials = dict((x, self.rareshapes) for x in (9, 23, 27))
get_shape_source = lambda x: specials.get(x, self.shapes)
for i in xrange(min(self.size, 41)):
self.board[i] = Cell(random.choice(get_shape_source(i)))
Note that this would break if len(self.board) < min(self.size, 41) but well, that's still basically what your current code do.
edit: given your comment, the obvious way to explicitly choose which rareshape goes where is to explicitly associate images with spots. Now what's the best way to do so / the best place ton configure this really depends on your whole code or at least on more than what you posted. As a very simple and minimal exemple, you could just have this:
from collections import ordereddict
def load_images(self)
self.image_color = {}
self.shapes = []
self.rareshapes = ordereddict()
colors = 'red', 'blue', 'yellow'
letters = 'acgtu'
for c in colors:
im = pygame.image.load('images/{}.png'.format(c))
self.shapes.append(im)
self.image_color[im] = c
for l in letters:
im = pygame.image.load('rareimages/{}{}.png'.format(c, l))
self.rareshapes.[(c, l)] = im
self.image_color[im] = l
def randomize(self):
"""
Replace the entire board with fresh shapes.
"""
raremap = {
# spot index : rareshape
9: ('red', 'a')],
23: ('blue', 'u'),
27: ('yellow', 'g')
}
for i in xrange(self.size):
if i in raremap:
im = self.rareshapes[raremap[i]]
else:
im = random.choice(self.shapes)
self.board[i] = Cell(im)
But it will be just unmaintainable in the long run - too much hardcoded stuff, and too much knowledge leaking from one method to another. I don't know what 'self' is an instance of, but you should considered splitting the responsabilities so you have the invariant parts in one class and the "configration" (images to load, spots / rareshapes mapping etc) in another. Some design patterns that come to mind are TemplateMethod (where you have an abstract base class with the invariant parts and concrete subclasses implementing the "configuration" part), Builder, and of course Strategy (in your case the "Strategy" class would take care of the configuration).

Categories