#example snake snake = [[1, 2], [1, 3], [2, 3]]
def draw():
canvas.delete('all')
for segment in snake:
y = segment[0] * 10
x = segment[1] * 10
canvas.create_rectangle(x, y, x + 10, y + 10, fill="red")
canvas.update()
I have created a simple snake game in python using tkinter but the movement slows down rapidly when the snake array contains 30+ rectangles, so i was wondering is there a better way to do the drawing of the objects so it works faster instead of constantly calling this draw function?
Another possible issue is the inefficiency of the move functions:
def move_right(event):
global left, right, up, down
if right != True:
left, right, up, down = False, True, False, False
while right == True:
if snake[0][1] >= snake[1][1]:
for x in range(len(snake) - 1, 0, -1):
snake[x] = snake[x - 1]
snake[0] = [snake[0][0], snake[0][1] + 1]
draw()
time.sleep(0.05)
This my first actual game so don't kill me :(.
You don't have to delete all rectangles from canvas. You have to remove only last rectangle and add new head rectangle.
To add new x,y in snake list you don't have to move all elements - you need only snake.insert(0, [new_x, new_y])
You can use root.after instead of while loop and sleep
Example - without checking collisions
import tkinter as tk
# === constants ===
BLOCK_SIZE = 10
TIME = 50
# === functions ===
# create all rectangles on canvas
def create_snake(canvas, snake):
snake_rect = []
for x, y in snake:
x1 = x * BLOCK_SIZE
y1 = y * BLOCK_SIZE
x2 = x1 + BLOCK_SIZE
y2 = y1 + BLOCK_SIZE
rect = canvas.create_rectangle(x1,y1,x2,y2, fill='red')
snake_rect.append(rect)
return snake_rect
# move snake - add first rectangle and remove last one
def move(canvas, snake, snake_rect, remove_last=True):
# get head
x, y = snake[0]
# new head position
if direction == 'up':
y = y-1
elif direction == 'down':
y = y+1
elif direction == 'left':
x = x-1
elif direction == 'right':
x = x+1
# add first - new head
snake.insert(0, [x, y])
x1 = x * BLOCK_SIZE
y1 = y * BLOCK_SIZE
x2 = x1 + BLOCK_SIZE
y2 = y1 + BLOCK_SIZE
rect = canvas.create_rectangle(x1,y1,x2,y2, fill='red')
snake_rect.insert(0, rect)
# remove last - tail (if snake doesn't eat 'apple')
if remove_last:
del snake[-1]
canvas.delete(snake_rect[-1])
del snake_rect[-1]
# call `move` function again after TIME miliseconds
root.after(TIME, move, canvas, snake, snake_rect)
# change direction
def change_direction(new_direction):
global direction
#print(new_direction)
if new_direction == 'left':
if direction != 'right':
direction = new_direction
elif new_direction == 'right':
if direction != 'left':
direction = new_direction
elif new_direction == 'up':
if direction != 'down':
direction = new_direction
elif new_direction == 'down':
if direction != 'up':
direction = new_direction
# === main ===
direction = 'up'
# ---
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
# create long (curved) snake
snake = [[x,25] for x in range(10,35)] + [[35, y] for y in range(25, 1, -1)] + [[x, 1] for x in range(35, 1, -1)]
snake_rect = create_snake(canvas, snake)
# call `move` function after TIME miliseconds
root.after(TIME, move, canvas, snake, snake_rect)
# bind arrows to change snake direction
root.bind('<Left>', lambda event:change_direction('left'))
root.bind('<Right>', lambda event:change_direction('right'))
root.bind('<Up>', lambda event:change_direction('up'))
root.bind('<Down>', lambda event:change_direction('down'))
# start program
root.mainloop()
Related
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT, RESET_SURF, RESET_RECT, NEW_SURF, NEW_RECT, SOLVE_SURF, SOLVE_RECT
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Slide Puzzle')
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
# Store the option buttons and their rectangles in OPTIONS.
RESET_SURF, RESET_RECT = makeText('Reset', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 90)
NEW_SURF, NEW_RECT = makeText('New Game', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 60)
SOLVE_SURF, SOLVE_RECT = makeText('Solve', TEXTCOLOR, TILECOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 30)
mainBoard, solutionSeq = generateNewPuzzle(80)
SOLVEDBOARD = getStartingBoard() # a solved board is the same as the board in a start state.
allMoves = [] # list of moves made from the solved configuration
while True: # main game loop
slideTo = None # the direction, if any, a tile should slide
msg = 'Click tile or press arrow keys to slide.' # contains the message to show in the upper left corner.
if mainBoard == SOLVEDBOARD:
msg = 'Solved!'
drawBoard(mainBoard, msg)
checkForQuit()
for event in pygame.event.get(): # event handling loop
if event.type == MOUSEBUTTONUP:
spotx, spoty = getSpotClicked(mainBoard, event.pos[0], event.pos[1])
if (spotx, spoty) == (None, None):
# check if the user clicked on an option button
if RESET_RECT.collidepoint(event.pos):
resetAnimation(mainBoard, allMoves) # clicked on Reset button
allMoves = []
elif NEW_RECT.collidepoint(event.pos):
mainBoard, solutionSeq = generateNewPuzzle(80) # clicked on New Game button
allMoves = []
elif SOLVE_RECT.collidepoint(event.pos):
resetAnimation(mainBoard, solutionSeq + allMoves) # clicked on Solve button
allMoves = []
else:
# check if the clicked tile was next to the blank spot
blankx, blanky = getBlankPosition(mainBoard)
if spotx == blankx + 1 and spoty == blanky:
slideTo = LEFT
elif spotx == blankx - 1 and spoty == blanky:
slideTo = RIGHT
elif spotx == blankx and spoty == blanky + 1:
slideTo = UP
elif spotx == blankx and spoty == blanky - 1:
slideTo = DOWN
elif event.type == KEYUP:
# check if the user pressed a key to slide a tile
if event.key in (K_LEFT, K_a) and isValidMove(mainBoard , LEFT):
slideTo = LEFT
elif event.key in (K_RIGHT, K_d) and isValidMove(mainBoard , RIGHT):
slideTo = RIGHT
elif event.key in (K_UP, K_w) and isValidMove(mainBoard , UP):
slideTo = UP
elif event.key in (K_DOWN, K_s) and isValidMove(mainBoard , DOWN):
slideTo = DOWN
if slideTo:
slideAnimation(mainBoard, slideTo, 'Click tile or press arrow keys to slide.', 8) # show slide on screen
makeMove(mainBoard, slideTo)
allMoves.append(slideTo) # record the slide
pygame.display.update()
FPSCLOCK.tick(FPS)
def terminate():
pygame.quit()
sys.exit()
def checkForQuit():
for event in pygame.event.get(QUIT): # get all the QUIT events
terminate() # terminate if any QUIT events are present
for event in pygame.event.get(KEYUP): # get all the KEYUP events
if event.key == K_ESCAPE:
terminate() # terminate if the KEYUP event was for the Esc key
pygame.event.post(event) # put the other KEYUP event objects back
def getStartingBoard():
# Return a board data structure with tiles in the solved state.
# For example, if BOARDWIDTH and BOARDHEIGHT are both 3, this function
# returns [[1, 4, 7], [2, 5, 8], [3, 6, BLANK]]
counter = 1
board = []
for x in range(BOARDWIDTH):
column = []
for y in range(BOARDHEIGHT):
column.append(counter)
counter += BOARDWIDTH
board.append(column)
counter -= BOARDWIDTH * (BOARDHEIGHT - 1) + BOARDWIDTH - 1
board[BOARDWIDTH-1][BOARDHEIGHT-1] = BLANK
return board
def getBlankPosition(board):
# Return the x and y of board coordinates of the blank space.
for x in range(BOARDWIDTH):
for y in range(BOARDHEIGHT):
if board[x][y] == BLANK:
return (x, y)
def makeMove(board, move):
# This function does not check if the move is valid.
blankx, blanky = getBlankPosition(board)
if move == UP:
board[blankx][blanky], board[blankx][blanky + 1] = board[blankx][blanky + 1], board[blankx][blanky]
elif move == DOWN:
board[blankx][blanky], board[blankx][blanky - 1] = board[blankx][blanky - 1], board[blankx][blanky]
elif move == LEFT:
board[blankx][blanky], board[blankx + 1][blanky] = board[blankx + 1][blanky], board[blankx][blanky]
elif move == RIGHT:
board[blankx][blanky], board[blankx - 1][blanky] = board[blankx - 1][blanky], board[blankx][blanky]
def isValidMove(board, move):
blankx, blanky = getBlankPosition(board)
return (move == UP and blanky != len(board[0]) - 1) or \
(move == DOWN and blanky != 0) or \
(move == LEFT and blankx != len(board) - 1) or \
(move == RIGHT and blankx != 0)
def getRandomMove(board, lastMove=None):
# start with a full list of all four moves
validMoves = [UP, DOWN, LEFT, RIGHT]
# remove moves from the list as they are disqualified
if lastMove == UP or not isValidMove(board, DOWN):
validMoves.remove(DOWN)
if lastMove == DOWN or not isValidMove(board, UP):
validMoves.remove(UP)
if lastMove == LEFT or not isValidMove(board, RIGHT):
validMoves.remove(RIGHT)
if lastMove == RIGHT or not isValidMove(board, LEFT):
validMoves.remove(LEFT)
# return a random move from the list of remaining moves
return random.choice(validMoves)
def getLeftTopOfTile(tileX, tileY):
left = XMARGIN + (tileX * TILESIZE) + (tileX - 1)
top = YMARGIN + (tileY * TILESIZE) + (tileY - 1)
return (left, top)
def getSpotClicked(board, x, y):
# from the x & y pixel coordinates, get the x & y board coordinates
for tileX in range(len(board)):
for tileY in range(len(board[0])):
left, top = getLeftTopOfTile(tileX, tileY)
tileRect = pygame.Rect(left, top, TILESIZE, TILESIZE)
if tileRect.collidepoint(x, y):
return (tileX, tileY)
return (None, None)
I would like to allow it where my tiles can jump to other side of the board if it is available. If a tile is on the left, and an empty slot is on the right; I want to make it where if it where it will jump over and skip the tiles using modulo arithmetic.
I have tried to incorporate modulo arithmetic looping but im not sure where to fully put it in. I have tried it on the makeMove function but I was presented with an error.
You don't really need the modulo to check for "wrap around" blank slots.
Based on the comments in the function getStartingBoard():
# returns [[1, 4, 7], [2, 5, 8], [3, 6, BLANK]]
It seems a board is a two dimensional list, where the cell at (x,y) would be board[y][x].
So say we have a board of some BOARD_WIDTH by BOARD_HEIGHT, given a cell's co-ordinates (x,y), we need to find a list of the neighbouring cells where it's "BLANK" - which we will assume is the number zero.
So first we build a list of all neighbouring cells. At this stage we don't care if they go outside the board. It just adds the cells, top-left, top-centre, ..., bottom-right. Obviously we don't include the actual referenced cell in this.
Then we go through the cell-list, correcting any that are outside the bounds of the board. The fix is to simply add the dimension to the value. Thus if our x has become -1, we can add the BOARD_WIDTH, so it becomes BOARD_WIDTH-1, which is the right-side column number. Similarly if x equals BOARD_WIDTH, subtracting wraps this back to zero. We do the same thing for the y-value.
As a final step, we run through the list of cells, finding the BLANK ones, and added them to a list for returning.
def getBlankCellsAround( board, x, y, BOARD_WIDTH, BOARD_HEIGHT ):
""" Return a list of BLANK cells around the board position (x,y) """
# Default cells around x,y
cell_list = [ (x-1, y-1), (x, y-1), (x+1, y-1), \
(x-1, y), (x+1, y), \
(x-1, y+1), (x, y+1), (x+1, y+1) ]
# Correct any cells that are off the edge so they wrap-around
blanks_list = []
for cell in cell_list:
cx, cy = cell
if ( cx < 0 ):
cx += BOARD_WIDTH
elif ( cx >= BOARD_WIDTH ):
cx -= BOARD_WIDTH
if ( cy < 0 ):
cy += BOARD_HEIGHT
elif ( cy >= BOARD_HEIGHT ):
cy -= BOARD_HEIGHT
if ( board[cy][cx] == 0 ): # if it's blank, copy it to output
blanks_list.append( ( cx, cy ) )
# return the list of blank cells (which might be zero-length)
return blanks_list
I am making a one man pong game and I got everything except for moving the paddle up and down and I don't really understand how to do it can someone help. Is it something to do with the "pos" or is it something to do it with the syntax of the lines. The part where it controls the movements of the paddles is def move paddle
import tkinter
# steps
# ball diagonal
# paddle draw
# paddle animation with keyboard (right/left) -> challenge up/down
# collisions (if time)
# graphic parameters
canvas_width = 400
canvas_height = 500
ball_size = 30
timer_refresh = 20
paddle_width = 100
paddle_height = 20
# ball movement
y_move = 2
x_move = 2
# paddle movement
paddle_speed = 6
# game_state
game_running = True
def end_game():
global game_running
game_running = False
canvas.create_text(canvas_width/2, canvas_height/2, text="you lost!")
# move paddle when key is pressed
def move_paddle(event):
key_symbol = event.keysym
print(key_symbol)
pos = canvas.coords(paddle)
left = pos[0]
right = pos[2]
up = pos[1]
down = pos[3]
if key_symbol == "Left" and left > 0:
canvas.move(paddle, -paddle_speed, 0)
elif key_symbol == "Right" and right <= canvas_width:
canvas.move(paddle, paddle_speed, 0)
# move paddle up
elif key_symbol == "Up" and up >= 0:
canvas.move(paddle, paddle_speed, 0)
# move paddle down
elif key_symbol == "Down" and down <= canvas_width:
canvas.move(paddle, paddle_speed, 0)*
def collision(ball_pos):
overlap_result = canvas.find_overlapping(ball_pos[0],ball_pos[1],ball_pos[2],ball_pos[3])
if paddle in overlap_result:
return True;
else:
return False;
# draw/move ball
def draw():
global y_move, x_move
canvas.move(ball1, x_move, y_move)
pos = canvas.coords(ball1)
top_y = pos[1]
bottom_y = pos[3]
left = pos[0]
right = pos[2]
if top_y <= 0:
y_move = -y_move
elif bottom_y >= canvas_height-5:
y_move = -y_move
end_game()
# did I hit left or right wall?
elif left <= 0 or right >= canvas_width-5:
x_move = -x_move
# did I collide with the paddle? if so bounce vertically
if collision(pos):
y_move = -y_move
# animation timer
def master_timer():
# draw/move ball
draw()
# tkinter processing
tk.update_idletasks()
tk.update()
if game_running:
tk.after(timer_refresh, master_timer)
tk = tkinter.Tk()
tk.title("Simplified Pong")
# block resizing window
tk.resizable(0,0)
# drawing the canvasd
canvas = tkinter.Canvas(tk, width=canvas_width, height=canvas_height, bd=0, highlightthickness=0)
canvas.pack()
ball1 = canvas.create_oval(0, 0, ball_size, ball_size, fill="red")
canvas.move(ball1, canvas_width/2, canvas_height/2)
paddle = canvas.create_rectangle(0,0,paddle_width, paddle_height, fill="black")
canvas.move(paddle, canvas_width/2, canvas_height/1.2)
canvas.bind_all("<KeyPress-Right>", move_paddle)
canvas.bind_all("<KeyPress-Left>", move_paddle)
canvas.bind_all("<KeyPress-Up>", move_paddle)
canvas.bind_all("<KeyPress-Down>", move_paddle)
master_timer()
tk.mainloop()
The problem is quite simple if you refer to the docs of move method, for tkinter, or have an understanding of how to use it. From the docs:
.move(tagOrId, xAmount, yAmount)
Moves the items specified by tagOrId by adding xAmount to their x coordinates and yAmount to their y coordinates.
So if you notice, it takes the x coords first and then the y coords. So when you want to translate(move) upon the y axis(up and down), you want to alter the y axis argument, but instead you are doing it for the x axis. So you have to pass the variable onto the correct parameter only.
elif key_symbol == "Up" and up >= 0:
canvas.move(paddle, 0, -paddle_speed)
# move paddle down
elif key_symbol == "Down" and down <= canvas_width:
canvas.move(paddle, 0, paddle_speed)
Also note that you can get rid of all the key binds and just keep a single general bind, because inside your function, you are already getting the key that is pressed dynamically, so it doesn't make much sense to bind to all possible keys:
canvas.bind_all("<KeyPress>", move_paddle) # Instead of Up, Down, Left and Right
Also another tip is, you can take advantage of * to pass the contents of the iterable as the argument to the function:
canvas.find_overlapping(*ball_pos) # Instead of ball_pos[0] and so on
from tkinter import *
window = Tk()
canvas = Canvas(window, width=1280, height=720)
canvas.pack()
a = (0, 50), (50, 100) # coordinates of the rectangle
rect = canvas.create_rectangle(a, fill="red")
#rect = canvas.create_oval(a, fill="red")
speed = 5 # speed of the rectangle
jump = False
def keypress(event):
x = 0
y = 0
if event.char == "a": x-= speed
elif event.char == "d": x+= speed
elif event.char == "w": y-= speed
elif event.char == "s": y+= speed
elif event.char == " ":
y = 50
jump = True
while jump:
y = 0
jump = False
canvas.move(rect, x, y)
canvas.update()
canvas.after(1)
window.bind("<Key>", keypress)
window.mainloop()
This is my code, im trying to make the rectangle jump but whenever i add the jump code, everything stops working even the normal "asdw" movement that would work otherwise
In case you just want to do a simple jumping animation where the rectangle goes up and down again, just use physics: Define a gravitational pull and an initial vertical velocity and then loop until the ground is reached again:
elif event.char == " ":
diff = 0 ## Difference to initial level
y = -3 ## Initial speed in y direction
grav = .1 ## Gravitation
while diff >= 0: ## While it is still jumping (higher than initially)
canvas.move(rect, x, y)
canvas.update()
sleep(.01) ## Pause for 1/100 second
diff-=y ## Update current jumping height
y+=grav ## Update the speed in y direction
y = 0 ## Just so it is not moved again, afterwards
To make this code snippet work, you have to import time.sleep at the beginning of the program:
from time import sleep
i have a problem with this code, i am a new person with programming and been using the book "how to think like a computer scientist 3rd edition" and he did not solve exercise 2 of chapter 17 this given: "he deliberately left a mistake in the code to animate Duke. If you click on one of the checkerboard squares to the right of Duke, he salutes anyway. Why? Find a one-line solution to the error ", I've tried many forms but I have not succeeded, I leave you all the code and the images that I have used
PS: images must have the name: ball.png and duke_spritesheet.png
import pygame
gravity = 0.025
my_clock = pygame.time.Clock()
class QueenSprite:
def __init__(self, img, target_posn):
self.image = img
self.target_posn = target_posn
(x, y) = target_posn
self.posn = (x, 0) # Start ball at top of its column
self.y_velocity = 0 # with zero initial velocity
def update(self):
self.y_velocity += gravity
(x, y) = self.posn
new_y_pos = y + self.y_velocity
(target_x, target_y) = self.target_posn # Unpack the position
dist_to_go = target_y - new_y_pos # How far to our floor?
if dist_to_go < 0: # Are we under floor?
self.y_velocity = -0.65 * self.y_velocity # Bounce
new_y_pos = target_y + dist_to_go # Move back above floor
self.posn = (x, new_y_pos) # Set our new position.
def draw(self, target_surface): # Same as before.
target_surface.blit(self.image, self.posn)
def contains_point(self, pt):
""" Return True if my sprite rectangle contains point pt """
(my_x, my_y) = self.posn
my_width = self.image.get_width()
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)
def handle_click(self):
self.y_velocity += -2 # Kick it up
class DukeSprite:
def __init__(self, img, target_posn):
self.image = img
self.posn = target_posn
self.anim_frame_count = 0
self.curr_patch_num = 0
def update(self):
if self.anim_frame_count > 0:
self.anim_frame_count = (self.anim_frame_count + 1 ) % 60
self.curr_patch_num = self.anim_frame_count // 6
def draw(self, target_surface):
patch_rect = (self.curr_patch_num * 50, 0,
50, self.image.get_width())
target_surface.blit(self.image, self.posn, patch_rect)
def contains_point(self, pt):
""" Return True if my sprite rectangle contains pt """
(my_x, my_y) = self.posn
my_width = self.image.get_width()
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)
def handle_click(self):
if self.anim_frame_count == 0:
self.anim_frame_count = 5
def draw_board(the_board):
""" Draw a chess board with queens, as determined by the the_board. """
pygame.init()
colors = [(255,0,0), (0,0,0)] # Set up colors [red, black]
n = len(the_board) # This is an NxN chess board.
surface_sz = 480 # Proposed physical surface size.
sq_sz = surface_sz // n # sq_sz is length of a square.
surface_sz = n * sq_sz # Adjust to exactly fit n squares.
# Create the surface of (width, height), and its window.
surface = pygame.display.set_mode((surface_sz, surface_sz))
ball = pygame.image.load("ball.png")
# Use an extra offset to centre the ball in its square.
# If the square is too small, offset becomes negative,
# but it will still be centered :-)
ball_offset = (sq_sz-ball.get_width()) // 2
all_sprites = [] # Keep a list of all sprites in the game
# Create a sprite object for each queen, and populate our list.
for (col, row) in enumerate(the_board):
a_queen = QueenSprite(ball,
(col*sq_sz+ball_offset, row*sq_sz+ball_offset))
all_sprites.append(a_queen)
# Load the sprite sheet
duke_sprite_sheet = pygame.image.load("duke_spritesheet.png")
# Instantiate two duke instances, put them on the chessboard
duke1 = DukeSprite(duke_sprite_sheet,(sq_sz*2, 0))
duke2 = DukeSprite(duke_sprite_sheet,(sq_sz*5, sq_sz))
# Add them to the list of sprites which our game loop manages
all_sprites.append(duke1)
all_sprites.append(duke2)
while True:
# Look for an event from keyboard, mouse, etc.
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break;
if ev.type == pygame.KEYDOWN:
key = ev.dict["key"]
if key == 27: # On Escape key ...
break # leave the game loop.
if key == ord("r"):
colors[0] = (255, 0, 0) # Change to red + black.
elif key == ord("g"):
colors[0] = (0, 255, 0) # Change to green + black.
elif key == ord("b"):
colors[0] = (0, 0, 255) # Change to blue + black.
if ev.type == pygame.MOUSEBUTTONDOWN: # Mouse gone down?
posn_of_click = ev.dict["pos"] # Get the coordinates.
for sprite in all_sprites:
if sprite.contains_point(posn_of_click):
sprite.handle_click()
break
for sprite in all_sprites:
sprite.update()
# Draw a fresh background (a blank chess board)
for row in range(n): # Draw each row of the board.
c_indx = row % 2 # Alternate starting color
for col in range(n): # Run through cols drawing squares
the_square = (col*sq_sz, row*sq_sz, sq_sz, sq_sz)
surface.fill(colors[c_indx], the_square)
# Now flip the color index for the next square
c_indx = (c_indx + 1) % 2
# Ask every sprite to draw itself.
for sprite in all_sprites:
sprite.draw(surface)
my_clock.tick(60) # Waste time so that frame rate becomes 60 fps
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
draw_board([0, 5, 3, 1, 6, 4, 2]) # 7 x 7 to test window size
PS: I think the error is here but it did not succeed
return ( x >= my_x and x + my_width and y >= my_y and y < my_y + my_height)
The issue is caused by the face, that "duke_spritesheet.png" is a sprite sheet. When you define the rectangular region which is covered by the object, then you have to use the width of a single image, rather than the width of the entire sprite sheet:
my_width = self.image.get_width()
my_width = 50
Change this in the method contains_point of the class DukeSprite:
class DukeSprite:
# [...]
def contains_point(self, pt):
""" Return True if my sprite rectangle contains pt """
(my_x, my_y) = self.posn
my_width = 50
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)
I'm making a simple game in Tkinter, and I'm working on the basic movement. It works well except for the fact that the player can move off the screen.
How would I add collision detection to the sides of the window?
I'm moving the player with a move function bound to the arrow keys, like this:
def move(event):
if event.keysym == 'Up':
self.canvas.move(self.id, 0, -5)
elif event.keysym == 'Down':
self.canvas.move(self.id, 0, 5)
elif event.keysym == 'Left':
self.canvas.move(self.id, -5, 0)
else:
self.canvas.move(self.id, 5, 0)
You can get the size of your canvas like this:
size, _ = self.canvas.winfo_geometry().split('+', maxsplit=1)
w, h = (int(_) for _ in size.split('x'))
And the position of your Squarey like this:
x, y, _, __ = self.canvas.coords(self.id)
(There may be better ways to do this, of course)
Then just adapt your movement function like this:
if event.keysym == 'Up':
if y > 0:
self.canvas.move(self.id, 0, -5)
elif event.keysym == 'Down':
if y+50 < h:
self.canvas.move(self.id, 0, 5)
elif event.keysym == 'Left':
if x > 0:
self.canvas.move(self.id, -5, 0)
else:
if x+50 < w:
self.canvas.move(self.id, 5, 0)
That should work for you (at least it does for me). But you shouldn't stop here, there are some improvements that you can make.
The first one that I would do is something like this:
def __init__(self, canvas, color, width=50, height=50):
self.canvas = canvas
self.width = width
self.height = height
self.id = canvas.create_rectangle(10, 10, width, height, fill=color)
Then you could change your move:
left_edge = x
right_edge = left_edge + self.width
top_edge = y
bottom_edge = top_edge + self.height
if event.keysym == 'Up' and top_edge > 0:
...
elif event.keysym == 'Down' and bottom_edge < h:
...
elif event.keysym == 'Left' and left_edge > 0:
...
elif event.keysym == 'Right' and right_edge < w:
...
I assume you want to detect the collision of a rectangle with the edges of the screen. I am stranger to tkinter, however I have experience with pygame, let me explain as well as I can.
In pygame, a rectange position are given as left_top corner, a(one side), b(another side). Such as,
(x, y) b
...............
. .
. .
a. .
. .
...............
if you want to detect collision, you have to check all sides, using these values.
Let's say screen is (width, height)
# Top side collision
if y < 0:
print "Touched top"
# Right side collision
if x + b > width:
print "Touched right"
# Bottom side collision
if y + a > height:
print "Touched bottom"
# Left side collision
if x < 0:
print "Touched left"
I am pretty sure quite similar logic is needed in tkinter as well.