I am making a game in Pygame. Now I have only one rectangle is moving on screen. The direction of rectangle can be changed from keyboard.
main.py :
import pygame
import sys
import player
import room
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 255, 0)
green = (0, 0, 255)
# Global variables
S_WIDTH = 800
S_HEIGHT = 800
FPS = 60
# player speed
SPEED = 2
# Initialization
pygame.init()
screen = pygame.display.set_mode([S_WIDTH, S_HEIGHT])
pygame.display.set_caption("Py-Man Alpha")
clock = pygame.time.Clock()
# Game class
class Game(object):
"""
This is main class of game.
All game events will happen here.
"""
Player = player.Player(blue, 50, 50, 0, 0)
def __init__(self, screen, running):
self.screen = screen
self.running = running
self.run()
def run(self):
while self.running:
# Events
for event in pygame.event.get():
# If user hits 'x'
if event.type == pygame.QUIT:
self.running = False
# Keyborad events
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.Player.direction = 'up'
self.Player.move(SPEED, WALLS)
elif event.key == pygame.K_DOWN:
self.Player.direction = 'down'
self.Player.move(SPEED, WALLS)
elif event.key == pygame.K_LEFT:
self.Player.direction = 'left'
self.Player.move(SPEED, WALLS)
elif event.key == pygame.K_RIGHT:
self.Player.direction = 'right'
self.Player.move(SPEED, WALLS)
elif event.key == pygame.K_p:
self.Player.direction = 'pause'
# Clear screen
self.screen.fill(white)
#Draw
self.Player.move(SPEED, WALLS)
self.Player.draw(self.screen)
for wall in WALLS:
wall.draw()
# Set clock
clock.tick(60)
# Update screen
pygame.display.flip()
# End of game
pygame.quit()
sys.exit()
# Tests
game = Game(screen, True)
player.py:
#imports
import pygame
class Player(pygame.sprite.Sprite):
"""
This class represent the player image
and has the player actions.
"""
direction = 'right'
def __init__(self, color, width, height, x, y):
# Pygame constructor
pygame.sprite.Sprite.__init__(self)
# Init. variables
self.color = color
self.width = width
self.height = height
# Create sprite
self.image = pygame.Surface([width, height])
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, screen):
# Draw player on the screen
pygame.draw.rect(screen, self.color, [self.rect.x, self.rect.y, self.width, self.height], 0)
def move(self, speed, walls):
# Move the player
if self.direction == 'up':
self.rect.y -= speed
elif self.direction == 'down':
self.rect.y += speed
elif self.direction == 'left':
self.rect.x -= speed
elif self.direction == 'right':
self.rect.x += speed
so, how I can speed up my game? How I can improve speed? Where I wrong?
Thanks!
EDIT:
The refresh of monitor is slow. I press 'up' key , but monitor take command after 2-3 seconds. Why?
You can increase SPEED or clock.tick(60)
But I prefer to increase SPEED.
clock.tick(60) means 60 FPS and you could increase it but many monitors refresh its screen with 60Hz and it means 60 FPS. Bigger FPS means more work for CPU so it is hotter and you can hear CPU fan :)
BTW: Human eye needs at least 24 FPS to see animation.
Related
My problem is very simple. The bullets I fire sticks to the screen if I shoot fast. If I shoot slowly, they don't stick. Anyone have an idea how this phenomenon occurs?
screenshot of the bullets sticking to the screen
Below I have entered the code. I follow this default game flowchart:
I am curious about the origin of the problem. Is it the code or hardware?
import sys
import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group
# pygame initializing
pygame.init()
#create the screen surface
screen = pygame.display.set_mode((800, 700))
class Color():
def __init__(self):
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.red = (255, 0, 0)
self.green = (0, 255, 0)
self.green_lambda = (10, 255, 150)
self.blue = (0, 0, 255)
# set up the colors
color = Color() # make an instance of this class - this makes some colors available
class Spaceship(Sprite):
"""
This class represents the Spaceship.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self):
""" Constructor"""
# Call the parent class (Sprite) constructor
super().__init__()
width = 22
height = 32
self.screen = screen
self.image = pygame.Surface((width, height))
self.image.fill(color.black)
self.image.set_colorkey(color.black)
pygame.draw.polygon(self.image, color.green_lambda, [[10,0],[15,22],[20,30],[10,27],[0,30],[5,22]],2)
self.rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# As the rect method only take integers we store a
# This value is only used at the beginning, i.e. before the game loop starts
self.center_x = self.rect.centerx
self.center_y = self.rect.centery
class Bullet(Sprite):
"""
This class represents the bullets.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface((8,10))
self.image.fill(color.red)
self.image.set_colorkey((color.red))
pygame.draw.ellipse(self.image, color.green, [1, 0, 5, 8], 2)
self.rect = self.image.get_rect()
self.rect.centerx = defender.rect.centerx
self.rect.bottom = defender.rect.top
# def function to move the bullets
def update_pos(self):
self.rect.y -= bullet_speed
# create spaceship instance
defender = Spaceship()
# create group to store sprites in
all_sprites_list = Group()
all_sprites_list.add(defender)
ship_speed = 0.5
bullet_speed = 3
def run_game():
m_right = False
m_left = False
m_up = False
m_down = False
new_bullet = False
while True:
"""This is the user interaction section"""
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_RIGHT:
m_right = True
elif event.key == pygame.K_LEFT:
m_left = True
elif event.key == pygame.K_UP:
m_up = True
elif event.key == pygame.K_DOWN:
m_down = True
elif event.key == pygame.K_SPACE:
new_bullet = Bullet()
#print(dir(new_bullet))
all_sprites_list.add(new_bullet)
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
m_right = False
elif event.key == pygame.K_LEFT:
m_left = False
elif event.key == pygame.K_UP:
m_up = False
elif event.key == pygame.K_DOWN:
m_down = False
"""Below is the game logic, which gets input from the user interaction
section and more"""
# Movement of spaceship depending on the flag boolean value and on screen width and height
if m_right and defender.rect.right < defender.screen_rect.right:
defender.center_x += ship_speed
if m_left and defender.rect.left > defender.screen_rect.left:
defender.center_x -= ship_speed
if m_up and defender.rect.top > defender.screen_rect.top:
defender.center_y -= ship_speed
if m_down and defender.rect.bottom < defender.screen_rect.bottom:
defender.center_y += ship_speed
# The cumulative value (which is a float number) for the spaceships movement
# is given to the spaceship rect variable (which can only be integer) now.
# This enables fine adjusting of the speed
defender.rect.centerx = defender.center_x
defender.rect.centery = defender.center_y
all_sprites_list.update()
screen.fill(color.black)
if new_bullet:
new_bullet.update_pos()
# Below the bullets which leaves the screen display are deleted
if new_bullet.rect.bottom < defender.screen_rect.top:
all_sprites_list.remove(new_bullet)
all_sprites_list.draw(screen)
print(all_sprites_list)
pygame.display.flip()
run_game()
instead of just updating the position of new_bullet
# if new_bullet:
# new_bullet.update_pos()
# # Below the bullets which leaves the screen display are deleted
# if new_bullet.rect.bottom < defender.screen_rect.top:
# all_sprites_list.remove(new_bullet)
update the position of all bullets
for bullet in all_sprites_list:
if isinstance(bullet,Bullet):
bullet.update_pos()
if bullet.rect.bottom < defender.screen_rect.top:
all_sprites_list.remove(bullet)
del bullet
Joran Beasley's answer is correct. I'd just like to point out that you can also put the behavior of the sprites into their update methods which get called automatically when you call all_sprites_list.update(). You can actually move most of the code in the while loop to the update methods.
I've got an example with these changes and some more tips in the comments (a quick code review):
import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group
# I'd just define some global constants for the colors.
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
GREEN_LAMBDA = (10, 255, 150)
class Spaceship(Sprite):
"""This class represents the Spaceship."""
def __init__(self, screen):
"""Constructor"""
super().__init__()
self.screen = screen
# pygame.SRCALPHA makes the surface transparent.
self.image = pygame.Surface((22, 32), pygame.SRCALPHA)
pygame.draw.polygon(
self.image, GREEN_LAMBDA,
[[10,0],[15,22],[20,30],[10,27],[0,30],[5,22]], 2
)
self.screen_rect = self.screen.get_rect()
# You can pass the position as the midbottom argument to `get_rect`.
self.rect = self.image.get_rect(midbottom=self.screen_rect.midbottom)
self.center_x = self.rect.centerx
self.center_y = self.rect.centery
# I've removed the `m_right`, etc. variables and just set the speed
# of the sprite in the event loop.
self.max_speed = 3.5
self.speed_x = 0
self.speed_y = 0
def update(self):
# Move the sprite.
self.center_x += self.speed_x
self.center_y += self.speed_y
self.rect.centerx = self.center_x
self.rect.centery = self.center_y
# Keep the sprite on the screen.
if not self.screen_rect.contains(self.rect):
self.rect.clamp_ip(self.screen_rect)
self.center_x, self.center_y = self.rect.center
class Bullet(Sprite):
"""This class represents the bullets."""
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((8, 10), pygame.SRCALPHA)
pygame.draw.ellipse(self.image, GREEN, [1, 0, 5, 8], 2)
self.rect = self.image.get_rect(midbottom=pos)
self.speed = 3 # The speed is now an attribute.
def update(self):
self.rect.y -= self.speed
if self.rect.top < 0:
self.kill() # Remove the sprite from all groups.
def run_game():
pygame.init()
screen = pygame.display.set_mode((800, 700))
clock = pygame.time.Clock() # Use a clock to limit the frame rate.
defender = Spaceship(screen)
all_sprites = Group() # Changed the name because groups are not lists.
all_sprites.add(defender)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
elif event.key == pygame.K_RIGHT:
defender.speed_x = defender.max_speed
elif event.key == pygame.K_LEFT:
defender.speed_x = -defender.max_speed
elif event.key == pygame.K_UP:
defender.speed_y = -defender.max_speed
elif event.key == pygame.K_DOWN:
defender.speed_y = defender.max_speed
elif event.key == pygame.K_SPACE:
new_bullet = Bullet(defender.rect.midtop) # Pass the pos.
all_sprites.add(new_bullet)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT and defender.speed_x > 0:
defender.speed_x = 0
elif event.key == pygame.K_LEFT and defender.speed_x < 0:
defender.speed_x = 0
elif event.key == pygame.K_UP and defender.speed_y < 0:
defender.speed_y = 0
elif event.key == pygame.K_DOWN and defender.speed_y > 0:
defender.speed_y = 0
all_sprites.update() # Calls the update methods of all sprites.
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
run_game()
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)).
Currently, I have small image that I move using the D and A keys on the keyboard. The code works just how I want it to, but it seems to be a bit unnecessarily complicated. Is there a more efficient way of doing this?
In the main loop, my code checks to see what key is pressed through the events, but after that, if a key is no longer pressed, it checks to see if another key is pressed, just in case a user pressed a key while another key was initially pressed.
Here is my code:
import pygame
import sys
from pygame.locals import *
SCREENX = 640
SCREENY = 480
LEFT = 'left'
RIGHT = 'right'
class Character(pygame.sprite.Sprite):
def __init__(self, image_file):
super().__init__()
temp_image = pygame.image.load(image_file)
self.image = pygame.transform.scale(temp_image, (100, 100))
self.rect = self.image.get_rect()
self.moving = False
self.direction = RIGHT
def start_move(self, direction):
self.moving = True
if direction != self.direction:
self.image = pygame.transform.flip(self.image, True, False)
self.direction = direction
def stop_move(self):
if self.moving:
self.moving = False
def move(self):
if self.direction == RIGHT:
self.rect.x += 5
if self.direction == LEFT:
self.rect.x -= 5
def update(self):
if self.moving:
self.move()
def main():
pygame.init()
surface = pygame.display.set_mode((SCREENX, SCREENY))
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
girl = Character("assets/girl.png")
girl.rect.x = SCREENX/2
girl.rect.y = SCREENY - girl.rect.height
sprites.add(girl)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if not girl.moving:
if event.key == K_d:
girl.start_move(RIGHT)
if event.key == K_a:
girl.start_move(LEFT)
if event.type == KEYUP:
if event.key == K_a or event.key == K_d:
girl.stop_move()
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_d]:
girl.start_move(RIGHT)
if keys_pressed[K_a]:
girl.start_move(LEFT)
surface.fill((255, 255, 255))
sprites.update()
sprites.draw(surface)
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
main()
Yes, you can get rid of most of the event handling, you can generalize your main loop, and you can get rid of some fields of your Character class.
See my explanatory notes in the comments:
import pygame
import sys
from pygame.locals import *
SCREENX = 640
SCREENY = 480
class Character(pygame.sprite.Sprite):
def __init__(self, image_file):
pygame.sprite.Sprite.__init__(self)
temp_image = pygame.image.load(image_file)
# only to the flipping of the image once
self.image_r = pygame.transform.scale(temp_image, (100, 100))
self.image_l = pygame.transform.flip(self.image_r, True, False)
self.image = self.image_l
self.rect = self.image.get_rect()
def update(self, pressed_keys):
move = 0
if pressed_keys[K_d]: move += 1
if pressed_keys[K_a]: move -= 1
self.rect.move_ip(move*5, 0)
# check which direction we're facing and set the image
self.image = self.image_l if move < 0 else self.image_r
def main():
pygame.init()
surface = pygame.display.set_mode((SCREENX, SCREENY))
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
girl = Character("assets/girl.png")
girl.rect.x = SCREENX / 2
girl.rect.y = SCREENY - girl.rect.height
sprites.add(girl)
while True:
for event in pygame.event.get():
# since we are in a function, we can simply return.
# we don't care here what happens after we quit the main loop
if event.type == QUIT: return pygame.quit()
# get all pressed keys, and just let all sprites
# decide what they want to do with it
keys_pressed = pygame.key.get_pressed()
# you could wrap this information in a more
# general container represeting the game state
# See how the game loop does not care what a
# Sprite does with this information
sprites.update(keys_pressed)
surface.fill((255, 255, 255))
sprites.draw(surface)
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
main()
I am currently working on a pong clone in Python using Pygame. I am no where near an expert at the subject of programming, and have run into an error. Whenever I run the code I currently have, which will be listed below separated by Blockquotes, the program will freeze up, and not respond. I cannot tell what the issue may be with the little knowledge I have. I was hoping that someone would be able to assist me on this issue. I am currently using a tutorial to help instruct me through the making of this game, and the code looks pretty identical at this point. I will include a hyperink to the tutorial here. I appreciate any and all feedback.
3000X_runner.py
import GameObj
if __name__ == '__main__':
game = GameObj.Game()
game.run()
PongPaddle.py
import os, pygame
class Paddle(pygame.sprite.Sprite):
def __init__(self, user, xy):
pygame.sprite.Sprite.__init__(self)
if user == 1:
self.image = pygame.image.load(os.path.join("images", "playerPaddle.png"))
else:
self.image = pygame.image.load(os.path.join("images", "opponentPaddle.png"))
self.rect = self.image.get_rect()
self.rect.centerx, self.rect.centery = xy
self.movement_speed = 5
self.velocity = 0
def up(self):
self.velocity -= self.movement_speed
def down(self):
self.velocity += self.movement_speed
def move(self, dy):
if self.rect.bottom + dy > 720:
self.rect.bottom = 720
elif self.rect.top + dy < 0:
self.rect.bottom = 0
else: self.rect.y += dy
def update(self):
self.move(self.velocity)
GameObj.py
import os, pygame, PongPaddle
class Game(object):
def __init__(self):
pygame.init()
#setup screen width/height
self.screen = pygame.display.set_mode((1080, 720))
self.clock = pygame.time.Clock() #setups internal pygame clock
#Mandatory silly name featuring extra numbers for game name
pygame.display.set_caption("Hyper Pong 3000-X")
#create background.
self.background = pygame.Surface((1080, 720))
self.background.fill((0,0,0)) #black background
#draws a white line down the middle of screen
#line(Surface, color, start_pos, end_pos, width=1) -> Rect
pygame.draw.line(self.background, (255, 255, 255), (540, 0), (540, 720), 5)
self.screen.blit(self.background, (0, 0)) #blits to screen
#I shall render all the sprites here:
self.sprites = pygame.sprite.OrderedUpdates()
#setting up the paddles and adding them to my sprite group
self.player_paddle = PongPaddle.Paddle(1, (50, 200))
self.sprites.add(self.player_paddle)
self.opponent_paddle = PongPaddle.Paddle(2, (1030, 200))
self.sprites.add(self.opponent_paddle)
def run(self):
stillPlaying= True
while stillPlaying:
self.clock.tick(30)
#handles our pygame events, giving us a easy to view, clutter free game loop
stillPlaying = self.handleEvents
#updates all of our sprites
for sprite in self.sprites:
sprite.update()
self.sprites.clear(self.screen, self.background)
dirty = self.sprites.draw(self.screen)
pygame.display.update(dirty)
def handleEvents(self):
#here we handle all of our events, such as when either player will
#press a key to move, quit the game, etc etc.
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
#player1 keydown events
if event.key == pygame.K_w:
self.player_paddle.up()
if event.key == pygame.K_s:
self.player_paddle.down()
#player2 keydown events
if event.key == pygame.K_UP:
self.opponent_paddle.up()
if event.key == pygame.K_DOWN:
self.opponent_paddle.down()
elif event.type == pygame.KEYUP:
#player1 keyup events
if event.key == pygame.K_w:
self.player_paddle.down()
if event.key == pygame.K_s:
self.player_paddle.up()
#player2 keyup events
if event.key == pygame.K_UP:
self.opponent_paddle.down()
if event.key == pygame.K_DOWN:
self.opponent_paddle.up()
return True
Simply forgot to add () in GameObj.py after
stillPlaying = self.handleEvents
It should look like: stillPlaying = self.handleEvents()
I'm making a pygame program to animate a sprite, but the problem isn't a particular error:
import sys
import os
import pygame
from pygame.locals import *
pygame.init ()
WHITE = ( 255, 255, 255 )
BLACK = ( 0, 0, 0 )
Surface = pygame.display.set_mode ((400, 400), 0, 32)
pygame.display.set_caption ('Animation Test')
class Processes (object):
#staticmethod
def load_image (imagefile):
image = pygame.image.load (imagefile) #Remember to call imagefile as a string statement
return image, image.get_rect ()
class Sprite (pygame.sprite.Sprite):
def __init__ (self):
pygame.sprite.Sprite.__init__ (self)
self.image, self.rect = Processes.load_image ('frame1.jpg')
self.blindrect = pygame.Rect ((self.rect.top - 289, self.rect.left - 289), (self.rect.width, self.rect.height))
def animation (self, key_event = None, mouse_event = None):
blindpass = 0
if key_event == K_LEFT:
self.rect.centerx = self.rect.centerx - 10
if key_event == K_RIGHT:
self.rect.centerx = self.rect.centerx + 10
if key_event == K_UP:
self.rect.centery = self.rect.centery - 10
if key_event == K_DOWN:
self.rect.centery = self.rect.centery + 10
if mouse_event == MOUSEBUTTONDOWN:
self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()
if blindpass > 0:
if key_event == K_LEFT:
self.blindrect.centerx = self.blindrect.centerx - 10
if key_event == K_RIGHT:
self.blindrect.centerx = self.blindrect.centerx + 10
if key_event == K_UP:
self.blindrect.centery = self.blindrect.centery - 10
if key_event == K_DOWN:
self.blindrect.centery = self.blindrect.centery + 10
if mouse_event == MOUSEBUTTONDOWN:
self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()
mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))
while True:
Surface.fill (WHITE)
for event in pygame.event.get ():
if event.type == KEYDOWN:
mecha.animation (key_event = event.key)
if event.type == MOUSEBUTTONDOWN:
mecha.animation (mouse_event = event.type)
if event.type == QUIT:
pygame.quit ()
sys.exit (0)
allsprites.draw (Surface)
pygame.display.update ((mecha.rect, mecha.blindrect))
Now, the error is a bit weird. The output is that the sprite does appear on the screen, and it moves when I provide key and mouse input, but the problem is is that it's leaving trails. Which is why I created a rectangle to trail the sprite rectangle which is supposed to be filled with white every time the game loop loops. But it doesn't. At least, not until I minimize the window, and pull it up again, all the trails disappear and the background becomes white instead of black. But when I move the image, the trails start forming again. I minimize, they disappear as supposed to.
If your render function does:
screen.fill(white)
# blit or sprite group drawing
pygame.display.flip()
It will clear the entire screen in white, draw the sprites once. There won't be a trail.
Your code didn't pasted correctly. fLike your if QUIT statement has no indention, for a multi statement. The same with class Sprite
You might get naming problems if you use Surface or Sprite as your class/variable names.
You're not initializing and updating the blindrect correctly. That's why the correct portion of the screen is not being updated and your sprite is leaving a trail.
In your update loop, since you're only updating the portion of the display that changed, you need to update the entire screen once to initialize it before you go into the update loop. That's why the screen is not initially white.
See the updated source below (only the Sprite class and later was modified):
class Sprite (pygame.sprite.Sprite):
def __init__ (self):
pygame.sprite.Sprite.__init__ (self)
self.image, self.rect = Processes.load_image ('frame1.jpg')
#initialize blindrect to be the same rectangle as rect
self.blindrect = pygame.Rect ((self.rect.top, self.rect.left), (self.rect.width, self.rect.height))
def animation (self, key_event = None, mouse_event = None):
print(self.rect)
print(self.blindrect)
#save the current sprite position before moving the sprite
self.blindrect.centerx = self.rect.centerx
self.blindrect.centery = self.rect.centery
if key_event == K_LEFT:
self.rect.centerx = self.rect.centerx - 10
if key_event == K_RIGHT:
self.rect.centerx = self.rect.centerx + 10
if key_event == K_UP:
self.rect.centery = self.rect.centery - 10
if key_event == K_DOWN:
self.rect.centery = self.rect.centery + 10
if mouse_event == MOUSEBUTTONDOWN:
self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()
print(self.rect)
print(self.blindrect)
mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))
#need to initially fill the entire surface with white
Surface.fill (WHITE)
pygame.display.update ((pygame.Rect((0,0),(400,400))))
while True:
Surface.fill (WHITE)
for event in pygame.event.get ():
if event.type == KEYDOWN:
mecha.animation (key_event = event.key)
if event.type == MOUSEBUTTONDOWN:
mecha.animation (mouse_event = event.type)
if event.type == QUIT:
pygame.quit ()
sys.exit (0)
allsprites.draw (Surface)
#update only the previous sprite position and new sprite position
pygame.display.update ((mecha.rect, mecha.blindrect))