Adjust alarm time to match clock time in python - python

I'm just learning Python, and as one of the basic challenges that were suggested to me by friends I've been working on an alarm clock. I successfully made an alarm clock that played a .wav sound at a predetermined time. Now I've been using Pygame for a GUI, and it all worked great, until I had to set the buttons to adjust the alarm time. See when I compare the alarm time to the clock time, the clock time is in string form, so the alarm time has to be as well. But the buttons are unable to + or - from a string, so I'm kind of stuck. I tried ways of turning it into a string, but everything has been fairly unsuccessful so far. Wondering if anyone here had a suggestion.
Here's the code:
#!/usr/bin/python
import os.path, sys, datetime, time
import os, sys, math
import pygame, random
from pygame.locals import *
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, 'data')
currenttime = datetime.datetime.now()
clocktime = currenttime.strftime("%H:%M")
alarmtime = "13:23"
pygame.init()
#Screen and background
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Alarm Clock")
background = pygame.image.load(os.path.join(data_dir, 'diamondplate.jpg'))
background = pygame.transform.scale(background, (width, height))
#Current time
font = pygame.font.Font(None, 250)
text = font.render("%s" % clocktime, True, (255,140,0), (0,0,0))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery - 200
#Alarm time
text2 = font.render("%s" % '00:00', True, (255,140,0), (0,0,0))
text2Rect = text2.get_rect()
text2Rect.centerx = screen.get_rect().centerx
text2Rect.centery = screen.get_rect().centery + 200
#Alarm noise
def alarmsound(file_path=os.path.join(main_dir, 'data', 'boom.wav')):
pygame.mixer.init(11025)
sound = pygame.mixer.Sound(file_path)
channel = sound.play()
pygame.time.wait(1000)
#Image load function
def load_image(file):
file = os.path.join(data_dir, file)
surface = pygame.image.load(file)
return surface.convert_alpha()
#Hour arrow up
class Hourup(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self,self.groups)
image = load_image('arrowup.png')
image = pygame.transform.scale(image, (85,85))
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.area = surface.get_rect()
self.rect.bottomleft = text2Rect.topleft
def click_check(self,eventpos):
if self.rect.collidepoint(eventpos):
pass
def update(self):
pass
#Hour arrow down
class Hourdown(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self,self.groups)
image = load_image('arrowdown.png')
image = pygame.transform.scale(image, (85,85))
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.area = surface.get_rect()
self.rect.bottom = text2Rect.top
self.rect.left = 159
def click_check(self,eventpos):
if self.rect.collidepoint(eventpos):
pass
def update(self):
pass
#Minute arrow up
class Minuteup(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self,self.groups)
image = load_image('arrowup.png')
image = pygame.transform.scale(image, (85,85))
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.area = surface.get_rect()
self.rect.bottomright = (442,414)
def click_check(self,eventpos):
if self.rect.collidepoint(eventpos):
pass
def update(self):
pass
#Minute arrow down
class Minutedown(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self,self.groups)
image = load_image('arrowdown.png')
image = pygame.transform.scale(image, (85,85))
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.area = surface.get_rect()
self.rect.bottomright = text2Rect.topright
def click_check(self,eventpos):
if self.rect.collidepoint(eventpos):
pass
def update(self):
pass
#Groups
allsprites = pygame.sprite.Group()
Hourup.groups = allsprites
Hourdown.groups = allsprites
Minutedown.groups = allsprites
Minuteup.groups = allsprites
hourup = Hourup()
hourdown = Hourdown()
minutedown = Minutedown()
minuteup = Minuteup()
clickableobjects = [hourup, hourdown, minutedown, minuteup]
def main():
while 1:
currenttime = datetime.datetime.now()
clocktime = currenttime.strftime("%H:%M")
screen.blit(background,(0,0))
text = font.render("%s" % clocktime, True, (255,140,0), (0,0,0))
text2 = font.render("%s" % alarmtime, True, (255,140,0), (0,0,0))
screen.blit(text,textRect)
screen.blit(text2,text2Rect)
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
for object in clickableobjects:
object.click_check(event.pos)
if clocktime == alarmtime and soundcheck = False:
alarmsound()
soundcheck = True
allsprites.draw(screen)
allsprites.update()
pygame.display.update()
pygame.display.flip
if __name__ == '__main__':
main()

You are looking for strptime() which will convert a string to a datetime instance.
see here for how to properly use it.
Comparing two datetime instances will give you a timedelta instance which you can read about here. Essentially it will give you the difference between the two times to the nearest milisecond.
Learn everything you can about the datetime, time, and calendar modules. Once you learn those dealing with times and dates in python becomes really easy.

Related

pygame rect doesn't change position while i give it a new position and update it

okay so i'm fairly new to programming and i'm making a game based on the arcade game alien invasion. but i want to make an intro with moving text to show credits at the start because it's fun and good practice to learn. but i cant seem to make it work. i want the text to fly from the side of the screen to middle stop there then fly away again
so here is the function that draws the texts.
from Settings import *
import sys
class TextsObjects:
def __init__(self):
self.setting = Settings()
self.intro_settings = IntroSettings()
self.display_intro_1 = DisplayText(
self.intro_settings.intro_text_1,
self.intro_settings.intro_text_1_pos_x,
self.intro_settings.intro_text_1_pos_y,
self.intro_settings.font_size,
self.setting.light_grey
)
self.display_intro_2 = DisplayText(
self.intro_settings.intro_text_2,
self.intro_settings.intro_text_2_pos_x,
self.intro_settings.intro_text_2_pos_y,
self.intro_settings.font_size,
self.setting.light_grey
)
self.display_intro_title = DisplayText(
self.intro_settings.intro_text_title,
self.intro_settings.intro_text_title_pos_x,
self.intro_settings.intro_text_title_pos_y,
self.intro_settings.font_size,
self.setting.light_grey)
class Intro:
def __init__(self, screen, setting):
self.screen = screen
self.setting = setting
self.i_setting = IntroSettings()
self.intro_text = TextsObjects()
self.clock = pygame.time.Clock()
self.speed = 50 * setting.time
self.screen_c_left = setting.screen_width / 2 - self.speed / 2
self.screen_c_right = setting.screen_width / 2 + self.speed / 2
def intro_update(self):
# reset the background so you don't get multiple drawings onscreen
self.screen.fill(self.setting.dark_blueish)
if 0 <= self.i_setting.intro_text_1_pos_x <= \
self.setting.screen_width:
# Draw the first text
self.intro_text.display_intro_1.draw_me_with_shadow(
self.screen)
if self.screen_c_left < self.i_setting.intro_text_2_pos_x < \
self.screen_c_right:
# Draw the second text
self.intro_text.display_intro_2.draw_me_with_shadow(
self.screen)
pygame.display.update()
def show_intro(self):
self.i_setting.intro_text_1_pos_x += self.speed
# show the intro for self.intro_time amount of time
for frames in range(0, self.setting.fps * 3):
# test for events
for event in pygame.event.get():
print(event)
# when the window is closed stop the game loop
if event.type == pygame.QUIT:
sys.exit()
self.intro_update()
self.setting.clock.tick(self.setting.fps)
and here are the classes that are relevant:
class Settings:
def __init__(self):
# screen size
self.screen_width = 800
self.screen_height = 500
# set clock and fps
self.clock = pygame.time.Clock()
self.fps = 60
# calculate the seconds in every frame
self.time = 1 / self.fps
class IntroSettings:
def __init__(self):
self.setting = Settings()
self.font_size = 100
self.timer_font_size = 10
self.intro_text_1_pos_x = (self.setting.screen_width / 2) - 100
self.intro_text_1_pos_y = \
(self.setting.screen_height / 2 - self.font_size)
self.intro_text_1 = "A game by:"
self.intro_text_2_pos_x = (self.setting.screen_width / 2)
self.intro_text_2_pos_y = \
(self.setting.screen_height / 2 + self.font_size)
self.intro_text_2 = "Mark Olieman"
self.intro_text_title_pos_x = (self.setting.screen_width / 2)
self.intro_text_title_pos_y = (self.setting.screen_height + self.font_size)
self.intro_text_title = "Alien Invasion!"
self.intro_text_timer_pos_x = 20
self.intro_text_timer_pos_y = 20
self.speed = -2
class DisplayText:
def __init__(self, text, pos_x, pos_y, font_size, text_color):
self.font = pygame.font.Font("freesansbold.ttf", font_size)
self.text_surf = self.font.render(text, True, text_color)
self.text_rect = self.text_surf.get_rect()
self.text_rect.center = (pos_x, pos_y)
self.shadow_color = (0, 0, 0)
self.text_surf_shadow = self.font.render(text, True, self.shadow_color)
self.text_rect_shadow = self.text_surf.get_rect()
self.text_rect_shadow.center = (pos_x + 10, pos_y + 5)
def draw_me(self, screen):
screen.blit(self.text_surf, self.text_rect)
def draw_me_with_shadow(self, screen):
screen.blit(self.text_surf_shadow, self.text_rect_shadow)
screen.blit(self.text_surf, self.text_rect)
i am sorry for the amount of code but because i don't know where the problem lies i want you guys to have all the relevant information so at last here is my main game loop
import pygame
from Settings import *
from Entities import *
from Game_functions import *
from Menu import *
# make the modules available
setting = Settings()
screen = pygame.display.set_mode((setting.screen_width, setting.screen_height))
pygame.display.set_caption("Alien Invasion, By Mark Olieman")
intro = Intro(screen, setting)
player = Player(screen, setting)
bullet = Bullet(screen, setting, player)
intro_set = IntroSettings()
# initizialize pygame
pygame.init()
pygame.mixer.init()
# initizialize the screen and set a caption
# function to start gaming
def gaming():
bullets = pygame.sprite.Group()
intro.show_intro()
intro_set.intro_text_1_pos_x += intro.speed
print(intro_set.intro_text_1_pos_x)
# Game Loop
while True:
# check events and make actions happen
events(bullets, player, screen, setting)
# update everything and draw bullets
update(player, setting, bullets, screen)
# update screen at certain fps
pygame.display.update()
setting.clock.tick(setting.fps)
# Start the game
gaming()
# Stop the game
pygame.quit()
I found two mistakes in Intro.show_intro()
you add speed before loop but you have to do it inside loop
you add speed to self.i_setting.intro_text_1_pos_x but you have to add
self.intro_text.display_intro_1.text_rect.x += self.speed
self.intro_text.display_intro_1.text_rect_shadow.x += self.speed
Correct version which moves text (and shadow) "A game by:"
def show_intro(self):
#self.i_setting.intro_text_1_pos_x += self.speed
# show the intro for self.intro_time amount of time
for frames in range(0, self.setting.fps * 3):
# test for events
for event in pygame.event.get():
print(event)
# when the window is closed stop the game loop
if event.type == pygame.QUIT:
sys.exit()
# you have to add speed inside loop
#self.i_setting.intro_text_1_pos_x += self.speed
self.intro_text.display_intro_1.text_rect.x += self.speed
self.intro_text.display_intro_1.text_rect_shadow.x += self.speed
self.intro_update()
self.setting.clock.tick(self.setting.fps)
There can be another problem - rect.x can keep only integer values but speed has value 0.83 so when you add 0.83 then it can be rounded to 1 so it may move faster then you expect - or it can move too far (or may move not so smooth as you expected). If speed will be smaller then 0.5 then it may even round it to 0 so it will not move.
You may have to keep position in separated values as float values, change them and copy to rect.x only before drawing.
Full code which I run in one file - I removed some imports and elements which I hadn't (ie, Player)
import sys
class TextsObjects:
def __init__(self):
self.setting = Settings()
self.intro_settings = IntroSettings()
self.display_intro_1 = DisplayText(
self.intro_settings.intro_text_1,
self.intro_settings.intro_text_1_pos_x,
self.intro_settings.intro_text_1_pos_y,
self.intro_settings.font_size,
self.setting.light_grey
)
self.display_intro_2 = DisplayText(
self.intro_settings.intro_text_2,
self.intro_settings.intro_text_2_pos_x,
self.intro_settings.intro_text_2_pos_y,
self.intro_settings.font_size,
self.setting.light_grey
)
self.display_intro_title = DisplayText(
self.intro_settings.intro_text_title,
self.intro_settings.intro_text_title_pos_x,
self.intro_settings.intro_text_title_pos_y,
self.intro_settings.font_size,
self.setting.light_grey)
class Intro:
def __init__(self, screen, setting):
self.screen = screen
self.setting = setting
self.i_setting = IntroSettings()
self.intro_text = TextsObjects()
self.clock = pygame.time.Clock()
self.speed = 150 * setting.time
print(self.speed)
self.screen_c_left = setting.screen_width / 2 - self.speed / 2
self.screen_c_right = setting.screen_width / 2 + self.speed / 2
def intro_update(self):
# reset the background so you don't get multiple drawings onscreen
self.screen.fill(self.setting.dark_blueish)
if 0 <= self.i_setting.intro_text_1_pos_x <= self.setting.screen_width:
# Draw the first text
self.intro_text.display_intro_1.draw_me_with_shadow(self.screen)
if self.screen_c_left < self.i_setting.intro_text_2_pos_x < self.screen_c_right:
# Draw the second text
self.intro_text.display_intro_2.draw_me_with_shadow(self.screen)
pygame.display.update()
def show_intro(self):
#self.i_setting.intro_text_1_pos_x += self.speed
# show the intro for self.intro_time amount of time
for frames in range(0, self.setting.fps * 3):
# test for events
for event in pygame.event.get():
print(event)
# when the window is closed stop the game loop
if event.type == pygame.QUIT:
sys.exit()
#self.i_setting.intro_text_1_pos_x += self.speed
self.intro_text.display_intro_1.text_rect.x += self.speed
self.intro_text.display_intro_1.text_rect_shadow.x += self.speed
self.intro_update()
self.setting.clock.tick(self.setting.fps)
class Settings:
def __init__(self):
# screen size
self.screen_width = 800
self.screen_height = 500
# set clock and fps
self.clock = pygame.time.Clock()
self.fps = 60
# calculate the seconds in every frame
self.time = 1 / self.fps
self.light_grey = (128,128,128)
self.dark_blueish = (0,0,255)
class IntroSettings:
def __init__(self):
self.setting = Settings()
self.font_size = 100
self.timer_font_size = 10
self.intro_text_1_pos_x = (self.setting.screen_width / 2) - 100
self.intro_text_1_pos_y = \
(self.setting.screen_height / 2 - self.font_size)
self.intro_text_1 = "A game by:"
self.intro_text_2_pos_x = (self.setting.screen_width / 2)
self.intro_text_2_pos_y = \
(self.setting.screen_height / 2 + self.font_size)
self.intro_text_2 = "Mark Olieman"
self.intro_text_title_pos_x = (self.setting.screen_width / 2)
self.intro_text_title_pos_y = (self.setting.screen_height + self.font_size)
self.intro_text_title = "Alien Invasion!"
self.intro_text_timer_pos_x = 20
self.intro_text_timer_pos_y = 20
self.speed = -2
class DisplayText:
def __init__(self, text, pos_x, pos_y, font_size, text_color):
self.font = pygame.font.Font("freesansbold.ttf", font_size)
self.text_surf = self.font.render(text, True, text_color)
self.text_rect = self.text_surf.get_rect()
self.text_rect.center = (pos_x, pos_y)
self.shadow_color = (0, 0, 0)
self.text_surf_shadow = self.font.render(text, True, self.shadow_color)
self.text_rect_shadow = self.text_surf.get_rect()
self.text_rect_shadow.center = (pos_x + 10, pos_y + 5)
def draw_me(self, screen):
screen.blit(self.text_surf, self.text_rect)
def draw_me_with_shadow(self, screen):
screen.blit(self.text_surf_shadow, self.text_rect_shadow)
screen.blit(self.text_surf, self.text_rect)
import pygame
#from Entities import *
#from Game_functions import *
#from Menu import *
# initizialize pygame
pygame.init()
pygame.mixer.init()
# make the modules available
setting = Settings()
screen = pygame.display.set_mode((setting.screen_width, setting.screen_height))
pygame.display.set_caption("Alien Invasion, By Mark Olieman")
intro = Intro(screen, setting)
#player = Player(screen, setting)
#bullet = Bullet(screen, setting, player)
intro_set = IntroSettings()
# initizialize the screen and set a caption
# function to start gaming
def gaming():
bullets = pygame.sprite.Group()
intro.show_intro()
intro_set.intro_text_1_pos_x += intro.speed
print(intro_set.intro_text_1_pos_x)
# Game Loop
while True:
# check events and make actions happen
#events(bullets, player, screen, setting)
# update everything and draw bullets
#update(player, setting, bullets, screen)
# update screen at certain fps
pygame.display.update()
setting.clock.tick(setting.fps)
# Start the game
gaming()
# Stop the game
pygame.quit()

Python game keys are not responding

I am trying to implement a Python game (aliens.py from the PyGame package) but when I run it, I can't move my player or do any shooting...
I run it on Mac OS X, python3 and have a french keyboard. Could this have anything to do the fact that it does not take any of my keyboard commands?
On the screen of my terminal I see:
^[ (when I press esc),
^[[D (when I press the left arrow),
^[[C (when I press the right arrow),
^[[A (when I press the upwards arrow),
^[[B (when I press the downwards arrow
...
Is this normal? It does not help replacing K_RIGHT with ^[[C.
#!/usr/bin/env python
import random, os.path
#import basic pygame modules
import pygame
from pygame.locals import *
#see if we can load more than standard BMP
if not pygame.image.get_extended():
raise SystemExit("Sorry, extended image module required")
#game constants
MAX_SHOTS = 2 #most player bullets onscreen
ALIEN_ODDS = 22 #ances a new alien appears
BOMB_ODDS = 60 #chances a new bomb will drop
ALIEN_RELOAD = 12 #frames between new aliens
SCREENRECT = Rect(0, 0, 940, 480)
SCORE = 0
main_dir = os.path.split(os.path.abspath(__file__))[0]
def load_image(file):
"loads an image, prepares it for play"
file = os.path.join(main_dir, 'data', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))
return surface.convert()
def load_images(*files):
imgs = []
for file in files:
imgs.append(load_image(file))
return imgs
class dummysound:
def play(self): pass
def load_sound(file):
if not pygame.mixer: return dummysound()
file = os.path.join(main_dir, 'data', file)
try:
sound = pygame.mixer.Sound(file)
return sound
except pygame.error:
print ('Warning, unable to load, %s' % file)
return dummysound()
# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard
class Player(pygame.sprite.Sprite):
speed = 10
bounce = 24
gun_offset = -11
images = []
def __init__(self):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
self.reloading = 0
self.origtop = self.rect.top
self.facing = -1
def move(self, direction):
if direction: self.facing = direction
self.rect.move_ip(direction*self.speed, 0)
self.rect = self.rect.clamp(SCREENRECT)
if direction < 0:
self.image = self.images[0]
elif direction > 0:
self.image = self.images[1]
self.rect.top = self.origtop - (self.rect.left//self.bounce%2)
def gunpos(self):
pos = self.facing*self.gun_offset + self.rect.centerx
return pos, self.rect.top
class Alien(pygame.sprite.Sprite):
speed = 13
animcycle = 12
images = []
def __init__(self):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.facing = random.choice((-1,1)) * Alien.speed
self.frame = 0
if self.facing < 0:
self.rect.right = SCREENRECT.right
def update(self):
self.rect.move_ip(self.facing, 0)
if not SCREENRECT.contains(self.rect):
self.facing = -self.facing;
self.rect.top = self.rect.bottom + 1
self.rect = self.rect.clamp(SCREENRECT)
self.frame = self.frame + 1
self.image = self.images[self.frame//self.animcycle%3]
class Explosion(pygame.sprite.Sprite):
defaultlife = 12
animcycle = 3
images = []
def __init__(self, actor):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(center=actor.rect.center)
self.life = self.defaultlife
def update(self):
self.life = self.life - 1
self.image = self.images[self.life//self.animcycle%2]
if self.life <= 0: self.kill()
class Shot(pygame.sprite.Sprite):
speed = -11
images = []
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(midbottom=pos)
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.top <= 0:
self.kill()
class Bomb(pygame.sprite.Sprite):
speed = 9
images = []
def __init__(self, alien):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = self.images[0]
self.rect = self.image.get_rect(midbottom=
alien.rect.move(0,5).midbottom)
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.bottom >= 470:
Explosion(self)
self.kill()
class Score(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(None, 20)
self.font.set_italic(1)
self.color = Color('white')
self.lastscore = -1
self.update()
self.rect = self.image.get_rect().move(10, 450)
def update(self):
if SCORE != self.lastscore:
self.lastscore = SCORE
msg = "Score: %d" % SCORE
self.image = self.font.render(msg, 0, self.color)
def main(winstyle = 0):
# Initialize pygame
pygame.init()
if pygame.mixer and not pygame.mixer.get_init():
print ('Warning, no sound')
pygame.mixer = None
# Set the display mode
winstyle = 0 # |FULLSCREEN
bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32)
screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth)
#Load images, assign to sprite classes
#(do this before the classes are used, after screen setup)
img = load_image('player1.gif')
Player.images = [img, pygame.transform.flip(img, 1, 0)]
img = load_image('explosion1.gif')
Explosion.images = [img, pygame.transform.flip(img, 1, 1)]
Alien.images = load_images('alien1.gif', 'alien2.gif', 'alien3.gif')
Bomb.images = [load_image('bomb.gif')]
Shot.images = [load_image('shot.gif')]
#decorate the game window
icon = pygame.transform.scale(Alien.images[0], (32, 32))
pygame.display.set_icon(icon)
pygame.display.set_caption('Pygame Aliens')
pygame.mouse.set_visible(0)
#create the background, tile the bgd image
bgdtile = load_image('background.gif')
background = pygame.Surface(SCREENRECT.size)
for x in range(0, SCREENRECT.width, bgdtile.get_width()):
background.blit(bgdtile, (x, 0))
screen.blit(background, (0,0))
pygame.display.flip()
#load the sound effects
boom_sound = load_sound('boom.wav')
shoot_sound = load_sound('car_door.wav')
if pygame.mixer:
music = os.path.join(main_dir, 'data', 'house_lo.wav')
pygame.mixer.music.load(music)
pygame.mixer.music.play(-1)
# Initialize Game Groups
aliens = pygame.sprite.Group()
shots = pygame.sprite.Group()
bombs = pygame.sprite.Group()
all = pygame.sprite.RenderUpdates()
lastalien = pygame.sprite.GroupSingle()
#assign default groups to each sprite class
Player.containers = all
Alien.containers = aliens, all, lastalien
Shot.containers = shots, all
Bomb.containers = bombs, all
Explosion.containers = all
Score.containers = all
#Create Some Starting Values
global score
alienreload = ALIEN_RELOAD
kills = 0
clock = pygame.time.Clock()
#initialize our starting sprites
global SCORE
player = Player()
Alien() #note, this 'lives' because it goes into a sprite group
if pygame.font:
all.add(Score())
while player.alive():
#get input
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
return
keystate = pygame.key.get_pressed()
# clear/erase the last drawn sprites
all.clear(screen, background)
#update all the sprites
all.update()
#handle player input
direction = keystate[K_RIGHT] - keystate[K_LEFT]
player.move(direction)
firing = keystate[K_SPACE]
if not player.reloading and firing and len(shots) < MAX_SHOTS:
Shot(player.gunpos())
shoot_sound.play()
player.reloading = firing
# Create new alien
if alienreload:
alienreload = alienreload - 1
elif not int(random.random() * ALIEN_ODDS):
Alien()
alienreload = ALIEN_RELOAD
# Drop bombs
if lastalien and not int(random.random() * BOMB_ODDS):
Bomb(lastalien.sprite)
# Detect collisions
for alien in pygame.sprite.spritecollide(player, aliens, 1):
boom_sound.play()
Explosion(alien)
Explosion(player)
SCORE = SCORE + 1
player.kill()
for alien in pygame.sprite.groupcollide(shots, aliens, 1, 1).keys():
boom_sound.play()
Explosion(alien)
SCORE = SCORE + 1
for bomb in pygame.sprite.spritecollide(player, bombs, 1):
boom_sound.play()
Explosion(player)
Explosion(bomb)
player.kill()
#draw the scene
dirty = all.draw(screen)
pygame.display.update(dirty)
#cap the framerate
clock.tick(40)
if pygame.mixer:
pygame.mixer.music.fadeout(1000)
pygame.time.wait(1000)
pygame.quit()
#call the "main" function if running this script
if __name__ == '__main__': main()
Event KEYDOWN sends values unicode, key, mod and you can display it to see what codes (numeric values) uses your keyboard.
if event.type == KEYDOWN;
print('key:', event.key)
print('unicode:', event.uniconde)
print('mod:', event.mod)
And then you can use them to test your keys and move objects.
It is surely due to keyboard keys problem , just go to this documentation and find out proper keyword for your respective keyboard button:
"https://www.pygame.org/docs/ref/key.html"

Why doesnt my sprite move?

I'm new to Pygame and I'm trying to move my sprite on my background image.
My sprite is not re appearing after it moves? Any ideas?
This is most of the program without some screens.
I have been trying to get this to work for many hours,
#dependencies
import pygame as P
import random as R
def welcome(screen):
#load background
bg = P.image.load("space-wallpaper.jpg")
screen.blit(bg,[0,0])
#set fonts etc
font = P.font.Font("Space_Age.ttf",60)
width, height = screen.get_size()
#play button
message = "PLAY "
text = font.render(message,1,[255 , 0, 0])
rect = text.get_rect()
x, y = text.get_size()
rect = rect.move((width - x)/2, (height - y)/2)
screen.blit(text,rect)
#high_score button
message = "HIGH SCORE "
text = font.render(message,1,[255 , 0, 0])
rect = text.get_rect()
x, y = text.get_size()
rect = rect.move((width - x)/2, (height - y)/2 +100)
screen.blit(text,rect)
def play_button(screen):
"""launch welcome screen.
"""
#welcome screen play button
font = P.font.Font("Space_Age.ttf",60)
message = "PLAY "
play_x,play_y = font.size(message)
play_text = font.render(message,1,[255 , 0, 0])
width, height = 800,600
screen.blit(play_text,[(width - play_x)/2, (height - play_y)/2])
play_rect = play_text.get_rect().move((width - play_x)/2, (height - play_y)/2)
P.display.flip()
return(play_rect)
def welcome_background(screen):
# Welcome screen background
bg = P.image.load("space-wallpaper.jpg")
screen.blit(bg,[0,0])
P.display.update()
def high_score_screen(screen):
"""opens the highscore screen"""
high_score_bg = P.image.load("bg_game.jpg")
screen.blit(high_score_bg,[0,0])
P.display.update()
def splash_screen(screen):
"""loads the first screen in the game with a 3 sec wait"""
splash_image = P.image.load('splash.jpg')
screen.blit(splash_image,[0,0])
P.display.update()
P.time.wait(2001)
def play_game(screen):
"""loads the play game screen"""
game_bg = P.image.load("bg_game.jpg")
screen.blit(game_bg,[0,0])
P.display.update()
def move_right(screen,x_cord,flagship):
dist = 20
play_game(screen)
x_cord = x_cord + dist
print(x_cord)
screen.blit(flagship,[x_cord])
P.display.update()
def key_detection(screen,flagship,x_cord):
key = P.key.get_pressed()
if key[P.K_RIGHT]:
move_right(screen,x_cord,flagship)
#move_right()
elif key[P.K_LEFT]:
print("left")
class Sprite():
def __init__(self,screen):
""" The constructor of the class """
self.flagship = P.image.load("sprite2.png")
self.x = 0
self.y = 0
def display(self,screen):
#screen.blit(self.sprite,[self.x,self.y]) changed by geoff
screen.blit(self.flagship,[self.x,self.y])
P.display.update()
_init_
# dependencies
from mods import *
import pygame as P
#initialise pygame
P.init()
def main():
# parameters to control pygame basics
screen_size = width, height = 800,600 #sixe of playing screen
P.display.set_caption('Space Smasher!')
screen = P.display.set_mode(screen_size)
clock = P.time.Clock() # timer used to control rate of looping
loop_rate = 20 #number of times per second does loop
play = True #control the playing of the actual game
splash_screen(screen)
welcome(screen)
P.display.flip()
rect_play = play_button(screen)
flagship = Sprite(screen)
while play:
key_detection(screen,flagship.image,flagship.x)
# for event in P.event.poll(): changed by geoff
event = P.event.poll() #did the player do something?
if event.type == P.QUIT:
play = False
if event.type == P.MOUSEBUTTONDOWN:
player_position = P.mouse.get_pos()
if rect_play.collidepoint(player_position):
play_game(screen)
flagship.display(screen)
P.quit()
if __name__ == '__main__':
main()
You are not calling either of your functions or your classes anywhere. You need a while loop that is similarly structured to this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#Do your movements
With the following imports at the top:
import pygame, sys
from pygame.locals import *
Here is an example of moving an object with the keys:
import pygame, sys
from pygame.locals import *
pygame.init()
WIDTH=1439
HEIGHT=791
DISPLAYSURF = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Hello Pygame World!')
pygame.key.set_repeat(1, 10)
circx, circy = 200, 150
CIRWIDTH=20
while True: # main game loop
if pygame.key.get_pressed()[pygame.K_UP]:
circy-=5
if pygame.key.get_pressed()[pygame.K_DOWN]:
circy+=5
if pygame.key.get_pressed()[pygame.K_RIGHT]:
circx+=5
if pygame.key.get_pressed()[pygame.K_LEFT]:
circx-=5
try:
for event in pygame.event.get():
if event.type == QUIT or event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
pygame.quit()
sys.exit()
except AttributeError:
pass
DISPLAYSURF.fill((0, 0, 0))
pygame.draw.circle(DISPLAYSURF, (158, 219, 222), (circx, circy), CIRWIDTH)
pygame.display.flip()
Main loop should be similar to:
while True:
# events - check keyboad and mouse and change player direction or speed
# move - move sprite with speed and direction
# check collison
# draw background, all objects and all sprites
# clock - to keep constant speed - FPS (Frames Per Seconds)
So you have to move sprites in every loop and draw them.

Pygame Collision Detection: AttributeError

I've been banging my head against this for a while. I am trying to make a game with PyGame and I got up to the collision segment and have been stuck for a while and have checked a few threads.
This is the code I have (removed other methods and conditional statements in between, but left the relevant parts). I am a little confused by the error because I do a self.imageRect = self.image.get_rect() in both classes init, yet I have this error. The error was specifically:
"AttributeError: 'Pebble' object has no attribute 'rect'" when the program attempts to carry out the collision detection part in the dog class. What have I been doing wrong?
import random
import pygame, sys
pygame.init()
clock = pygame.time.Clock() # fps clock
screenSize = WIDTH, HEIGHT = [800, 600]
screen = pygame.display.set_mode(screenSize)
background = pygame.Surface(screen.get_size())
bgColorRGB = [153, 204, 255]
background.fill(bgColorRGB)
pebbleGroup = pygame.sprite.Group()
pebbleSingle = pygame.sprite.GroupSingle()
dogSingle = pygame.sprite.GroupSingle()
#----------------------------------------------------------------------
class Dog(pygame.sprite.Sprite):
def __init__(self, path, speed):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load(path) # load sprite from path/file loc.
self.imageRect = self.image.get_rect() # get bounds of image
self.imageWidth = self.image.get_width()
self.imageHeight = self.image.get_height()
self.speed = speed
# sets location of the image, gets the start location of object
# sets the start location as the image's left and top location
def setLocation(self, location):
self.imageRect.left, self.imageRect.top = location
def checkCollision(self, pebble, dogGroup):
if pygame.sprite.spritecollide(pebble, dogGroup, False):
print "collided"
#---------------------------------------------------------------------
class Pebble(pygame.sprite.Sprite):
def __init__(self, path, speed, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(path)
self.imageRect = self.image.get_rect()
self.imageWidth = self.image.get_width()
self.imageHeight = self.image.get_height()
self.imageRect.left, self.imageRect.top = location
self.speed = speed # initialize speed
self.isDragged = False
#----------------------------------------------------------------------
def startGame():
pebblePaths = ['images/pebble/pebble1.jpg', 'images/pebble/pebble2.jpg']
for i in range(3):
pebblePath = pebblePaths[random.randrange(0, len(pebblePaths))]
pebbleSpeed = [random.randrange(1, 7), 0]
pebbleLocation = [0, random.randrange(20, HEIGHT - 75)]
pebble = Pebble(pebblePath, pebbleSpeed, pebbleLocation)
pebbleGroup.add(pebble)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for pebble in pebbleGroup:
dog.checkCollision(pebble, dogSingle)
pygame.display.flip()
clock.tick(30) # wait a little before starting again
startGame()
It's expecting Sprite.rect, so change from
self.imageRect = self.image.get_rect()
#to
self.rect = self.image.get_rect()
Note
self.imageWidth = self.image.get_width()
self.imageHeight = self.image.get_height()
self.imageRect.left, self.imageRect.top = location
These are not necessary, since rect's have many properties, like self.rect.width or self.rect.topleft = location . Another useful one is centerx.
full list at: pygame Rect docs

What do i call if its in a special method

I'm trying to make a function when the mouse button is up, it changes the picture of the ghosts to one single image.
Problem being, I have no idea what to call upon (Hence the ??? in the script). It's hard because the ghosts are created via a loop. Can anyone possibly help?
Maybe i need to change the ghosts into sprites? could you help with that too?
import pygame
import random
import sys
class Ball:
def __init__(self,X,Y,imagefile):
self.velocity = [3,3]
self.ball_image = pygame.image.load (imagefile). convert() ### i want this image to change
self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
self.sound = pygame.mixer.Sound ('Thump.wav')
if __name__ =='__main__':
width = 800
height = 600
background_colour = 0,0,0
GHOST_IMAGE = ["images/blue-right.png", "images/red-right.png", "images/orange-right.png", "images/pink-right.png"]
GHOST_IMAGETWO = ["images/blue-left.png", "images/red-left.png", "images/orange-left.png", "images/pink-left.png"]
pygame.init()
frame = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bouncing Ball animation")
num_balls = 4
ball_list = []
for i in range(num_balls):
ball_list.append( Ball(random.randint(0, width),random.randint(0, height), (GHOST_IMAGE[i]) ))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.MOUSEBUTTONUP:
??? = pygame.image.load("images/vulnerable.png").convert() ###i know that this is where and what i need to change it to, but dont know what instance name to call upon.
frame.fill(background_colour)
for ball in ball_list:
if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width:
ball.sound.play()
ball.velocity[0] = -1 * ball.velocity[0]
if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height:
ball.sound.play()
ball.velocity[1] = -1 * ball.velocity[1]
ball.ball_boundary = ball.ball_boundary.move (ball.velocity)
frame.blit (ball.ball_image, ball.ball_boundary)
pygame.display.flip()
One way is to just iterate over the ball_list and change each ball:
elif event.type == pygame.MOUSEBUTTONUP:
image = pygame.image.load("images/vulnerable.png").convert()
for ball in ball_list:
ball.ball_image = image
Another way would be implement the image-changing behaviour directly in the Ball class:
class Ball:
def __init__(self,X,Y,imagefile):
self.vulnerable = False
self.velocity = [3,3]
self.normal_ball_image = pygame.image.load (imagefile). convert()
self.v_ball_image = pygame.image.load("images/vulnerable.png").convert()
self.ball_image = self.normal_ball_image
self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
self.sound = pygame.mixer.Sound ('Thump.wav')
def toggle(self):
self.vulnerable = not self.vulnerable
self.ball_image = self.v_ball_image if self.vulnerable else self.normal_ball_image
And in your loop:
elif event.type == pygame.MOUSEBUTTONUP:
for ball in ball_list:
ball.toggle()

Categories

Resources