Follow-up question on my previous question.
After I got the circle to show up properly, I wish to be able to move that circle now with keyboard input.
Since it is a class I am trying to change I must have an instance so that I change the same instance of the class all the time, and I do have one.
import pygame
pygame.init()
#Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#Display
display_width, display_height = 800, 600
screen_center = int(display_width * 0.5), int(display_height * 0.5)
screen = pygame.display.set_mode((display_width, display_height))
#Clock
clock = pygame.time.Clock()
class Player():
def __init__(self):
self.xPos = int(display_width * 0.5)
self.yPos = int(display_height * 0.5)
self.color = blue
self.radius = 15
self.width = 0
pygame.draw.circle(screen, self.color, (self.xPos, self.yPos), self.radius, self.width)
def game_loop():
player = Player() #PLAYER CLASS INSTANCE
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.color = blue # THESE COLORS ARE MAINLY HERE FOR DEBUGGING
if event.key == pygame.K_RIGHT:
player.color = red # THESE COLORS ARE MAINLY HERE FOR DEBUGGING
if event.key == pygame.K_UP:
pass
if event.key == pygame.K_DOWN:
pass
#Background
screen.fill(white)
#Draw everything in order, each drawn object will be drawn beneath the next drawn object.
player
#Update
pygame.display.update()
clock.tick(60)
game_loop()
So whenever I press the left key for example, the xPos inside the class must change so that my circle moves.
You could try changing your player class to make it have a separat emethod for drawing itself:
class Player():
def __init__(self):
self.xPos = int(display_width * 0.5)
self.yPos = int(display_height * 0.5)
self.color = blue
self.radius = 15
self.width = 0
def draw(self):
pygame.draw.circle(screen, self.color, (self.xPos, self.yPos), self.radius, self.width)
Then inside your main loop, call player.draw() after you've done the screen fill:
#Background
screen.fill(white)
#Draw everything in order, each drawn object will be drawn beneath the next drawn object.
player.draw()
Now the player object wont draw itself when it's created, instead it will be drawn during every iteration of your main loop.
Related
IDK why my player.move() is not working here's my main class:
import pygame
from player import *
pygame.init()
WIDTH, HEIGHT = 900, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("MyGame!")
FPS = 60
player_x = 500
player_y = 500
PLAYER_WIDTH = 60
PLAYER_HEIGHT = 60
PLAYER_VEL = 5
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
keys_pressed = pygame.key.get_pressed()
player_rect = pygame.Rect(player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT)
player = Player(player_rect, BLACK, WIN, keys_pressed, PLAYER_VEL)
def draw_window():
WIN.fill(WHITE)
pygame.display.update()
def main():
run = True
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
player.move()
player.draw_player()
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
and here's my other class called player.py that the move function is not working:
import pygame
class Player(object):
def __init__(self, player, color, surface, keys_pressed, vel):
self.player = player
self.color = color
self.surface = surface
self.keys_pressed = keys_pressed
self.vel = vel
def draw_player(self):
pygame.draw.rect(self.surface, self.color, self.player)
pygame.display.update()
def move(self):
if self.keys_pressed[pygame.K_a]:
self.player.x -= self.vel
if self.keys_pressed[pygame.K_d]:
self.player.x += self.vel
I tried putting the player = Player(...) before the main function.
But whatever I tried, it doesn't seem to work and this is my first time posting a question on stackoverflow. This problem also happened alot in the past so thanks if you helped me out.
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is 1, otherwise 0. The contents of the keys_pressed list don't magically change when the state of the keys changes. keys_pressed is not tied to the keys. You need to get the new state of the keys in every frame:
class Player(object):
# [...]
def move(self):
# get current state of the keys
self.keys_pressed = pygame.key.get_pressed()
if self.keys_pressed[pygame.K_a]:
self.player.x -= self.vel
if self.keys_pressed[pygame.K_d]:
self.player.x += self.vel
I am trying to make a replica for pong in pygame for my first project but when I try to move my paddles they stretch instead. I believe the reason is it creates a new rect every time I try to move it but I can't seem to figure out why. Please review the code and help rectify my mistake.
Here is my code:
import pygame
W, H = 600, 500
screen = pygame.display.set_mode((W, H))
FPS = 30
class Paddle(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super(Paddle, self).__init__()
self.x = x
self.y = y
self.width = width
self.height = height
self.surf = pygame.Surface((width, height))
self.surf.fill((255, 255, 255))
self.rect = self.surf.get_rect()
self.rect.center = (x, y)
def move(self, distance):
self.rect.move_ip(0, distance)
paddleA = Paddle(15, 250, 10, 50)
paddleB = Paddle(585, 250, 10, 50)
allSprites = pygame.sprite.Group()
allSprites.add(paddleA)
allSprites.add(paddleB)
run = True
clock = pygame.time.Clock()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
pressedKeys = pygame.key.get_pressed()
if pressedKeys[pygame.K_UP]:
paddleB.move(-5)
elif pressedKeys[pygame.K_DOWN]:
paddleB.move(5)
elif pressedKeys[pygame.K_w]:
paddleA.move(-5)
elif pressedKeys[pygame.K_s]:
paddleA.move(5)
for sprite in allSprites:
screen.blit(sprite.surf, sprite.rect)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
Before drawing the new rect you should fill the screen with the background color, to remove the old rect. Otherwise the old one is still drawn there and you are just drawing new over the old one. Its' like painting a new picture on an old one.
screen.fill(color, rect) should do the trick.
I am teaching a fortnightly coding class to a group of dozen super bright young enthusiasts. We have already covered OOP and created a text based adventure using OOP.
Now I am planning to teach PyGame and continue using objects, and I am wondering if games could be built in such a way where the code for each object is in a separate file?, this would be really neat and easier to build on.
Well For the code below I tried making separate files for each object. This was only partially successful because the draw method never works quite well, I believe the issue that I cannot have separate files referencing the same pygame screen.
import pygame
import random
import time
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0,0,255)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
pygame.init()
class Paddle:
'''Class to keep players location'''
def __init__(self,x=350, y=480, width =70,height=20):
self.x = x
self.y = y
self.change_x = 0
self.change_y = 0
self.width = width
self.height = height
self.score = 0
def move(self):
self.x += self.change_x
self.y += self.change_y
def draw(self):
pygame.draw.rect(screen, BLUE, [self.x,self.y, self.width, self.height])
def check_collision(self,ball):
if ball.y>460:
if abs(35+ self.x - ball.x) < 30:
self.score += 1
ball.draw(BLUE)
ball.y = 0
ball.x = random.randint(0,650)
ball.change_y = random.randint(2,3+int(self.score/5))
class Ball:
"""Class to keep track of a ball's location and vector."""
def __init__(self,x=350,y=250,size=25):
self.x = x
self.y = y
self.change_x = 0
self.change_y = 0
self.size = size
def move(self):
self.x += self.change_x
self.y += self.change_y
def draw(self,colour = WHITE):
pygame.draw.circle(screen,WHITE, [self.x, self.y], self.size)
# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing Balls")
done = False
clock = pygame.time.Clock()
screen.fill(BLACK)
ball = Ball()
player = Paddle()
ball.change_y = 2
ball.draw()
while not done:
screen.fill(BLACK)
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pass
if event.key == pygame.K_LEFT:
player.change_x = -5
if event.key == pygame.K_RIGHT:
player.change_x = 5
else:
ball.change_x = 0
player.change_x = 0
if ball.y > 500:
print('YOUR SCORE: ',player.score)
time.sleep(2)
pygame.quit()
#move ball and player and check if they collide
ball.move()
player.move()
player.check_collision(ball)
#draw ball and player
ball.draw()
player.draw()
#render frame
clock.tick(60)
pygame.display.flip()
# Print score and exit
print('YOUR SCORE: ',player.score)
pygame.quit()
When I had separate files this is the error that I got in relation to screen
line 20, in draw
pygame.draw.circle(screen,WHITE, [self.x, self.y], self.size)
NameError: name 'screen' is not defined
Add a surface argument to the draw() methods of the classes Paddle and Ball and draw the object on the surface which is passed to the method:
class Paddle:
# [...]
def draw(self, surface):
pygame.draw.rect(surface, BLUE, [self.x,self.y, self.width, self.height])
class Ball:
# [...]
def draw(self, surface, colour = WHITE):
pygame.draw.circle(surface, colour, [self.x, self.y], self.size)
Now you can draw the objects on any pygame.Surface you want, e.g. screen:
ball.draw(screen)
player.draw(screen)
I currently have a racecar game in development, using Pygame. I understand that in order to get the sprite to move the way a real car does, trigonometry is required. However, for now, I am simply trying to get the racecar image to rotate as the user holds a button. What is the simplest way to do so?
import pygame
pygame.init()
#DEFING COLOURS
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
BLUE = (0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
size = (800,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My First Game")
class Car(pygame.sprite.Sprite):
#BASE CAR CLASS
def __init__(self, filename):
#INITIALISE OBJECT PROPERTIES
super().__init__()
#LOAD IMAGE
self.image = pygame.image.load(filename).convert_alpha()
#SET BACKGROUND COLOUR
#self.image.set_colorkey(WHITE)
#SET RECTANGLE COLLISION BOX
self.rect = self.image.get_rect()
self.angle = 0
self.angle_change = 0
#Create sprites list
all_sprites_list = pygame.sprite.Group()
#Create F1car object
F1car = Car("car.png")
car_rotation = 0.0
surface = pygame.Surface((15, 15))
#Add F1car to sprites list
all_sprites_list.add(F1car)
#LOOP UNTIL USER EXITS THE GAME
carryOn = True
#CLOCK TO CONTROL FRAME RATE
clock = pygame.time.Clock()
##MAIN 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 #END THE LOOP
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
car_rotation += 0.1
pygame.transform.rotate(surface, car_rotation)
#GAME LOGIC
#DRAWING CODE
#CLEARING SCREEN
screen.fill(WHITE)
#DRAWING SHAPES
pygame.draw.rect(screen, RED, [55, 200, 100, 70], 0)
pygame.draw.rect(screen, BLUE, [78, 300, 60, 70], 0)
#LIST OF SPRITES TO COLLIDE WITH EACHOTHER
#blocks_hit_list = pygame.sprite.spritecollide(F1car, )
#DRAW SPRITES FROM all_sprites_list LIST
all_sprites_list.draw(screen)
#UPDATE THE SCREEN
pygame.display.flip()
#SET UPDATE RATE
clock.tick(60)
pygame.quit()
I'd give the Car class an update method and in this method rotate the car if self.angle_change is not 0. That allows you to call all_sprites.update() to call the update methods of all contained sprites.
Set the angle_change in the event loop to start the rotation. In the update method, increase the self.angle by the self.angle_change, then use pygame.transform.rotate or .rotozoom and pass the self.angle. Afterwards you need to get a new rect and pass the center of the old rect as the center argument.
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
WHITE = (255, 255, 255)
CAR_IMAGE = pygame.Surface((45, 90), pygame.SRCALPHA)
CAR_IMAGE.fill((150, 20, 0))
class Car(pygame.sprite.Sprite):
def __init__(self, pos, image):
super().__init__()
self.image = image
# Store a reference to the original to preserve the image quality.
self.orig_image = self.image
self.rect = self.image.get_rect(center=pos)
self.angle = 0
self.angle_change = 0
def update(self):
if self.angle_change != 0:
self.angle += self.angle_change
# I prefer rotozoom because it looks smoother.
self.image = pygame.transform.rotozoom(self.orig_image, self.angle, 1)
self.rect = self.image.get_rect(center=self.rect.center)
all_sprites = pygame.sprite.Group()
f1_car = Car((300, 300), CAR_IMAGE)
all_sprites.add(f1_car)
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
elif event.type == pygame.KEYDOWN:
# Set the rotation speed of the car sprite.
if event.key == pygame.K_RIGHT:
f1_car.angle_change = -3
elif event.key == pygame.K_LEFT:
f1_car.angle_change = 3
elif event.type == pygame.KEYUP:
# Stop rotating if the player releases the keys.
if event.key == pygame.K_RIGHT and f1_car.angle_change < 0:
f1_car.angle_change = 0
elif event.key == pygame.K_LEFT and f1_car.angle_change > 0:
f1_car.angle_change = 0
all_sprites.update()
screen.fill(WHITE)
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
This can be done rather simply. You need a game loop that checks for inputs. Then you must check that the desired input is present, and increase the rotation of your car each time the input is present.
import pygame
run = True
car_rotation = 0.0
surface = pygame.Surface((100, 60)) # 100 horizontal length. 60 is the vertical length.
while run:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
car_rotation += 0.1
surface = pygame.transform.rotate(surface, car_rotation)
For the case of your code
You have done some wrong checks in the loop. Change your code from pygame.K_RIGHT to pygame.K_r, to use your R key to rotate your sprite. In order to use the mouse, change the pygame.event.type to .MOUSEBUTTONDOWN or .MOUSEBUTTONUP, and keep pygame.K_RIGHT.
Change the if statement, if event.key == pygame.K_r, to
if event.key == pygame.K_r
car_rotation += 1.0
for car in all_sprites_list:
car.image = pygame.transform.rotate(car.image, car_rotation)
and then remove surface = pygame.Surface((15, 15)).
some of you may have seen my previous questions regarding a Pygame project I'm currently working on, but I decided to do a rewrite and follow a proper object oriented programming since it wasn't really working out.
This is what I have so far:
###### Import & Init ######
import pygame
import os, random, math, copy, sys
pygame.init()
###### Variables ######
displayWidth, displayHeight = 600, 600
shipWidth, shipHeight = 50, 50
# Variables that will be used to centre the ship.
startX = displayWidth / 2
startY = displayHeight - 40
screen = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Space Arcader')
####### Colours #######
# Colour list containing most common colours.
# Colour R G B
red = (255, 0, 0)
green = ( 0, 255, 0)
blue = ( 0, 0, 255)
grey = (100, 100, 100)
black = ( 0, 0, 0)
white = (255, 255, 255)
# Create a list from the colours in order to call it later.
colourList = [red, green, blue, black, white]
####### Classes #######
# Ship class
class Ship(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("assets/ship.png").convert_alpha()
self.image = pygame.transform.scale(self.image,(shipWidth, shipHeight))
self.transform = self.image
self.rect = self.image.get_rect()
self.rect.centerx = startX
self.rect.centery = startY
# Where the arrow will be pointing on game start
self.angle = 90
def update(self, direction):
if direction == 'right' and self.angle > 20:
self.angle -= 4
elif direction == 'left' and self.angle < 160:
self.angle += 4
self.transform = pygame.transform.rotate(self.image, self.angle)
self.rect = self.transform.get_rect()
self.rect.centerx = startX
self.rect.centery = startY
def draw(self):
screen.blit(self.transform, self.rect)
# Score class
class Score(object):
def __init__(self):
self.total = 0
self.font = pygame.font.SysFont('Helvetica', 15)
self.render = self.font.render('Score: ' + str(self.total), True, white)
self.rect = self.render.get_rect()
self.rect.left = 5
self.rect.bottom = displayHeight - 2
self.render.set_colorkey((0,0,0))
def update(self, delete_scoreList):
self.total += ((len(delete_scoreList)) * 50)
self.render = self.font.render('Score: ' + str(self.total), True, white)
def draw(self):
screen.blit(self.render, self.rect)
# Game class
class MainGame(object):
def __init__(self):
self.score = 0
self.game_over = False
def controls(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
Menu.terminate()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
direction = 'left'
elif event.key == pygame.K_RIGHT:
direction = 'right'
elif event.type == pygame.KEYUP:
direction = None
if event.key == pygame.K_SPACE:
bullet = Bullet()
bullet.rect.x = arrow.rect.x
bullet.rect.y = arrow.rect.y
all_sprites_list.add(bullet)
bulletList.add(bullet)
elif event.key == pygame.K_ESCAPE:
running = False
MenuInit()
def displayInit(self, screen):
# Set screen width and height.
display = pygame.display.set_mode((displayWidth, displayHeight))
# Set the background image of the window.
background = pygame.image.load("assets/background.jpg")
# Blit the background onto the screen.
screen.blit(background, (0, 0))
# Disable mouse visibility.
pygame.mouse.set_visible(False)
# Code to redraw changing/moving objects.
pygame.display.flip()
# Menu class
class Menu:
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)
def set_rend(self):
menu_font = pygame.font.SysFont('Helvetica', 40)
self.rend = menu_font.render(self.text, True, self.get_color())
def get_color(self):
if self.hovered:
return (white)
else:
return (grey)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
def terminate():
pygame.quit()
sys.exit()
####### Functions #######
def MenuInit():
# Set the background image of the window.
background = pygame.image.load("assets/menuBackground.jpg")
options = [Menu("Start game", (200, 250)), Menu("Quit", (265, 300))]
# Enable mouse visibility.
pygame.mouse.set_visible(True)
while True:
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
else:
option.hovered = False
option.draw()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
for option in options:
if option.hovered and option.text == "Start game":
MainInit()
elif option.hovered and option.text == "Quit":
Menu.terminate()
pygame.display.update()
screen.blit(background,(0,0))
def MainInit():
# Manage the refresh rate
clock = pygame.time.Clock()
# Loop the game until the user closes the application.
running = True
# Open an instance of the Game class
game = MainGame()
ship = Ship()
score = Score()
# Main Loop.
while running:
draw = ship.draw()
ship.update(direction)
# ship.update(direction)
# ship.draw()
controls = game.controls()
game.displayInit(screen)
# Refresh rate speed (frames per second).
clock.tick(60)
# Open the menuInit() function which brings up the main menu.
if __name__ == '__main__':
MenuInit()
So my problem is trying to blit the ship and score onto the MainInit() function which calls the game class object as you can see above. Calling the game class object works fine because the background image changes and the controls work perfectly. However, when I follow the same method for ship and score, it doesn't seem to work. In the commented out comments, you can see I tried a few things but I got various errors such as "NameError: global name 'direction' is not defined" or NameError: global name 'update' is not defined
Any pointers? :)
Thank you very much.
The problem is caused by an out-of-scope variable - exactly as the error is telling you: global name 'direction' is not defined".
You use direction in your def MainInit(), but direction is never defined in that function. The place you define/set a direction-variable, is in class MainGame.controls().
The problem is, however, that the direction-variable created in class MainGame.controls() is local only. It will only exist within that specific function, MainGame.controls(). When that function is not used any longer, the value of direction will cease to exist - which is why there is no such thing as direction defined in def MainInit(). It's out of scope.
To fix this problem, you can choose to use direction as a global variable. It requires you to define the direction value outside any functions, so at the very beginning should work.
Whenever you want to read/modify that specific global variable, you should use the global keyword, to tell your Python function that you want to use/modify a global variable, and not a local one
global direction
This might be of interest to you: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them
Personally I would not use global variables, but rather store a direction member variable in the Ship-class and directly change that.
Global variables can become quite a mess.