I am working on a basic pong pygame and I have coded my main.py, paddle.py, and ball.py files and unfortunately, when I run the game it only displays a black pygame window. My paddles, ball, net, and score are not visible... it's just blank.
Screenshot of Blank/Black Window
The following is my code, I have seperated it by each file.
I cannot seem to find what is causing this error so any feedback would be appreciated.
MAIN.PY
# Import pygame library and initialize the game engine
import pygame
from paddle import Paddle
from ball import Ball
pygame.init()
# Define some colors
BLACK = (0,0,0)
WHITE = (255,255,255)
# Open a new window
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
paddleA = Paddle(WHITE, 10, 100)
paddleA.rect.x = 20
paddleA.rect.y = 200
paddleB = Paddle(WHITE, 10, 100)
paddleB.rect.x = 670
paddleB.rect.y = 200
ball = Ball(WHITE, 10, 10)
ball.rect.x = 345
ball.rect.y = 195
# This will be a list that contains all the sprites we intend to use in game.
all_sprites_list = pygame.sprite.Group()
# Add paddles to the list of sprites
all_sprites_list.add(paddleA)
all_sprites_list.add(paddleB)
all_sprites_list.add(ball)
# Loop will carry on until user exits the game (ex. clicks the close button)
carryOn = True
# Clock will be used to control how fast the screen updates
clock = pygame.time.Clock()
# Initialize player scores
scoreA = 0
scoreB = 0
# ----- Main Program Loop -----
while carryOn:
# --- Main Event Loop ---
for event in pygame.event.get(): # user did something
if event.type==pygame.QUIT: # if user clicked close
carryOn==False # Flag that we are done so we exit this loop
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x: # Pressing the x key will quit the game
carryOn==False
# Moving the paddles when the user uses the arrow keys (Player A),
# or W/S keys (Player B)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
paddleA.moveUp(5)
if keys[pygame.K_s]:
paddleA.moveDown(5)
if keys[pygame.K_UP]:
paddleB.moveUp(5)
if keys[pygame.K_DOWN]:
paddleB.moveDown(5)
# --- Game Logic ---
all_sprites_list.update()
# Checks if the ball is bouncing against any of the 4 walls
if ball.rect.x>=690:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x<=0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y>490:
ball.velocity[1] = -ball.velocity[1]
if ball.rect.y<0:
ball.velocity[1] = -ball.velocity[1]
#Detect collisions between the ball and the paddles
if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
ball.bounce()
# --- Drawing Code ---
# clears the screen to black
screen.fill(BLACK)
# draws the net
pygame.draw.line(screen, WHITE, [349, 0], [349, 500], 5)
# Draws all sprites in one go (I only have 2 for now)
all_sprites_list.draw(screen)
# Display scores
font = pygame.font.Font(None, 74)
text = font.render(str(scoreA), 1, WHITE)
screen.blit(text, (250,10))
text.font.render(str(scoreB), 1, WHITE)
screen.blit(text (420,10))
# update screen with what we've drawn
pygame.display.flip()
# limit to 60 frames per second
clock.tick(60)
# Once we have exited the main program loop we stop the game engine
pygame.quit()
PADDLE.PY
import pygame
BLACK = (0,0,0)
class Paddle(pygame.sprite.Sprite):
# This class represents a paddle. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the paddle and it's x and y position (width nd height).
# Set the background color as transparent
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# Draw the paddle which is a rectangle
pygame.draw.rect(self.image, color, [0,0, width, height])
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
def moveUp(self, pixels):
self.rect.y -= pixels
# Check that you are not going too far (off screen)
if self.rect.y < 0:
self.rect.y = 0
def moveDown(self, pixels):
self.rect.y += pixels
# Check that you are not going too far (off screen)
if self.rect.y > 400:
self.rect.y = 400
BALL.PY
import pygame
from random import randint
BLACK = (0,0,0)
class Ball(pygame.sprite.Sprite):
#This class represents a ball. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the ball, its width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# Draw the ball (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
self.velocity = [randint(4,8),randint(-8,8)]
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8,8)
You are simply not flipping the screen in the main loop, in the main file.
All is actually being updated, but nothing is being rendered on the screen.
Just intend the code from
# --- Drawing Code ---
to
clock.tick(60)
I think you forgot to indent it.
Just be careful not to include pygame.quit() in the main loop.
Just add a tabulation layer from
...
# --- Drawing Code ---
...
to end in main.py
Related
I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)
You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
I'm trying to program a traffic simulation and need the pygame collision detection. However as soon as i run the program (it isn't finished yet) it tells me i have an attribute error on car_up because it hasn't got the attribute rect. Can someone tell me why it is so and what i can do to improve it? And yes i have been trying figuring it out on my own but with no success.
import pygame
import random
import threading
from pygame.locals import (QUIT, KEYDOWN, K_ESCAPE)
# Define constants for the screen width and height
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 400
grey=[90 , 90 , 90]
green =[125, 255, 50]
spawnrate_up=1500
spawnrate_down=800
dimentions=[75,35]
#CLASSES
#create car
class Car_up (pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100, -20)
self.rect.y = 150
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
merge=random.randint(750,1000)
if self.rect.left > SCREEN_WIDTH:
self.kill()
if self.rect.colliderect(car_down.rect):
car_up.speed.x=car_up.speed.y=0
elif self.rect.centery<=266 and self.rect.centerx>merge:
self.speedx=10
self.speedy=3
else:
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
class Car_down(pygame.sprite.Sprite):
def __init__(self, green, dimentions):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface(dimentions)
self.image.fill(green)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = random.randint(-100,-20)
self.rect.y = 250
self.speedx= 10
self.speedy= 0
# move car and delete once out of screen
def update(self):
self.speedx=10
self.speedy=0
self.rect.move_ip(self.speedx, self.speedy)
if self.rect.left > SCREEN_WIDTH:
self.kill()
pygame.init()
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock=pygame.time.Clock()
#create custom event
ADDCAR_down= pygame.USEREVENT + 1
ADDCAR_up= pygame.USEREVENT + 2
pygame.time.set_timer(ADDCAR_up, spawnrate_up )
pygame.time.set_timer(ADDCAR_down, spawnrate_down )
#create car
car_up= pygame.sprite.Group()
car_down= pygame.sprite.Group()
car_down.add(Car_down(green,dimentions))
car_up.add(Car_up(green,dimentions))
#infinite loop
run = True
#MAIN
while run:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
run = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
run = False
elif event.type == ADDCAR_down:
# Create the new CAR and add it to sprite groups
new_car_down = Car_down(green,dimentions)
car_down.add(new_car_down)
elif event.type == ADDCAR_up:
new_car_up = Car_up(green,dimentions)
car_up.add(new_car_up)
#grey screen
screen.fill(grey)
# Draw the car on the screen
car_up.draw(screen)
car_up.update()
car_down.draw(screen)
car_down.update()
pygame.display.flip()
clock.tick(25)
car_down is a pygame.sprite.Group so obviously it doesn't have a rect attribute. However the pygams.sprites.Sprite object in the Group have a rect attribute. You need to use pygame.sprite.spritecollide to detect collision of a Sprite and the objects in a Group:
if self.rect.colliderect(car_down.rect):
if pygame.sprite.spritecollide(self, car_down, False):
See also How do I detect collision in pygame?
I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)
You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
How to rotate an image(player) to the mouse direction?
(2 answers)
How to turn the sprite in pygame while moving with the keys
(1 answer)
Closed 1 year ago.
I am creating a racecar game and I have decided to rewrite it because I realised I was initially writing it in a very inefficient way. I initially used image an just changed their location around the screen but I am now trying to learn how to create an object and I am running into a problem which I had initially but I don't know how to fix within a class.
What happens is when I turn the image it distorts a lot and ends up slowing down because I think pygame can't handle it maybe. In my old code, I created a copy of the original image and rotated the copy of the image which fixed the distortion problem. I now don't know how to do the same within a class.
main.py
import pygame, random
#Let's import the Car Class
from Car import Car
pygame.init()
SCREENWIDTH=800
SCREENHEIGHT=600
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerCar = Car(60, 80, 70)
playerCar.rect.x = 160
playerCar.rect.y = 100
# Add the car to the list of objects
all_sprites_list.add(playerCar)
#Allowing the user to close the window...
carryOn = True
clock=pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x:
playerCar.moveRight(10)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
#playerCar.moveLeft(5)
playerCar.rot_center(2)
if keys[pygame.K_RIGHT]:
#playerCar.moveRight(5)
playerCar.rot_center(-2)
if keys[pygame.K_UP]:
playerCar.moveUp(5)
if keys[pygame.K_DOWN]:
playerCar.moveDown(5)
all_sprites_list.update()
#Drawing on Screen
screen.fill("white")
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(25)
pygame.quit()
Car.py
import pygame
WHITE = (255, 255, 255)
class Car(pygame.sprite.Sprite):
#This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, width, height, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Instead we could load a proper picture of a car...
self.image = pygame.image.load("car.png").convert_alpha()
self.image = pygame.transform.rotate(self.image, 90)
#Initialise attributes of the car.
self.width=width
self.height=height
rect_surf = pygame.Surface((width, height), pygame.SRCALPHA)
rect_surf.fill((0, 0, 0, 0))
self.rect_surf = rect_surf
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, (0, 0, 0, 0), [0, 0, self.width, self.height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, pixels):
self.rect.y -= pixels
def moveDown(self, pixels):
self.rect.y += pixels
def changeSpeed(self, speed):
self.speed = speed
def rot_center(self, angle):
rotated_image = pygame.transform.rotate(self.image, angle)
new_rect = rotated_image.get_rect(center = self.image.get_rect(center = (self.rect.x + (self.rect.width / 2), self.rect.y + (self.rect.height / 2))).center)
self.image = rotated_image
self.rect = new_rect
Any help on how to slvoe this would be much appreciated. I had questions about how to initially turn the image on another question, How do I make an object in pygame rotate.
I also don't know how to apply the rect correctly so that it is the size of the image and doesn't create a transparent spot on the image.
Any help would be much appreciated thanks. :):)
You should keep original image in separated variable with angle
self.original_image = pygame.image.load("car.png").convert_alpha()
self.angle = 90
and use it to generate rotated image at start
self.image = pygame.transform.rotate(self.original_image, self.angle)
and later you should increase self.angle and again create rotated image using original image
def rot_center(self, angle):
self.angle += angle
rotated_image = pygame.transform.rotate(self.original_image, self.angle)
new_rect = rotated_image.get_rect(center=self.rect.center)
self.image = rotated_image
self.rect = new_rect
or even shorter
def rot_center(self, angle):
self.angle += angle
self.image = pygame.transform.rotate(self.original_image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
Full working code
I used red rectangle instead image so everyone can run it and test it.
I used pygame.math.Vector2(pixels, 0).rotate(self.angle) to move up/down using current angle - car direction - instead of moving to top/bottom of the screen.
import pygame
import random
# --- constants ---
WHITE = (255, 255, 255)
RED = (255, 0, 0)
SCREENWIDTH = 800
SCREENHEIGHT = 600
size = (SCREENWIDTH, SCREENHEIGHT)
# --- classes ---
class Car(pygame.sprite.Sprite):
#This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, width, height, speed):
# Call the parent class (Sprite) constructor
super().__init__()
#Initialise attributes of the car.
self.width = width
self.height = height
# Instead we could load a proper picture of a car...
#self.original_image = pygame.image.load("car.png").convert_alpha()
self.original_image = pygame.surface.Surface((width, height)).convert_alpha()
self.original_image.fill(RED)
self.angle = 90
self.image = pygame.transform.rotate(self.original_image, self.angle)
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, pixels):
#self.rect.y += pixels
x, y = pygame.math.Vector2(pixels, 0).rotate(self.angle)
self.rect.x += x
self.rect.y -= y
def moveDown(self, pixels):
#self.rect.y += pixels
x, y = pygame.math.Vector2(pixels, 0).rotate(self.angle)
self.rect.x -= x
self.rect.y += y
def changeSpeed(self, speed):
self.speed = speed
def rot_center(self, angle):
self.angle += angle
self.image = pygame.transform.rotate(self.original_image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
# --- main ---
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerCar = Car(60, 80, 70)
playerCar.rect.x = 160
playerCar.rect.y = 100
# Add the car to the list of objects
all_sprites_list.add(playerCar)
#Allowing the user to close the window...
carryOn = True
clock = pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn=False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
playerCar.moveRight(10)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
#playerCar.moveLeft(5)
playerCar.rot_center(2)
if keys[pygame.K_RIGHT]:
#playerCar.moveRight(5)
playerCar.rot_center(-2)
if keys[pygame.K_UP]:
playerCar.moveUp(5)
if keys[pygame.K_DOWN]:
playerCar.moveDown(5)
all_sprites_list.update()
#Drawing on Screen
screen.fill("white")
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(25)
pygame.quit()
I'm working on my school project for which im designing a 2D game.
I have 3 images, one is the player and the other 2 are instances (coffee and computer). What i want to do is, when the player image collides with one of the 2 instances i want the program to print something.
I'm unsure if image collision is possible. But i know rect collision is possible. However, after several failed attempts, i can't manage to make my images rects. Somebody please help me. Here is my source code:
import pygame
import os
black=(0,0,0)
white=(255,255,255)
blue=(0,0,255)
class Player(object):
def __init__(self):
self.image = pygame.image.load("player1.png")
self.image2 = pygame.transform.flip(self.image, True, False)
self.coffee=pygame.image.load("coffee.png")
self.computer=pygame.image.load("computer.png")
self.flipped = False
self.x = 0
self.y = 0
def handle_keys(self):
""" Movement keys """
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_DOWN]:
self.y += dist
elif key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_RIGHT]:
self.x += dist
self.flipped = False
elif key[pygame.K_LEFT]:
self.x -= dist
self.flipped = True
def draw(self, surface):
if self.flipped:
image = self.image2
else:
im = self.image
for x in range(0, 810, 10):
pygame.draw.rect(screen, black, [x, 0, 10, 10])
pygame.draw.rect(screen, black, [x, 610, 10, 10])
for x in range(0, 610, 10):
pygame.draw.rect(screen, black, [0, x, 10, 10])
pygame.draw.rect(screen, black, [810, x, 10, 10])
surface.blit(self.coffee, (725,500))
surface.blit(self.computer,(15,500))
surface.blit(im, (self.x, self.y))
pygame.init()
screen = pygame.display.set_mode((800, 600))#creates the screen
player = Player()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # quit the screen
running = False
player.handle_keys() # movement keys
screen.fill((255,255,255)) # fill the screen with white
player.draw(screen) # draw the player to the screen
pygame.display.update() # update the screen
clock.tick(60) # Limits Frames Per Second to 60 or less
Use pygame.Rect() to keep image size and position.
Image (or rather pygame.Surface()) has function get_rect() which returns pygame.Rect() with image size (and position).
self.rect = self.image.get_rect()
Now you can set start position ie. (0, 0)
self.rect.x = 0
self.rect.y = 0
# or
self.rect.topleft = (0, 0)
# or
self.rect = self.image.get_rect(x=0, y=0)
(Rect use left top corner as (x,y)).
Use it to change position
self.rect.x += dist
and to draw image
surface.blit(self.image, self.rect)
and then you can test collision
if self.rect.colliderect(self.rect_coffe):
BTW: and now class Player looks almost like pygame.sprite.Sprite :)