I can not seem to find the bug in my program. The program will run without errors however, my paddle image will not move. I have spent hours researching and tried multiple different changes. I am using python 3.4.1. My goal is to create a paddle game of sorts. Thank you.
# Python game
import pygame
import sys
pygame.init()
ball_image = pygame.image.load("ball.png")
ballrect = ball_image.get_rect()
paddle_image = pygame.image.load("paddle.png")
paddlerect = paddle_image.get_rect()
screen_w = 825
screen_h = 727
size = [screen_w, screen_h]
speed = [1, 1]
black = (0, 0, 0)
blue = (0,0,255)
screen = pygame.display.set_mode(size)
class paddle(object):
paddle_speed = [0, 0]
paddle_speed_d = [0, -1]
paddle_speed_u = [0, 1]
image = paddle_image
rect = image.get_rect()
x = 20
y = screen_h / 2
w = rect.width
h = rect.height
def __init__(self, side):
self.side = side
if side == "l":
x == width - 20 - w
def move_d(self):
self.rect.move(self.paddle_speed_d)
def move_u(self):
self.rect.move(self.paddle_speed_u)
pad1 = paddle("r")
#pad2 = paddle("l")
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
pad1.move_d()
if event.key == pygame.K_UP:
pad1.move_u()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > screen_w:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > screen_h:
speed[1] = -speed[1]
#if ballrect.bottom == 0:
#ballrect.y = screen_h
#if ballrect.top == screen_h:
#ballrect.bottom = 0
screen.fill(black)
screen.blit(ball_image, ballrect)
screen.blit(pad1.image, pad1.rect)
pygame.display.flip()
pygame.time.wait(1)
From the documentation:
move()
Returns a new rectangle that is moved by the given offset. The x and y arguments can be any integer value, positive or negative.
move_ip()
Same as the Rect.move() method, but operates in place.
If you change self.rect.move(self.paddle_speed_d) to self.rect.move_ip(self.paddle_speed_d) in move_d(), that should do the trick (likewise for move_u().
(This still leaves the problem that move_d() is only called on key down. This means you have to repeatedly press the down key to move your paddle. This may be what you want, but if it's not, consider having an update function move the paddle on every loop, and have move_d() and move_u() set the velocity of the paddle.)
Thanks for trying to help, but I discovered my problem. The format in which I saved my image was not showing movement. I changed my image and now it works.
Related
I'm working on a space shooter game where you have to dodge asteroids and shoot them. Right now, I'm working on collision for the asteroids. I'm just testing one asteroid for now, but the asteroid passes straight through the ship and doesn't end the game like I want it to.
Here's the code:
import pygame
pygame.init()
#initalizing all the clunky variables
size = (900,700)
BLACK = (0, 0, 30)
RED = (255, 0, 0)
YELLOW = (0, 255, 0)
x_pos = 450
y_pos = 600
global x_pos
global y_pos
direct = 0
w, h = 100, 100
screen = pygame.display.set_mode(size)
klok = pygame.time.Clock()
#main ship image and its rotations
ship = pygame.image.load('u-sniper.png')
shipL = pygame.transform.rotate(ship, 270)
shipR = pygame.transform.rotate(ship, 90)
shipD = pygame.transform.rotate(ship, 180)
#init hitbox
hitbox = ship.get_rect()
hitbox.center = w//2,h//2
#funct for drawing ship
def drawShip():
if direct == 0:
screen.blit(ship, [x_pos,y_pos])
if direct == 1:
screen.blit(shipR, [x_pos,y_pos])
if direct == 2:
screen.blit(shipD, [x_pos,y_pos])
if direct == 3:
screen.blit(shipL, [x_pos,y_pos])
#asteroid obstacles (these are meant to collide with the ship)
class asteroid:
def __init__(self,x,y,spawn):
self.x = x
self.y = y
self.spawn = spawn
def drawA(self):
if self.spawn == 1:
pygame.draw.circle(screen, RED, (self.x,self.y), 30)
def moveA(self):
self.y += 8
if self.y > 650:
self.spawn = 0
done = False
roid = asteroid(450,0,1)
#asteroid hitbox init
rect_asteroid = (roid.x, roid.y, 30, 30)
#here is where its going wrong, collision dosent register
def checkForCollisions():
collide = pygame.Rect.colliderect(hitbox,rect_asteroid)
if collide == True:
done = True
#loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
drawShip()
roid.drawA()
roid.moveA()
#calling fuction, but it dosent work
checkForCollisions()
#if branch that moves the ship
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_pos -= 5
direct = 0
if event.key == pygame.K_DOWN:
y_pos += 5
direct = 2
if event.key == pygame.K_RIGHT:
x_pos += 5
direct = 3
if event.key == pygame.K_LEFT:
x_pos -= 5
direct = 1
#collision between screen boundaries
if x_pos > 850:
x_pos -= 6
if x_pos < -50:
x_pos += 6
if y_pos > 650:
y_pos -= 6
if y_pos < 0:
y_pos += 6
pygame.display.flip()
klok.tick(60)
pygame.quit()
I tried multiple colliderect functions, but it just results in one thing: the ship and the asteroid pass straight through each other.
The pygame.Rect objects do not magically update their position when you change the coordinates used to draw the object. You need to update the location stored in the pygame.Rect objects before collision detection. Or just create the objects in checkForCollisions:
def checkForCollisions():
hitbox.topleft = (x_pos, y_pos)
rect_asteroid = (roid.x, roid.y, 30, 30)
collide = hitbox.colliderect(rect_asteroid)
return collide
The code below bounces a ball but for some reason the ball goes through the ground after it finishes its bounces. Anyone Know Why? The idea of the code is a ball starts at the top left corner and then falls and bounces and then goes up and back down and so on until it stops bouncing, but when it stops bouncing it starts jittering and slowly sinks through the ground. Idk why and I cant figure it out. Anyone know why? Thanks for the help
import pygame
pygame.init()
#All keyboard and mouse input will be handled by the following function
def handleEvents():
#This next line of code lets this function access the dx and dy
#variables without passing them to the function or returning them.
global dx,dy
#Check for new events
for event in pygame.event.get():
#This if makes it so that clicking the X actually closes the game
#weird that that wouldn't be default.
if event.type == pygame.QUIT:
pygame.quit(); exit()
#Has any key been pressed?
elif event.type == pygame.KEYDOWN:
#Escape key also closes the game.
if event.key == pygame.K_ESCAPE:
pygame.quit(); exit()
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
dx = dx + 5
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
dx = dx - 5
elif event.key == pygame.K_UP or event.key == pygame.K_w:
dy = dy - 5
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
dy = dy + 5
width = 1000
height = 600
size = (width, height)
black = (0, 0, 0) #r g b
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()
x = 0
y = 0
dx = 3
dy = 3
done = False
while not done:
handleEvents()
#Move the ball
x = x + dx
y = y + dy
ballrect.topleft = (x,y)
#PART A
if ballrect.left < 0 or ballrect.right > width:
dx = -dx
if ballrect.top < 0 or ballrect.bottom > height:
dy = -dy
'''If the ball is outside of the range delta y,
then delta y becomes the negative version of it, and the same goes
for delta x if it is outside the boundries of delta x
'''
#PART B
dy = dy * 0.99
dx = dx * 0.99
'''Could be useful if you want
the ball to stop moving after a certain amount of time,
or the opposite, it could make the ball slowly move a greater distance each frame'''
#PART C
dy = dy + 0.3
'''dy slowly gets .3 added to itself each frame, making the bounce
smaller each time until eventually it stops fully'''
#Draw everything
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
#Delay to get 30 fps
clock.tick(30)
pygame.quit()
Since the falling speed of the ball is greater than 1 pixel, you have to make sure that the ball does not fall below the lower edge of the window.
You need to constrain the bottom of the ball to the bottom of the window:
done = False
while not done:
# [...]
x = x + dx
y = y + dy
ballrect.topleft = (x,y)
#PART A
if ballrect.left < 0 or ballrect.right > width:
dx = -dx
if ballrect.top < 0:
dy = -dy
if ballrect.bottom > height:
ballrect.bottom = height # <---
y = ballrect.top # <---
dy = -dy
# [...]
This question already has answers here:
Not letting the character move out of the window
(2 answers)
Closed 2 years ago.
I'm trying to make the paddles move up and down : 1 rectangle. moving with w/a/s/d keys 2 rectangle. with arrows. I'm having trouble making the paddles stay in the screen?
You will make two “paddles” (i.e. Rectangles), one on the left side and one on the right side of the screen. These rectangles should be taller than they are wide (i.e. Look like a paddle from a pong game). The paddle on the left should be able to move up and down using the w and s keys, the paddle on the right should move up and down using the up and down arrow keys. Both paddles should not be allowed to leave the top or bottom of the screen. (* for this assignment you are just creating the paddles and getting them moving properly,, no balls necessary). Try to avoid hardcoded values.
# import the necessary modules
import pygame
import sys
#initialize pygame
pygame.init()
# set the size for the surface (screen)
screen_h = 800
screen_w = 600
screen = pygame.display.set_mode((screen_h,screen_w),0)
# set the caption for the screen
pygame.display.set_caption("Template")
# define colours you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)
#initialize variables for player
#variables for first rectangle
R1x = 740
R1y = 300
R1w = 50
R1h = 132
R1dx = 0
R1dy = 0
#variables for second rectangle
R2x = 10
R2y = 300
R2w = 50
R2h = 132
R2dx = 0
R2dy = 0
#speed
speed = 3
screen.fill(BLACK)
# set main loop to True so it will run
main = True
# main loop
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
R1dx = 0
R1dy = -speed
elif event.key == pygame.K_DOWN:
R1dx = 0
R1dy = speed
if event.key == pygame.K_w:
R2dx = 0
R2dy = -speed
elif event.key == pygame.K_s:
R2dx = 0
R2dy = speed
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
R1dx = 0
R1dy = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
R2dx = 0
R2dy = 0
# move the x and y positions of the rectangles
oldR1x = R1x
oldR1y = R1y
R1x = R1x + R1dx
R1y = R1y + R1dy
if R1x >= screen_w-50:
R1x = oldR1x
R1y = oldR1y
if R1x<= 50:
R1x = oldR1x
R1y = oldR1y
if R1y>= screen_h-50:
R1x = oldR1x
R1y = oldR1y
if R1y<= 50:
R1x = oldR1x
R1y = oldR1y
# draw the shapes, in this case the blue rectangles
pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)
# we are using .flip() here, it basically works the same as .update()
# we will discuss this more in class (you can use either one)
pygame.display.flip()
# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()
First of all you have swapped the screen width (screen_w) and the screen height (screen_h). It has to be:
# set the size for the surface (screen)
screen_w = 800
screen_h = 600
screen = pygame.display.set_mode((screen_w,screen_h),0)
The paddles move just up an down, so it sufficient to limit the y coordinate of the pddels to the range [0, screen_h-R1h]. Note, R1y respectively R2y are the top coordinate of the paddles:
R1y = max(0, min(screen_h-R1h, R1y + R1dy))
R2y = max(0, min(screen_h-R2h, R2y + R2dy))
Furthermore, the screen has to be cleared in the main application loop (screen.fill(BLACK)):
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
if event.type ==pygame.KEYDOWN:
# [...]
# move the x and y positions of the rectangles
R1y = max(0, min(screen_h-R1h, R1y + R1dy))
R2y = max(0, min(screen_h-R2h, R2y + R2dy))
# clear screen
screen.fill(BLACK)
# draw the shapes, in this case the blue rectangles
pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)
# we are using .flip() here, it basically works the same as .update()
# we will discuss this more in class (you can use either one)
pygame.display.flip()
Note, it the paddles would move along the x axis too (left, right), then the restriction of the x coordinate is similar:
R1x = max(0, min(screen_w-R1w, R1x + R1dx))
R2x = max(0, min(screen_w-R2w, R2x + R2dx))
So how would I be able to keep the green rectangle from getting outside of the box I have provided? If possible without using any classes. I would like to make a small map for levels within the given window so is there any way I can keep the rectangle from slipping through the lines? Or is it possible to force the rectangle to stop moving after hitting a certain point?
import pygame
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
#Functions
def drawSquare(screen, x, y):
pygame.draw.rect(screen, BLACK, [x, y, 21, 21])
pygame.draw.rect(screen, GREEN, [3+x, 3+y, 15, 15])
def drawMap():
pygame.draw.rect(screen, BLACK, [0, 1, 100, 100],5)
pygame.init()
# Set the width and height of the screen [width, height]
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Loading pictures
#Variables
# Speed in pixels per frame
x_speed = 0
y_speed = 0
# Current position
x_coord = 10
y_coord = 10
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.KEYDOWN:
# Figure out if it was an arrow key. If so
# adjust speed.
if event.key == pygame.K_LEFT:
x_speed = -3
elif event.key == pygame.K_RIGHT:
x_speed = 3
if x_coord > 200:
x_speed = 0
elif event.key == pygame.K_UP:
y_speed = -3
elif event.key == pygame.K_DOWN:
y_speed = 3
elif event.type == pygame.KEYUP:
# If it is an arrow key, reset vector back to zero
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed = 0
x_coord = x_coord + x_speed
y_coord = y_coord + y_speed
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# --- Drawing code should go here
screen.fill(WHITE)
drawSquare(screen, x_coord, y_coord)
drawMap()
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(45)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
If I understand your question, you want to keep the square you are drawing within the view bounds. I think this will do it for you (the top two lines are already in your code above, just to show where this would go).
x_coord = x_coord + x_speed
y_coord = y_coord + y_speed
# consider moving these to the top and using them in your drawMap function
map_x = 0
map_y = 1
map_width = 100
map_height = 100
x_coord = max(map_x, x_coord)
y_coord = max(map_y, y_coord)
# coordinates must be less than size of container
# the 21 here is the size of the square you are drawing
# consider making it a variable rather than hard coded number
x_coord = min(x_coord, map_width - 21)
y_coord = min(y_coord, map_heigh - 21)
While I think if you keep on this course (avoiding using classes/using global variables) you will find your code will become unmaintainable.
Hey guys I have been trying to move the ball image in a square manner on the screen with pygame. I have tried many times but it does not work. Please see my code below.
import time
import pygame, sys
from pygame.locals import *
pygame.init()
clocks = pygame.time.Clock()
surfaceObject = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Bounce')
mousey,mousex = 0,0
imgx = 10
imgy = 10
pixmove = 60
tiger = [2,2]
movement = 'down'
background = pygame.image.load('bg.jpg').convert()
ball = pygame.image.load('ball.jpg').convert_alpha()
pygame.mixer.music.load('yeah.mp3')
while True:
time.sleep(1)
if movement == 'down':
imgx += pixmove
if imgx < 640:
tiger[0] - tiger[1]
elif movement == 'right':
imgx += pixmove
if imgx < 0:
movement = 'up'
elif movement == 'up':
imgy -= pixmove
if imgy < 0:
movement = 'left'
elif movement == 'left':
imgx -= pixmove
if imgx < 0:
movement = 'down'
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
surfaceObject.blit(background,(mousex,mousey))
surfaceObject.blit(ball,(imgx,imgy))
pygame.mixer.music.play()
pygame.display.update()
clocks.tick(50)
When I run this code the ball moves in a straight way and the ball does not bounce back when it touches the end.
My goal is to rotate the ball in a square manner across the screen. I have tried changing the pixmove variable but it did not solve my problem.
Hope you guys can help me out ..Thanx in advance
Your indentation is totally broken, so I can't say what's going.
However, moving the image in a square fashion is easy. Just create a Rect in which the object should move, and once the object leaves the Rect, change the direction.
Take a look at the following example:
import pygame
from itertools import cycle
pygame.init()
screen = pygame.display.set_mode((300, 300))
# move inside the screen (or any other Rect)
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))
timer = pygame.time.Clock()
speed = 5
up, down, left, right = (0, -speed), (0, speed), (-speed, 0), (speed, 0)
dirs = cycle([up, right, down, left])
dir = next(dirs)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise
# move player
player.move_ip(dir)
# if it's outside the screen
if not s_r.contains(player):
# put it back inside
player.clamp_ip(s_r)
# and switch to next direction
dir = next(dirs)
screen.fill(pygame.color.Color('Black'))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
pygame.display.flip()
timer.tick(25)
Since we determined that what you actually want to do is for the ball to bounce off the edges, look here :
Why isn't the ball bouncing back?
In summary, what needs to happen is that you need to keep track of the direction the ball is going (in terms of it's vertical and horizontal movement) and change them as you hit a wall.
You seriously want to fix your indenting, as that leads your code to be highly ununderstandable.
If you want to move something, such as a circle, in a square formation, that is pretty easy:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1200, 600))
x, y = (100, 100)
dir = 'right'
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill((0, 0, 0))
pygame.draw.circle(DISPLAYSURF, (255, 255, 255), (x, y), 10)
if dir == 'right':
x+=14
if x >= 1100:
dir = 'down'
elif dir == 'down':
y+=14
if y >= 500:
dir = 'left'
elif dir == 'left':
x-=14
if x <= 100:
dir = 'up'
elif dir == 'up':
y-=14
if y <= 100:
dir = 'right'
pygame.display.flip()