I'm learning Python. At the moment, I can do what I want to do by composition, but when I try to do the same thing using inheritance, I get an error. Here's my code. I'm basically trying to make a class for a colored square.
from graphics import *
class Block(Rectangle):
def __init__(self, corner, colour):
self.corner = corner
self.colour = colour
self.x1 = self.corner.getX() * 30
self.y1 = self.corner.getY() * 30
self.x2 = self.x1 + 30
self.y2 = self.y1 + 30
self.point1 = Point(self.x1, self.y1)
self.point2 = Point(self.x2, self. y2)
Rectangle.__init__(self, self.point1, self.point2)
def draw(self, window):
self.window = window
self.Rectangle.draw(self.window)
new_win = GraphWin("thingy", 700, 500)
corner = Point(1, 1)
square1 = Block(corner, 'red')
square1.draw(new_win)
new_win.mainloop()
The error I get is
File "F:\Python\4\4_3.py", line 24, in draw
self.draw(self.window)
The error is repeated indefinitely.
Here is the code that does what I want when I do it with composition:
from graphics import *
class Block():
def __init__(self, corner, colour):
self.corner = corner
self.colour = colour
self.x1 = self.corner.getX() * 30
self.y1 = self.corner.getY() * 30
self.x2 = self.x1 + 30
self.y2 = self.y1 + 30
self.point1 = Point(self.x1, self.y1)
self.point2 = Point(self.x2, self. y2)
self.Rectangle = Rectangle(self.point1, self.point2)
def draw(self, window):
self.window = window
self.Rectangle.draw(self.window)
self.Rectangle.setFill(self.colour)
new_win = GraphWin("thingy", 150, 150)
corner = Point(1, 1)
square1 = Block(corner, 'red')
square1.draw(new_win)
new_win.mainloop()
from graphics import *
class Block(Rectangle):
def __init__(self, corner, colour):
self.corner = corner
self.colour = colour
self.x1 = self.corner.getX() * 30
self.y1 = self.corner.getY() * 30
self.x2 = self.x1 + 30
self.y2 = self.y1 + 30
self.point1 = Point(self.x1, self.y1)
self.point2 = Point(self.x2, self. y2)
Rectangle.__init__(self, self.point1, self.point2)
def draw(self, window):
self.window = window
Rectangle.draw(self, self.window)
# instead of self.Rectangle.draw(self.window)
In the case of inheritance, there is no self.Rectangle
The simple code for python 2.7 is:
BaseClassName.__init__(self, args)
Related
So, yeah I'm a beginner to Tkinter Canvas Games and it's being too difficult for me to code than I thought.
Okk, let's come to the chase, below is my code:
import time
from tkinter import *
from numpy import random
class Paddle:
def __init__(self, Main_Frame):
Main_Frame.title("Paddle")
Main_Frame.geometry("300x300+630+150")
self.Random_X = random.randint(270)
self.Random_Y = random.randint(120)
self.x = 3
self.y = 3
self.Y_Position = 288
self.Can = Canvas(Main_Frame, height = 300, width = 300)
self.Can.pack()
self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill = "Aqua", outline = "Aqua")
self.ball = self.Can.create_oval(self.Random_X, self.Random_Y, self.Random_X + 20, self.Random_Y + 20, outline = "Red", fill = "Red")
self.Can.bind("<Motion>", self.Move_Paddle)
self.Move_Ball()
def Move_Ball(self):
Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
if Ball_x2 > 300:
self.x = -self.x
if Ball_y2 > 300:
self.y = -self.y
if Ball_x1 < 0:
self.x = -self.x
if Ball_y2 < 10:
self.y = -self.y
self.Can.moveto(self.ball, self.x, self.y)
Window.after(1, self.Move_Ball)
def Move_Paddle(self, event):
self.X_Position = event.x
self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)
Window = Tk()
Class = Paddle(Window)
Window.mainloop()
So, my problem here is that even after calling the Move_Ball() function, it had no effect on the ball, (okk there is no error occurring though) when I tried to call the function under Move_Paddle() function (before adding the Window.after(1, self.Move_Ball) line), it worked well when I moved the mouse around the Canvas, well, I want it to automatically activate when the Paddle class is called.
I was trying to check the problem in the code by placing the function call in Move_Paddel() (with the Window.after(1, self.Move_Ball) line as in the code), it increased the trouble and insanely increased the speed of the ball.
If anyone could help me with these couple of problems, it would be highly appreciated.
Thanks in advance!
Use move() instead of moveto() method. move() method moves each of the items given by tagOrId in the canvas coordinate space by adding X amount to the x-coordinate of each point associated with the item and y amount to the y-coordinate of each point associated with the item.
Then you may increase the delay in the after() method.
Here is the working code:
import time
from tkinter import *
from numpy import random
class Paddle:
def __init__(self, Main_Frame):
Main_Frame.title("Paddle")
Main_Frame.geometry("300x300+630+150")
self.Random_X = random.randint(270)
self.Random_Y = random.randint(120)
self.x = 3
self.y = 3
self.Y_Position = 288
self.Can = Canvas(Main_Frame, height = 300, width = 300)
self.Can.pack()
self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill = "Aqua", outline = "Aqua")
self.ball = self.Can.create_oval(self.Random_X, self.Random_Y, self.Random_X + 20, self.Random_Y + 20, outline = "Red", fill = "Red")
self.Can.bind("<Motion>", self.Move_Paddle)
self.Move_Ball()
def Move_Ball(self):
Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
if Ball_x2 > 300:
self.x = -self.x
if Ball_y2 > 300:
self.y = -self.y
if Ball_x1 < 0:
self.x = -self.x
if Ball_y2 < 10:
self.y = -self.y
self.Can.move(self.ball, self.x, self.y)
Window.after(50 ,self.Move_Ball)
def Move_Paddle(self, event):
self.X_Position = event.x
self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)
Window = Tk()
Class = Paddle(Window)
Window.mainloop()
I rewrote your code and now I think it works as you wished. Red ball slowly goes down:
import time
from tkinter import *
from numpy import random
class Paddle:
def __init__(self, main_frame):
self.root = main_frame
random.seed(int(time.time()))
self.x = random.randint(270)
self.y = random.randint(120)
self.Y_Position = 288
self.Can = Canvas(self.root, height=300, width=300)
self.Can.pack()
self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill="Aqua", outline="Aqua")
self.ball = self.Can.create_oval(self.x, self.y, self.x + 20, self.y + 20, outline="Red", fill="Red")
self.Can.bind("<Motion>", self.move_paddle)
self.move_ball()
def move_ball(self):
Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
# if Ball_x2 > 300:
# self.x = -self.x
# if Ball_y2 > 300:
# self.y = -self.y
# if Ball_x2 < 0:
# self.x = 300
if Ball_y2 > 300:
self.y = 0
self.y += 0.1
self.Can.moveto(self.ball, self.x, self.y)
self.root.after(1, self.move_ball)
def move_paddle(self, event):
self.X_Position = event.x
self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)
root = Tk()
root.title("Paddle")
root.geometry("300x300+630+150")
Class = Paddle(root)
root.mainloop()
And please read about PEP8
You should use move() instead of moveto() inside Move_Ball() and after(1, ...) is too frequent so the ball will be moved very fast. Try after(50, ...) instead:
def Move_Ball(self):
...
self.Can.move(self.ball, self.x, self.y)
Window.after(50, self.Move_Ball)
New to programming. Working on a simple pong clone. Started the ball but want to make sure all sides of the window (500x500) will have the ball bounce off of it. How could I do this? Thanks!
P.S. This is my current code if needed.
import threading
import random
import time
import string
import os.path
from random import randint
from tkinter import *
class Pong:
Title = 'Pong'
Size = '500x500'
class Ball:
def __init__(self,canvas,x1,y1,x2,y2):
self.x1 =x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.canvas = canvas
self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="black")
def move_ball(self):
deltax = randint(0,5)
deltay = randint(0,5)
self.canvas.move(self.ball,deltax,deltay)
self.canvas.after(50,self.move_ball)
def PongGame():
print("Moved to PongGame.")
ball1 = Ball(canvas,10,10,30,30)
ball1.move_ball()
def titleButtonClicked(event):
print("Title screen button clicked.")
btn.pack_forget()
btn.place(x=600,y=600)
msg.pack_forget()
PongGame()
root = Tk()
root.geometry(Pong.Size)
root.title(Pong.Title)
root.resizable(False,False)
msg = Label(root, text = Pong.Title, font = ("", 50))
msg.pack()
canvas = Canvas(root, width = 500, height = 500)
canvas.pack()
btn=Button(root, text = "Start")
btn.bind('<Button-1>', titleButtonClicked)
btn.place(x=220,y=300)
root.mainloop()
Collisions are not trivial; the simplest is to reverse the x or the y velocity after checking which edge of the bounding box of the ball intersects with the boundaries of the canvas.
Maybe something like this:
import random
import tkinter as tk
WIDTH, HEIGHT = 500, 500
class Ball:
radius = 10
spawn_center = (250, 100)
def __init__(self, canvas):
self.canvas = canvas
self.id = None
self.create_ball()
self.velocity = None
self.assign_random_velocity()
self.keep_moving = True
self.move()
def create_ball(self):
xc, yc = self.spawn_center
x0, y0, = xc - self.radius, yc + self.radius
x1, y1, = xc + self.radius, yc - self.radius
self.id = self.canvas.create_oval(x0, y0, x1, y1)
def assign_random_velocity(self):
dx = random.randrange(1, 5) * random.choice((1, -1))
dy = random.randrange(1, 5) * random.choice((1, -1))
self.velocity = (dx, dy)
def move(self):
if self.keep_moving is None:
return
self.check_collision()
self.canvas.move(self.id, *self.velocity)
self.keep_moving = self.canvas.after(10, self.move)
def cancel_move(self):
if self.keep_moving is not None:
self.canvas.after_cancel(self.keep_moving)
self.keep_moving = None
def check_collision(self):
x0, y0, x1, y1 = self.canvas.coords(self.id)
dx, dy = self.velocity
if x0 < 0:
x0 = 0
dx = -dx
elif x1 > WIDTH:
x1 = WIDTH
dx = -dx
if y0 < 0:
y0 = 0
dy = -dy
elif y1 > HEIGHT:
y1 = HEIGHT
dy = -dy
self.velocity = dx, dy
class PongBoard(tk.Canvas):
def __init__(self, master):
self.master = master
super().__init__(self.master)
self.ball = None
self.spawn_new_ball()
def spawn_new_ball(self):
if self.ball is not None:
self.ball.cancel_move()
self.delete(self.ball.id)
self.ball = Ball(self)
root = Tk()
root.geometry(f'{WIDTH}x{HEIGHT+20}')
board = PongBoard(root)
new_ball_btn = tk.Button(root, text='spawn new ball', command=board.spawn_new_ball)
board.pack(expand=True, fill=tk.BOTH)
new_ball_btn.pack()
root.mainloop()
This will get you started, but you will have to implement the paddles, the paddles movement, the collision checking of the ball with the paddles, and keep the score by yourself.
I'm creating 2D game and when i am trying to add more than one wall they doesn't appear on canvas.
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()
class wall:
point1 = []
point2 = []
def __init__(self, canvas, x1, y1, x2, y2):
self.canvas = canvas
self.point1.append(x1)
self.point1.append(y1)
self.point2.append(x2)
self.point2.append(y2)
def draw(self):
self.canvas.create_line(self.point1[0], self.point1[1], self.point2[0], self.point2[1], width = 2)
walls = []
walls.append(wall(canvas, 90, 90, 100, 200))
walls.append(wall(canvas, 90, 90, 300, 100))
def update():
for wall in walls:
wall.draw()
root.after(int(1000/60), update)
root.after(int(1000/60), update)
root.mainloop()
If I add them manually they are drawing both.
canvas.create_line(90, 90, 100, 200, width = 2)
canvas.create_line(90, 90, 300, 100, width = 2)
Consider this part of your class wall:
class wall:
point1 = []
point2 = []
...
The lists point1 and point2 are defined as class attribute instead of instance attribute. So when you append new coordinates, the previous ones are still there.
To fix this, simply make point and point2 instance attributes instead:
class wall:
def __init__(self, canvas, x1, y1, x2, y2):
self.point1 = []
self.point2 = []
...
Or use the parameters directly:
class wall:
def __init__(self, canvas, x1, y1, x2, y2):
self.canvas = canvas
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def draw(self):
self.canvas.create_line(self.x1, self.y1, self.x2, self.y2, width = 2)
Use Instance attributes instead of Class attributes.
class wall:
def __init__(self, canvas, x1, y1, x2, y2):
self.canvas = canvas
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def draw(self):
self.canvas.create_line(self.x1, self.y1, self.x2, self.y2, width=2)
I have a Point class and Rectangle class set up, here is the code for that:
import math
class Point:
"""A point in two-dimensional space."""
def __init__(self, x: float = 0.0, y: float = 0.0)->None:
self.x = x
self.y = y
def moveIt(self, dx: float, dy: float)-> None:
self.x = self.x + dx
self.y = self.y + dy
def distance(self, otherPoint: float):
if isinstance(otherPoint, Point):
x1 = self.x
y1 = self.y
x2 = otherPoint.x
y2 = otherPoint.y
return ( (x1 - x2)**2 + (y1 - y2)**2 )**0.5
class Rectangle:
def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
self.topLeft = topLeft
self.bottomRight = bottomRight
The two points are the top left and the bottom right for the rectangle. How could I find the area and perimeter of this rectangle from two points? Would appreciate any and all help!
We can access the x and y values of each point and calculate the height and width, from there we can create methods that calculate area and perimeter
class Rectangle():
def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
self.topLeft = topLeft
self.bottomRight = bottomRight
self.height = topLeft.y - bottomRight.y
self.width = bottomRight.x - topLeft.x
self.perimeter = (self.height + self.width) * 2
self.area = self.height * self.width
rect = Rectangle(Point(3,10),Point(4,8))
print(rect.height)
print(rect.width)
print(rect.perimeter)
print(rect.area)
chrx#chrx:~/python/stackoverflow/9.24$ python3.7 rect.py
2
1
6
2
Or using methods
class Rectangle():
def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
self.topLeft = topLeft
self.bottomRight = bottomRight
self.height = topLeft.y - bottomRight.y
self.width = bottomRight.x - topLeft.x
def make_perimeter(self):
self.perimeter = (self.height + self.width) * 2
return self.perimeter
def make_area(self):
self.area = self.height * self.width
return self.area
rect = Rectangle(Point(3,10),Point(4,8))
print(rect.height)
print(rect.width)
print(rect.make_perimeter())
print(rect.make_area())
I am working to create a version of asteroids using Python and Tkinter. When the left or right arrow key is pressed the ship needs to rotate. The ship is a triangle on the Tkinter canvas. I am having trouble coming up with formula to adjust the coordinates for the triangle. I believe it has something to do with sin and cos, though I am not exactly sure. So far I have two classes one for the ship and the other for the game. In the ship class I have callback methods for the key presses. Any help would be greatly appreciated. Thanks.
Ship Class
import math
class Ship:
def __init__(self,canvas,x,y,width,height):
self.canvas = canvas
self.x = x - width/2
self.y = y + height/2
self.width = width
self.height = height
self.x0 = self.x
self.y0 = self.y
self.x1 = self.x0 + self.width/2
self.y1 = self.y0-self.height
self.x2 = self.x0 + self.width
self.y2 = self.y0
self.ship = self.canvas.create_polygon((self.x0, self.y0, self.x1, self.y1, self.x2, self.y2), outline="white", width=3)
def changeCoords(self):
self.canvas.coords(self.ship,self.x0, self.y0, self.x1, self.y1, self.x2, self.y2)
def rotateLeft(self, event=None):
# Should rotate one degree left.
pass
def rotateRight(self, event=None):
# Should rotate one degree right.
self.x0 = self.x0 -1
self.y0 = self.y0 - 1
self.x1 = self.x1 + 1
self.y1 = self.y1 + 1
self.x2 = self.x2 - 1
self.y2 = self.y2 + 1
self.changeCoords()
Game Class
from Tkinter import *
from ship import *
class Game:
def __init__(self, gameWidth, gameHeight):
self.root = Tk()
self.gameWidth = gameWidth
self.gameHeight = gameHeight
self.gameWindow()
self.ship = Ship(self.canvas, x=self.gameWidth/2,y=self.gameHeight/2, width=50, height=50)
self.root.bind('<Left>', self.ship.rotateLeft)
self.root.bind('<Right>', self.ship.rotateRight)
self.root.mainloop()
def gameWindow(self):
self.frame = Frame(self.root)
self.frame.pack(fill=BOTH, expand=YES)
self.canvas = Canvas(self.frame,width=self.gameWidth, height=self.gameHeight, bg="black", takefocus=1)
self.canvas.pack(fill=BOTH, expand=YES)
asteroids = Game(600,600)
First of all, you need to rotate around a center of the triangle. The centroid would probably work best for that. To find that, you can use the formula C = (1/3*(x0 + x1 + x2), 1/3*(y0 + y1 + y2)), as it's the average of all points in the triangle. Then you have to apply the rotation with that point as the center. So it'd be something like this...
import math
class Ship:
def centroid(self):
return 1 / 3 * (self.x0 + self.x1 + self.x2), 1 / 3 * (self.y0 + self.y1 + self.y2)
def __init__(self, canvas, x, y, width, height, turnspeed, acceleration=1):
self._d = {'Up':1, 'Down':-1, 'Left':1, 'Right':-1}
self.canvas = canvas
self.width = width
self.height = height
self.speed = 0
self.turnspeed = turnspeed
self.acceleration = acceleration
self.x0, self.y0 = x, y
self.bearing = -math.pi / 2
self.x1 = self.x0 + self.width / 2
self.y1 = self.y0 - self.height
self.x2 = self.x0 + self.width
self.y2 = self.y0
self.x, self.y = self.centroid()
self.ship = self.canvas.create_polygon((self.x0, self.y0, self.x1, self.y1, self.x2, self.y2), outline="white", width=3)
def changeCoords(self):
self.canvas.coords(self.ship,self.x0, self.y0, self.x1, self.y1, self.x2, self.y2)
def rotate(self, event=None):
t = self._d[event.keysym] * self.turnspeed * math.pi / 180 # the trig functions generally take radians as their arguments rather than degrees; pi/180 radians is equal to 1 degree
self.bearing -= t
def _rot(x, y):
#note: the rotation is done in the opposite fashion from for a right-handed coordinate system due to the left-handedness of computer coordinates
x -= self.x
y -= self.y
_x = x * math.cos(t) + y * math.sin(t)
_y = -x * math.sin(t) + y * math.cos(t)
return _x + self.x, _y + self.y
self.x0, self.y0 = _rot(self.x0, self.y0)
self.x1, self.y1 = _rot(self.x1, self.y1)
self.x2, self.y2 = _rot(self.x2, self.y2)
self.x, self.y = self.centroid()
self.changeCoords()
def accel(self, event=None):
mh = int(self.canvas['height'])
mw = int(self.canvas['width'])
self.speed += self.acceleration * self._d[event.keysym]
self.x0 += self.speed * math.cos(self.bearing)
self.x1 += self.speed * math.cos(self.bearing)
self.x2 += self.speed * math.cos(self.bearing)
self.y0 += self.speed * math.sin(self.bearing)
self.y1 += self.speed * math.sin(self.bearing)
self.y2 += self.speed * math.sin(self.bearing)
self.x, self.y = self.centroid()
if self.y < - self.height / 2:
self.y0 += mh
self.y1 += mh
self.y2 += mh
elif self.y > mh + self.height / 2:
self.y0 += mh
self.y1 += mh
self.y2 += mh
if self.x < -self.width / 2:
self.x0 += mw
self.x1 += mw
self.x2 += mw
elif self.x > mw + self.width / 2:
self.x0 -= mw
self.x1 -= mw
self.x2 -= mw
self.x, self.y = self.centroid()
self.changeCoords()
I made some changes to the controls that make the game a bit more like Asteroids, by the way. (Didn't implement firing, though. I may have gotten more into this than I expected, but I'm not going to do everything. Also, there's a bit of a problem when you try to use multiple movement keys at once, but that's due to the way Tk does event handling. It wasn't designed for gaming, so you'd have to fiddle around a fair bit to get that working properly with Tk/Tkinter.)
from tkinter import *
from ship import *
class Game:
def __init__(self, gameWidth, gameHeight):
self.root = Tk()
self.gameWidth = gameWidth
self.gameHeight = gameHeight
self.gameWindow()
self.ship = Ship(self.canvas, x=self.gameWidth / 2,y=self.gameHeight / 2, width=50, height=50, turnspeed=10, acceleration=5)
self.root.bind('<Left>', self.ship.rotate)
self.root.bind('<Right>', self.ship.rotate)
self.root.bind('<Up>', self.ship.accel)
self.root.bind('<Down>', self.ship.accel)
self.root.mainloop()
def gameWindow(self):
self.frame = Frame(self.root)
self.frame.pack(fill=BOTH, expand=YES)
self.canvas = Canvas(self.frame,width=self.gameWidth, height=self.gameHeight, bg="black", takefocus=1)
self.canvas.pack(fill=BOTH, expand=YES)
asteroids = Game(600,600)
As an aside, you might want to use properties to allow for easier handling of the points and such.