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
Related
so i'm currently making a python turtle graphics game, it's a pong game where there is a 2 paddles and they pass the ball to each other, once ball touches each side the player gets a point, the game is fully functional in 1920x1080 res, the thing is i made a main menu type of thing where the game starts after the Start button has been clicked, thing is the menu works exactly as intended but even before i press start the main game loop starts once canvas is opened, not when start button is clicked, so it looks like this: https://imgur.com/tRWc6q9, this is what it looks like after menu button has been pressed: https://imgur.com/FwW9dO2
Here is the whole code, sorry if it's quite long i have been trying to solve it for 2 days
import turtle
import time
pong = turtle.Screen()
pong.title("My First Project")
pong.bgcolor("black")
pong.setup(width=1920,height=1080)
pong.tracer(0)
#Step 1: Create Both Padles and Ball(Done)
#Step 2: Movement Functions and Linking them to Keyboard(Done)
#Step 3: Scoring System and Updating it(Done)
#Step 4: Winning Screen(Done)
#Step 5: Collision with Paddles and Balls(Done)
#Step 5: Main Menu(In Progress)
#Creating Menu
mainCanvas = turtle.Turtle()
mainCanvas.color("cyan")
mainCanvas.hideturtle
mainCanvas.speed(0)
mainCanvas.begin_fill()
#list to make the main menu bg
fd_list1=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd1, fwd1 in fd_list1:
mainCanvas.setheading(hd1)
mainCanvas.fd(fwd1)
mainCanvas.end_fill()
#Creating Title
mainTitle = turtle.Turtle()
mainTitle.penup()
mainTitle.speed(0)
mainTitle.hideturtle()
mainTitle.goto(0,270)
mainTitle.write("Main Menu",align="center",font=("a Absolute Empire",60,"normal"))
#Create Start Option
startGame = turtle.Turtle()
startGame.speed(0)
startGame.penup()
startGame.goto(0,100)
pong.register_shape("start_400x150.gif") #PNG for start button
startGame.shape("start_400x150.gif") #Applying the PNG
startGame.shapesize(stretch_len=20,stretch_wid=20)
#Start Click Function
def clickStart(x,y):
if x > startGame.xcor() - 200 and x < startGame.xcor() + 200 and y > startGame.ycor() - 75 and y < startGame.ycor() + 75:
time.sleep(0.5)
mainTitle.clear()
pong.bgcolor("black")
startGame.hideturtle()
#Get rid of Main Menu
mainCanvas.color("black")
mainCanvas.goto(0,0)
mainCanvas.begin_fill()
#List to change canvas to desired color once clicked
fd_list=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd, fwd in fd_list:
mainCanvas.setheading(hd)
mainCanvas.fd(fwd)
mainCanvas.end_fill()
#Hide Button
startGame.goto(0,5000)
#Key Binding Main Menu
pong.listen()
pong.onscreenclick(clickStart,1)
#creating Paddle A, Player 1(left Side)
paddleA = turtle.Turtle()
paddleA.speed(0)
paddleA.shape("square")
paddleA.shapesize(9,1)
paddleA.color("white")
paddleA.penup()
paddleA.goto(-900,0)
#creating Paddle B, Player 2(Right Side)
paddleB = turtle.Turtle()
paddleB.speed(0)
paddleB.shape("square")
paddleB.shapesize(9,1)
paddleB.color("white")
paddleB.penup()
paddleB.goto(900,0)
#creating Ball makes it move in diagonal direction in fixed speed with ball.dx = 3 and ball.dy = 3
#Speed depends on the power of your PC or Mac, you can change dx and dy according to ur computer speed(mine sucks so 3 is pretty fast for me)
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.shapesize(1.8,1.8)
ball.penup()
ball.goto(0,0)
ball.x = 5
ball.y = 5
#Middle Line
#Creates multiple lines, enumerates them then checks for each enumerate with idx
#t variable is number of turtles, a line that goes down the middle
turtles = [turtle.Turtle() for _ in range(100)]
for idx, t in enumerate(turtles):
t.speed(0)
t.shape("square")
t.shapesize(2,0.1)
t.color("white")
t.penup()
t.goto(0,540-10*idx)
#Movings Functions
#Paddle A Up with (w) key for player A(1)
def paddleAUp():
y = paddleA.ycor()
y += 20
paddleA.sety(y)
#Paddle A Down with (s) Key for player A(1)
def paddleADown():
y = paddleA.ycor()
y -= 20
paddleA.sety(y)
#Paddle B Up with (Up_Arrow) Key for player B(2)
def paddleBUp():
y = paddleB.ycor()
y += 20
paddleB.sety(y)
#Paddle B Down (Down_Arrow) key for player B(2)
def paddleBDown():
y = paddleB.ycor()
y -= 20
paddleB.sety(y)
#Binding the Moving functions to Keyboard
#Pong.listen, listens to keyboard input
pong.listen()
#Moves Paddle A(1) up and down the y axis with (w) and (s) key respectively
pong.onkeypress(paddleAUp,"w")
pong.onkeypress(paddleADown,"s")
#Moves Paddle B(2) up and down the y axis with (Up_Arrow) and (Down_Arrow) key respectively
pong.onkeypress(paddleBUp,"Up")
pong.onkeypress(paddleBDown,"Down")
#Scores
AScore = 0
BScore = 0
#Scoring System for Player A(1)
ScoreSystemA = turtle.Turtle()
ScoreSystemA.pencolor("White")
ScoreSystemA.penup()
ScoreSystemA.setposition(-950,480)
ScoreSystemA.pendown()
ScoreSystemA.write(f"P1 Score: {AScore}",font=("courier",30,"normal"))
ScoreSystemA.hideturtle()
#Scoring System for Player B(2)
ScoreSystemB = turtle.Turtle()
ScoreSystemB.pencolor("White")
ScoreSystemB.penup()
ScoreSystemB.setposition(680,480)
ScoreSystemB.pendown
ScoreSystemB.write(f"P2 Score: {BScore}", font=("courier",30,"normal"))
ScoreSystemB.hideturtle()
#Main Loop To check canvas and update it each millisecond
while True:
pong.update()
#Makes Ball Move
ball.setx(ball.xcor() + ball.x)
ball.sety(ball.ycor() + ball.y)
#Ball Bouncing when it hits edge, Edge Detection at +y
if ball.ycor() > 510:
ball.sety(510)
ball.y = ball.y * -1
#Ball Bouncing when it hits edge, Edge Detection at -y
if ball.ycor() < -510:
ball.sety(-510)
ball.y = ball.y * -1
#Counting scoring system, when Ball goes Behind Paddle the Ball Resets and Goes in the Other Direction
#Which will then update the Score Text at the upper Left Corner
if ball.xcor() > 950:
ball.goto(0,0)
ball.x *= -1
AScore = AScore + 1
ScoreSystemA.clear()
ScoreSystemA.write(f"P1 Score: {AScore}",font=("courier",30,"normal"))
#Same As Above but for Paddle B
if ball.xcor() < -950:
ball.goto(0,0)
ball.x *= -1
BScore = BScore + 1
ScoreSystemB.clear()
ScoreSystemB.write(f"P2 Score: {BScore}", font=("courier",30,"normal"))
#Winning Condition writes Player 1(A) Won and stops the Game
if AScore == 3:
AWin = turtle.Turtle()
AWin.pencolor("White")
AWin.hideturtle()
AWin.penup()
AWin.setposition(0,0)
AWin.pendown()
AWin.write("P1 Wins!",align="center",font=("courier",60,"normal"))
turtles.clear()
turtle.done()
#Same As Above but for Paddle B
if BScore == 3:
BWin = turtle.Turtle()
BWin.pencolor("White")
BWin.hideturtle()
BWin.penup()
BWin.setposition(0,0)
BWin.pendown()
BWin.write("P2 Wins!",align="center",font=("courier",60,"normal"))
turtles.clear()
turtle.done()
#Colission System first 2 conditions are for ball x coordinate to check if it's at the same x value as the paddle
#Third if ball is less than paddle y cor for top half of the paddle
#Fourth if ball is greater than paddle y cor for bottom half of the paddle
if ball.xcor() < -900 and ball.xcor() > -950 and (ball.ycor() < paddleA.ycor() + 72 and ball.ycor() > paddleA.ycor() - 72):
ball.x = ball.x * -1
#Same As Above but for Paddle B
if ball.xcor() > 900 and ball.xcor() < 950 and (ball.ycor() < paddleB.ycor() + 72 and ball.ycor() > paddleB.ycor() - 72):
ball.x = ball.x * -1
here is the bit i'm struggling with:
#Start Click Function
def clickStart(x,y):
if x > startGame.xcor() - 200 and x < startGame.xcor() + 200 and y > startGame.ycor() - 75 and y < startGame.ycor() + 75:
time.sleep(0.5)
mainTitle.clear()
pong.bgcolor("black")
startGame.hideturtle()
#Get rid of Main Menu
mainCanvas.color("black")
mainCanvas.goto(0,0)
mainCanvas.begin_fill()
#List to change canvas to desired color once clicked
fd_list=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd, fwd in fd_list:
mainCanvas.setheading(hd)
mainCanvas.fd(fwd)
mainCanvas.end_fill()
#Hide Button
startGame.goto(0,5000)
#Key Binding Main Menu
pong.listen()
pong.onscreenclick(clickStart,1)
any help is appreciated
i tried: making a condition where if click happens then the game loop starts, when i did that and i clicked the canvas just turned all black, i think it returned none
i tried: making a second game loop, that stops when clicking the start button, that just broke
What i expected: i run code, game opens and does nothing, till i press the start button then the game starts and it plays.
You've made this difficult to implement by putting too much functionality at the top level of the code instead of encapsulated in functions. I've reworked your code to shoehorn in this functionality below. Your "start" button is just a giant circle as I don't have your artwork:
from turtle import Screen, Turtle
SMALL_FONT = ("Courier", 30, "normal")
BIG_FONT = ("Courier", 60, "normal")
MENU_FONT = ("a Absolute Empire", 60, "normal")
WINDOW_WIDTH, WINDOW_HEIGHT = 1920, 1080
fd_list = [(90, WINDOW_HEIGHT/2), (0, WINDOW_WIDTH/2), (270, WINDOW_HEIGHT), (180, WINDOW_WIDTH), (90, WINDOW_HEIGHT), (0, WINDOW_WIDTH/2)]
def clickStart(x, y):
if startGame.xcor() - 200 < x < startGame.xcor() + 200 and startGame.ycor() - 75 < y < startGame.ycor() + 75:
screen.onscreenclick(None)
mainTitle.clear()
screen.bgcolor("black")
startGame.hideturtle()
# Get rid of Main Menu
mainCanvas.clear()
startGame.hideturtle()
# Moves Paddle A(1) up and down the y axis with (w) and (s) key respectively
screen.onkeypress(paddleAUp, "w")
screen.onkeypress(paddleADown, "s")
paddleA.showturtle()
# Moves Paddle B(2) up and down the y axis with (Up_Arrow) and (Down_Arrow) key respectively
screen.onkeypress(paddleBUp, "Up")
screen.onkeypress(paddleBDown, "Down")
paddleB.showturtle()
screen.listen()
play()
# Movings Functions
# Paddle A Up with (w) key for player A(1)
def paddleAUp():
paddleA.forward(20)
# Paddle A Down with (s) Key for player A(1)
def paddleADown():
paddleA.backward(20)
# Paddle B Up with (Up_Arrow) Key for player B(2)
def paddleBUp():
paddleB.forward(20)
# Paddle B Down (Down_Arrow) key for player B(2)
def paddleBDown():
paddleB.backward(20)
# Scores
AScore = 0
BScore = 0
# Main Loop To check canvas and update it each millisecond
def play():
global AScore, BScore
# Make Ball Move
ball.setx(ball.xcor() + ball.x)
ball.sety(ball.ycor() + ball.y)
# Ball Bouncing when it hits edge
if ball.ycor() > 510:
ball.sety(510)
ball.y *= -1
elif ball.ycor() < -510:
ball.sety(-510)
ball.y *= -1
# Counting scoring system, when Ball goes Behind Paddle the Ball Resets and Goes in the Other Direction
# Which will then update the Score Text at the upper Left Corner
if ball.xcor() > 950:
ball.goto(0, 0)
ball.x *= -1
AScore += 1
ScoreSystemA.clear()
ScoreSystemA.write(f"P1 Score: {AScore}", align='center', font=SMALL_FONT)
elif ball.xcor() < -950:
ball.goto(0, 0)
ball.x *= -1
BScore += 1
ScoreSystemB.clear()
ScoreSystemB.write(f"P2 Score: {BScore}", align='center', font=SMALL_FONT)
# Winning Condition writes Player 1(A) Won and stops the Game
if AScore == 3:
midLine.clear()
ball.hideturtle()
AWin = Turtle()
AWin.hideturtle()
AWin.pencolor("White")
AWin.write("P1 Wins!", align="center", font=BIG_FONT)
screen.update()
return
if BScore == 3:
midLine.clear()
ball.hideturtle()
BWin = Turtle()
BWin.hideturtle()
BWin.pencolor("White")
BWin.write("P2 Wins!", align="center", font=BIG_FONT)
screen.update()
return
# Colission System first 2 conditions are for ball x coordinate to check if it's at the same x value as the paddle
# Third if ball is less than paddle y cor for top half of the paddle
# Fourth if ball is greater than paddle y cor for bottom half of the paddle
if -950 < ball.xcor() < -900 and paddleA.ycor() - 72 < ball.ycor() < paddleA.ycor() + 72:
ball.x *= -1
elif 900 < ball.xcor() < 950 and paddleB.ycor() - 72 < ball.ycor() < paddleB.ycor() + 72:
ball.x = ball.x * -1
screen.update()
screen.ontimer(play)
screen = Screen()
screen.title("My First Project")
screen.bgcolor("black")
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT)
# screen.register_shape("start_400x150.gif") # PNG for start button
screen.tracer(0)
# Create Paddle A, Player 1 (left Side)
paddleA = Turtle()
paddleA.hideturtle()
paddleA.shape("square")
paddleA.shapesize(1, 9)
paddleA.color("white")
paddleA.penup()
paddleA.setheading(90)
paddleA.setx(-900)
# Create Paddle B, Player 2 (Right Side)
paddleB = paddleA.clone()
paddleB.setx(900)
# Create Ball making it move in diagonal direction in fixed speed
ball = Turtle()
ball.shape("square")
ball.color("white")
ball.shapesize(1.8)
ball.penup()
ball.x = 5 # user properties
ball.y = 5
# Middle Line
midLine = Turtle()
midLine.hideturtle()
midLine.color("white")
midLine.pensize(2)
midLine.penup()
midLine.sety(-WINDOW_HEIGHT/2)
midLine.pendown()
midLine.sety(WINDOW_HEIGHT/2)
# Scoring System for Player A(1)
ScoreSystemA = Turtle()
ScoreSystemA.hideturtle()
ScoreSystemA.color("White")
ScoreSystemA.penup()
ScoreSystemA.setposition(-480, 420)
ScoreSystemA.write(f"P1 Score: {AScore}", align='center', font=SMALL_FONT)
# Scoring System for Player B(2)
ScoreSystemB = Turtle()
ScoreSystemB.hideturtle()
ScoreSystemB.color("White")
ScoreSystemB.penup()
ScoreSystemB.setposition(480, 420)
ScoreSystemB.write(f"P2 Score: {BScore}", align='center', font=SMALL_FONT)
# Create Menu
mainCanvas = Turtle()
mainCanvas.hideturtle()
mainCanvas.color("cyan")
mainCanvas.begin_fill()
for heading, distance in fd_list:
mainCanvas.setheading(heading)
mainCanvas.forward(distance)
mainCanvas.end_fill()
# Create Title
mainTitle = Turtle()
mainTitle.hideturtle()
mainTitle.penup()
mainTitle.sety(270)
mainTitle.write("Main Menu", align="center", font=MENU_FONT)
# Create Start Option
startGame = Turtle()
startGame.penup()
startGame.sety(100)
# startGame.shape("start_400x150.gif")
startGame.shape('circle')
startGame.shapesize(20)
# Key Binding Main Menu
screen.onscreenclick(clickStart)
screen.update()
screen.mainloop()
As you can see, I've also made a number of other changes to streamline your code and make it behave properly in an event-driven environment.
This question already has answers here:
Problems with moving an enemy towards a character in pygame
(1 answer)
Pygame doesn't let me use float for rect.move, but I need it
(2 answers)
Closed 8 months ago.
So I have a functioning shooting mechanic in python pygame for a top down shooter, where I am using the mouse position to aim the bullets by working out the angles, however when I do this, the bullets are shooting slightly off where the mouse position is. for instance: the mouse would be where the red arrow is drawn and the bullets will be shooting by a small amount in the wrong direction
Any help would be appreciated
code below:
main.py:
#-------------Imports-------------#
import pygame,sys
#import globals
from background import*
from player import*
#-------------Constants-------------#
WIDTH,HEIGHT = 500,500
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
CLOCK = pygame.time.Clock()
BLACK = (0, 0, 0)
#-------------Instances-------------#
bg = Background()
player = Player()
#-------------Functions-------------#
def draw():
WINDOW.fill(BLACK)
bg.update(WINDOW)
player.update(WINDOW)
pygame.display.update()
#-------------Main Game Loop-------------#
def main():
#globals.intialise()
while 1:
CLOCK.tick(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
draw()
#globals.game_ticks += 1
if __name__ == "__main__":
main()
player.py
#-------------Imports-------------#
import pygame,math
#import globals
#-------------Constants-------------#
WIDTH,HEIGHT = 500,500
PLAYER_COLOUR = (255, 212, 112)
BLACK = (0,0,0)
PI = 3.14159265359
#-------------Classes-------------#
class Bullet:
def __init__(self,origin,angle):
self.speed = 20
self.x_speed,self.y_speed = self.speed*math.cos(math.radians(angle)),self.speed*math.sin(math.radians(angle))
self.rect = pygame.Rect(origin[0],origin[1],5,5)
def __del__(self):
pass
def update(self,window):
# move bullet
self.rect.x += self.x_speed
self.rect.y += self.y_speed
# draw bullet
pygame.draw.rect(window,BLACK,self.rect)
# check if bullet is out of the screen
if self.rect.x > WIDTH or self.rect.x < 0:
return -1
elif self.rect.y > HEIGHT or self.rect.y < 0:
return -1
class Player:
def __init__(self):
self.sprite = pygame.transform.scale(pygame.image.load("sprites/temp_player.png"),(50,50))
self.rect = pygame.Rect(250,250,50,50)
self.center = (self.rect.x,self.rect.y)
self.bullets = []
self.fire_rate = 12
def shoot(self,angle,window):
# update all bullets and delete if bullet is out of screen
for bullet in self.bullets:
if bullet.update(window) == -1:
self.bullets.remove(bullet)
del bullet
# instantiate bullet if mouse button pressed
#if pygame.mouse.get_pressed()[0] and globals.game_ticks % self.fire_rate == 0:
if pygame.mouse.get_pressed()[0]:
self.bullets.append(Bullet(self.rect.center,-angle))
def update(self,window):
mx,my = pygame.mouse.get_pos()
# find distance between mouse position and player position
diff_x,diff_y = mx-self.rect.x,my-self.rect.y
# word out angle between mouse and player
angle_rad = math.atan2(diff_y,diff_x)
angle = -360*angle_rad/(2*PI)
# adjust angle according to where we want to rotate the player
# when angle is bottom left
if abs(angle) > 90 and angle < 0:
a = 270-abs(angle)
# when angle is top left
elif abs(angle) > 90:
a = angle-90
# when angle is to the right
else:
a = angle - 90
# create new sprite that is rotated
rotated_image = pygame.transform.rotate(self.sprite,a)
# replace current rectangle with rotated sprite
self.rect = rotated_image.get_rect(center = self.center)
self.shoot(angle,window)
# add image to the screen
#window.blit(pygame.transform.rotate(self.sprite,a),self.rect)
background.py:
#-------------Imports-------------#
import pygame,random,ast,time,globals
#-------------Constants-------------#
WIDTH,HEIGHT = 500,500
TILE_DIMENSION = 9
TILE_SIZE = int(round(WIDTH/TILE_DIMENSION,0))
TO_EDGE = int((TILE_DIMENSION+1)/2)
#-------------Classes-------------#
class Background:
def __init__(self):
self.tiles = self.generate_screen()
self.centre = [2,2]
self.right = 0
self.up = 0
self.speed = 5
def generate_screen(self):
# generate original chunk of tiles
tiles = [[random.randint(100,200) for i in range(TILE_DIMENSION)] for j in range(TILE_DIMENSION)]
# eventually use image instead of random RGB value
return tiles
def movement(self,tile_rects):
keys = pygame.key.get_pressed()
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
# if player is on tile to the left of centre
if (self.right - self.speed) < -TILE_SIZE:
# reset movement and adjust centre
self.right = 0
self.centre[0] -= 1
else:
# add to movement if not on next tile
self.right -= self.speed
# move all rectangles in background to simulate player moving
for i in range(len(tile_rects)):
for j in range(len(tile_rects[0])):
tile_rects[i][j].x += self.speed
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
# if player is on tile to the right of centre
if (self.right + self.speed) > TILE_SIZE:
# reset movement and adjust centre
self.right = 0
self.centre[0] += 1
else:
# add to movement if not on next tile
self.right += self.speed
# move all rectangles in background to simulate player moving
for i in range(len(tile_rects)):
for j in range(len(tile_rects[0])):
tile_rects[i][j].x -= self.speed
if keys[pygame.K_w] or keys[pygame.K_UP]:
# if player is on tile above the centre
if (self.up + self.speed) > TILE_SIZE:
# reset movement and adjust centre
self.up = 0
self.centre[1] -= 1
else:
# add to movement if not on next tile
self.up += self.speed
# move all rectangles in background to simulate player moving
for i in range(len(tile_rects)):
for j in range(len(tile_rects[0])):
tile_rects[i][j].y += self.speed
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
# if player is on tile below the centre
if (self.up - self.speed) < -TILE_SIZE:
# reset movement and adjust centre
self.up = 0
self.centre[1] += 1
else:
# add to movement if not on next tile
self.up -= self.speed
# move all rectangles in background to simulate player moving
for i in range(len(tile_rects)):
for j in range(len(tile_rects[0])):
tile_rects[i][j].y -= self.speed
return tile_rects
def update(self,window):
# rendering in brand new map chunks
# if part of the chunk trying to be rendered in is non-existant in the 2D map array to the left
if self.centre[0]-TO_EDGE < 0:
# check how many tiles it is offset by
for i in range(0-(self.centre[0]-TO_EDGE)):
# add new column of values at the beginning of the 2D array
for i in range(len(self.tiles)):
self.tiles[i].insert(0,random.randint(120,230))
# due to whole array being shifted to the right, adjust the centre accordingly
self.centre[0] += 1
# if part of the chunk trying to be rendered is non-existant in the 2D map array to the right
if self.centre[0]+TO_EDGE >= len(self.tiles[0]):
# check how many tiles it is offset by
for i in range((self.centre[0]+TO_EDGE)-(len(self.tiles[0])-1)):
# add a new column of values at the end of the 2D array
for i in range(len(self.tiles)):
self.tiles[i].append(random.randint(120,230))
# if part of the chunk trying to be rendered in is non-existant in the 2D array at the top
if self.centre[1]-TO_EDGE < 0:
# check how many tiles it is offset by
for i in range(0-(self.centre[1]-TO_EDGE)):
# add a new row at the top of the 2D array
self.tiles.insert(0,[random.randint(120,230) for i in range(len(self.tiles[0]))])
# due to whole array shifting downwards, adjust the centre accordingly
self.centre[1] += 1
# if part of the chunk trying to be rendered in is non-existant in the 2D array at the bottom
if self.centre[1]+TO_EDGE >= len(self.tiles):
# check how many tiles it is offset by
for i in range((self.centre[1]+TO_EDGE)-(len(self.tiles)-1)):
# add a new row at the bottom of the 2D array
self.tiles.append([random.randint(120,230) for i in range(len(self.tiles[0]))])
# determining which tiles should be rendered in according to the centre(where player would be)
t = []
for i in range(TILE_DIMENSION+2):
t.append([])
for j in range(TILE_DIMENSION+2):
try:
t[i].append(self.tiles[i+(self.centre[1]-TO_EDGE)][j+(self.centre[0]-TO_EDGE)])
except:
pass
# create a rectangle for each tile that is rendered in
tile_rects = [[pygame.Rect((i-1)*TILE_SIZE-self.right,(j-1)*TILE_SIZE+self.up,TILE_SIZE,TILE_SIZE) for i in range(TILE_DIMENSION+2)] for j in range(TILE_DIMENSION+2)]
tile_rects = self.movement(tile_rects)
# draw all rectangles
for i in range(TILE_DIMENSION+2):
for j in range(TILE_DIMENSION+2):
try:
pygame.draw.rect(window,(0,int(t[i][j]),0),tile_rects[i][j])
except:
pass
the background script doesnt affect anything, its just there as a background to make it easier to see, and you may have to make your own temp_player.png image to make it compatible
i am making a pong game
i managed to code the ball to bounce around the window
but it seems that the code, for using user input to get the rectangles to move isn't working.
when i run the code i have been getting a traceback to my move.ball command relating to making the ball bounce, and i believe that may be stopping the rest of the code from running. I have included the full code in case any errors can be spotted in it.
from tkinter import *
import tkinter as tkr
import time
tk = tkr.Tk()
Canvas = tkr.Canvas(tk, width=300, height=400)
Canvas.grid()
ball = Canvas.create_oval(3,3,40,40,fill="light blue")
player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 120, 380)
player2 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player2, 120, -5)
x = 1
y = 3
while True:
Canvas.move(ball,x,y)
pos = Canvas.coords(ball)
if pos[3] >= 400 or pos[1] <= 0:
y = -y
if pos[2] >= 300 or pos[0] <= 0:
x = -x
tk.update()
time.sleep(0.025)
pass
tk.mainloop()
def left(event):
x == -10
y == 0
Canvas.move(player1, x, y)
def right(event):
x == 10
y == 0
Canvas.move(player1, x, y)
def up(event):
x == 0
y == -10
Canvas.move(player1, x, y)
def down(event):
x == -10
y == 0
Canvas.move(player1, x, y)
root.bind("<Left>", left)
root.bind("<Right>", right)
root.bind("<Up>", up)
root.bind("<Down>", down)
tk.mainloop()
i am getting this trace back-
Traceback (most recent call last):
File "C:/Users/amarb/Desktop/code/pong.py", line 15, in <module>
Canvas.move(ball,x,y)
File "C:\Users\amarb\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2949, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"
.!canvas is the internal identifier of the canvas widget that you created. This error is happening because you try to run code after mainloop() returns, which is after the window being destroyed. You can't interact with widgets after they have been destroyed.
Edit: New. How to get the paddle moving and hit the paddle. I only used one player at a time. You will do the rest. I had to modify some and add some new functions. I am using new python 3.11.0b3.
from tkinter import *
import tkinter as tkr
import time
import random
tk = tkr.Tk()
width=300
height=400
Canvas = tkr.Canvas(tk, width=width, height=height)
Canvas.pack()
tk.update()
ball = Canvas.create_oval(3,3,25,25,fill="red")
Canvas.move(ball, 245, 100)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
ball_x = starts[0]
ball_y = 3
player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 180, 380)
player2 = Canvas.create_rectangle(20,5,90,30,fill="blue")
Canvas.move(player2, 120, -5)
paddle_x = 0
def hit_bat(pos):
bat_pos = Canvas.coords(player1) #retrieve the coordinates of the bat position - note the ball coordinates are being passed
if pos[2] >= bat_pos[0] and pos[0] <= bat_pos[2]: #if the right side of the ball (that is the x right hand coordinate) is greater than the left side of the bat, AND the left side of the ball is less than the right side of the bat ....move etc
if pos[3]>= bat_pos[1] and pos[3] <= bat_pos[3]: #if the bottom of the ball (pos[3]) is between the paddle's top [bat pos[1]) and bottom (pos[3])
return True
return False
def draw_ball():
global ball_x, ball_y
Canvas.move(ball, ball_x, ball_y)
pos = Canvas.coords(ball)
if pos[1] <= 0:
ball_y = 6
if pos[3] >= height:
ball_y = -6
#Call the hit_bat function
if hit_bat(pos) == True: #if the hit_bat function returns a value of True, the direction of the ball is changed
ball_y = -6 #if the ball hits the bat, then send the ball flying up at a rate of -6 (higher the number the faster the fly!)
if pos[0] <= 0:
ball_x = 6
if pos[2]>= width:
ball_x = -6
tk.update()
def hit_paddle(pos):
paddle_pos = Canvas.coords(ball)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def draw_paddle():
global paddle_x
Canvas.move(player1, paddle_x, 0)
pos = Canvas.coords(player1)
if pos[0] <= 0:
paddle_x = 0
elif pos[2] >= width:
paddle_x = 0
time.sleep(0.02)
tk.update()
def move_left(event):
global paddle_x
paddle_x = -2
def move_right(event):
global paddle_x
paddle_x = 2
Canvas.bind_all("<KeyPress-Left>", move_left)
Canvas.bind_all("<KeyPress-Right>", move_right)
while True:
draw_ball()
draw_paddle()
New image:
So, as a personal project, I'm trying to build a PONG game. I have set up the window, all the code, the paddles move fine... It's just the ball. The ball moves once, and then never again. I have no clue whether this is an issue with the definition of the ball's movement or if it's just me not understanding how to use mainloop() correctly, but if I knew how to get the ball to keep moving FOREVER, that would be wonderful. If you want me to explain what I intend for the ball to do, look below the code
#Setting up the window
from tkinter import *
from time import *
HEIGHT=800
WIDTH=1280
window=Tk()
window.title('PONG!')
c=Canvas(window,width=WIDTH,height=HEIGHT,bg='black')
c.pack()
MID_X=WIDTH/2
MID_Y=HEIGHT/2
def pongstick():
return c.create_polygon(0,0, 10,0, 10,70, 0,70, fill='white')
def ball():
return c.create_oval(MID_X-10,MID_Y-10, MID_X+10,MID_Y+10, fill='white')
pong1=pongstick()
pong2=pongstick()
ballplay=ball()
MID_Y=MID_Y-35
c.move(pong1, 40, MID_Y)
c.move(pong2, WIDTH-40, MID_Y)
#Scores
player1p=0
player2p=0
ballspeed=10
#Movement of the paddles
stickspeed=10
def move_stick(event):
if event.keysym == 'w':
c.move(pong1, 0, -stickspeed)
elif event.keysym == 's':
c.move(pong1, 0, stickspeed)
if event.keysym == 'Up':
c.move(pong2, 0, -stickspeed)
elif event.keysym == 'Down':
c.move(pong2, 0, stickspeed)
#Ball movement logic
ballspeed=10
ballY=ballspeed
ballX=ballspeed
ballXadd=WIDTH/2
ballYadd=HEIGHT/2
def move_ball(ballX,ballY,ballXadd,ballYadd,player1p,player2p):
#Movement + edge hit detector
c.move(ballplay, ballX, ballY)
ballXadd=ballXadd+ballX
ballYadd=ballYadd+ballY
if ballXadd > WIDTH:
player2p=player2p+1
c.move(ball,MID_X-10,MID_Y-10)
ballXadd=0
ballYadd=0
elif ballXadd < WIDTH:
player1p=player1p+1
c.move(ball,MID_X-10,MID_Y-10)
ballXadd=0
ballYadd=0
elif ballYadd > HEIGHT:
if ballX == ballspeed:
ballY = -ballspeed
elif ballX == -ballspeed:
ballY = ballspeed
elif ballYadd < HEIGHT:
if ballX == ballspeed:
ballY = ballspeed
elif ballX == -ballspeed:
ballY = -ballspeed
#GAME!
c.bind_all('<Key>',move_stick)
while 5<7:
move_ball(ballX,ballY,ballXadd,ballYadd,player1p,player2p)
mainloop()
So, ballX is moving along the x axis, and ballY... Obvious
ballX/Yadd are the variables that set hit detection against the edge of the window. If ballYadd is more or less than the height, then the trajectory will change. If ballXadd is more or less than the width, the ball is returned to the middle, and a point (player1p or player2p, depending on which side it hit) and the trajectory resets.
I am just starting out learning pygame and livewires, and I'm trying to make a single-player pong game, where you just hit the ball, and it bounces around until it passes your paddle (located on the left side of the screen and controlled by the mouse), which makes you lose. I have the basic code, but the ball doesn't stay on the screen, it just flickers and doesn't remain constant. Also, the paddle does not move with the mouse. I'm sure I'm missing something simple, but I just can't figure it out. Help please! Here's what I have:
from livewires import games
import random
games.init(screen_width=640, screen_height=480, fps=50)
class Paddle(games.Sprite):
image=games.load_image("paddle.bmp")
def __init__(self, x=10):
super(Paddle, self).__init__(image=Paddle.image,
y=games.mouse.y,
left=10)
self.score=games.Text(value=0, size=25, top=5, right=games.screen.width - 10)
games.screen.add(self.score)
def update(self):
self.y=games.mouse.y
if self.top<0:
self.top=0
if self.bottom>games.screen.height:
self.bottom=games.screen.height
self.check_collide()
def check_collide(self):
for ball in self.overlapping_sprites:
self.score.value+=1
ball.handle_collide()
class Ball(games.Sprite):
image=games.load_image("ball.bmp")
speed=5
def __init__(self, x=90, y=90):
super(Ball, self).__init__(image=Ball.image,
x=x, y=y,
dx=Ball.speed, dy=Ball.speed)
def update(self):
if self.right>games.screen.width:
self.dx=-self.dx
if self.bottom>games.screen.height or self.top<0:
self.dy=-self.dy
if self.left<0:
self.end_game()
self.destroy()
def handle_collide(self):
self.dx=-self.dx
def end_game(self):
end_message=games.Message(value="Game Over",
size=90,
x=games.screen.width/2,
y=games.screen.height/2,
lifetime=250,
after_death=games.screen.quit)
games.screen.add(end_message)
def main():
background_image=games.load_image("background.bmp", transparent=False)
games.screen.background=background_image
paddle_image=games.load_image("paddle.bmp")
the_paddle=games.Sprite(image=paddle_image,
x=10,
y=games.mouse.y)
games.screen.add(the_paddle)
ball_image=games.load_image("ball.bmp")
the_ball=games.Sprite(image=ball_image,
x=630,
y=200,
dx=2,
dy=2)
games.screen.add(the_ball)
games.mouse.is_visible=False
games.screen.event_grab=True
games.screen.mainloop()
main()
I can't help you because you did not post the complete code here. At least, I do not see where you're updating the positions of the sprites (self.x += self.dx somewhere?) and updating the draw to screen. You're also not utilising your classes in the main() function.
That said, I'm seeing
def __init__(self, x=10):
and inside the constructor you never used the x variable. That worries me, too.
Consider using the Paddle and Ball class as a Sprite, like the following:
if __name__ == '__main__':
background_image = games.load_image("background.bmp", transparent=False)
games.screen.background = background_image
the_paddle = Puddle()
games.screen.add(the_paddle)
the_ball = Ball()
games.screen.add(the_ball)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
Note I've taken the liberty to make your code read more Pythonic. I have never used livewires, however, so my code may not function. But it should point you to the right direction. Good luck!
Why are you using livewires? You can use only pygame for a pong game.
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480)) # window size
pygame.display.set_caption("Simple pong") # window title
# this is a rect that contains the ball
# at the beginning it is set in the center of the screen
ball_rect = pygame.Rect((312, 232), (16, 16))
# speed of the ball (x, y)
ball_speed = [4, 4]
# this contains your paddle
# vertically centered on the left side
paddle_rect = pygame.Rect((8, 200), (8, 80))
# 1 point if you hit the ball
# -5 point if you miss the ball
score = 0
# load the font for displaying the score
font = pygame.font.Font(None, 30)
# mainloop
while True:
# event handler
for event in pygame.event.get():
# quit event => close the game
if event.type == pygame.QUIT:
exit(0)
# control the paddle with the mouse
elif event.type == pygame.MOUSEMOTION:
paddle_rect.centery = event.pos[1]
# correct paddle position if it's going out of window
if paddle_rect.top < 0:
paddle_rect.top = 0
elif paddle_rect.bottom >= 480:
paddle_rect.bottom = 480
# this test if up or down keys are pressed
# if yes move the paddle
if pygame.key.get_pressed()[pygame.K_UP] and paddle_rect.top > 0:
paddle_rect.top -= 5
elif pygame.key.get_pressed()[pygame.K_DOWN] and paddle_rect.bottom < 480:
paddle_rect.top += 5
# update ball position
# this move the ball
ball_rect.left += ball_speed[0]
ball_rect.top += ball_speed[1]
# these two if block control if the ball is going out on the screen
# if it's going it reverse speed to simulate a bounce
if ball_rect.top <= 0 or ball_rect.bottom >= 480:
ball_speed[1] = -ball_speed[1]
if ball_rect.right >= 640:
ball_speed[0] = -ball_speed[0]
# this control if the ball touched the left side
elif ball_rect.left <= 0:
score -= 5
# reset the ball to the center
ball_rect = pygame.Rect((312, 232), (16, 16))
# test if the ball is hit by the paddle
# if yes reverse speed and add a point
if paddle_rect.colliderect(ball_rect):
ball_speed[0] = -ball_speed[0]
score += 1
# clear screen
screen.fill((255, 255, 255))
# draw the ball, the paddle and the score
pygame.draw.rect(screen, (0, 0, 0), paddle_rect) # paddle
pygame.draw.circle(screen, (0, 0, 0), ball_rect.center, ball_rect.width/2) # ball
score_text = font.render(str(score), True, (0, 0, 0))
screen.blit(score_text, (320-font.size(str(score))[0]/2, 5)) # score
# update screen and wait 20 milliseconds
pygame.display.flip()
pygame.time.delay(20)