Simple pygame for python 2.7 code that works
It's Not Real :: stuff :: visible
Page 1 of 1
Simple pygame for python 2.7 code that works
Arrow key movement.
import pygame, sys, time
from pygame.locals import *
pygame.init()
FPS=30
fpsClock=pygame.time.Clock()
width=400
height=300
DISPLAYSURF=pygame.display.set_mode((width,height),0,32)
pygame.display.set_caption('Animation')
background=pygame.image.load('COWALtown.png')
UP='up'
LEFT='left'
RIGHT='right'
DOWN='down'
sprite=pygame.image.load('GoldWarrioeDown.png')
spritex=200
spritey=130
direction=DOWN
pygame.mixer.music.load('zasT.mp3')
pygame.mixer.music.play(-1, 0.0)
while True:
DISPLAYSURF.blit(background,(0,0))
DISPLAYSURF.blit(sprite,(spritex,spritey))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
sprite=pygame.image.load('GoldWarrioeLeft.png')
elif (event.key == K_RIGHT):
sprite=pygame.image.load('GoldWarrioeRight.png')
elif (event.key == K_UP):
sprite=pygame.image.load('GoldWarrioeUp.png')
elif (event.key == K_DOWN):
sprite=pygame.image.load('GoldWarrioeDown.png')
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
spritex -= 5
if keys_pressed[K_RIGHT]:
spritex += 5
if keys_pressed[K_UP]:
spritey -= 5
if keys_pressed[K_DOWN]:
spritey += 5
pygame.display.update()
fpsClock.tick(FPS)
import pygame, sys, time
from pygame.locals import *
pygame.init()
FPS=30
fpsClock=pygame.time.Clock()
width=400
height=300
DISPLAYSURF=pygame.display.set_mode((width,height),0,32)
pygame.display.set_caption('Animation')
background=pygame.image.load('COWALtown.png')
UP='up'
LEFT='left'
RIGHT='right'
DOWN='down'
sprite=pygame.image.load('GoldWarrioeDown.png')
spritex=200
spritey=130
direction=DOWN
pygame.mixer.music.load('zasT.mp3')
pygame.mixer.music.play(-1, 0.0)
while True:
DISPLAYSURF.blit(background,(0,0))
DISPLAYSURF.blit(sprite,(spritex,spritey))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
sprite=pygame.image.load('GoldWarrioeLeft.png')
elif (event.key == K_RIGHT):
sprite=pygame.image.load('GoldWarrioeRight.png')
elif (event.key == K_UP):
sprite=pygame.image.load('GoldWarrioeUp.png')
elif (event.key == K_DOWN):
sprite=pygame.image.load('GoldWarrioeDown.png')
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT]:
spritex -= 5
if keys_pressed[K_RIGHT]:
spritex += 5
if keys_pressed[K_UP]:
spritey -= 5
if keys_pressed[K_DOWN]:
spritey += 5
pygame.display.update()
fpsClock.tick(FPS)
Last edited by Adminator on Thu Dec 19, 2013 3:46 pm; edited 4 times in total
Re: Simple pygame for python 2.7 code that works
Collision
#! /usr/bin/env python
############################################################################
# File name : detectSpriteCollsion.py
# Purpose : Demostarting The Killing of Sprite On Collision
# Usages : Logic can be used in real time games
# Start date : 04/01/2012
# End date : 04/01/2012
# Author : Ankur Aggarwal
# License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html
# How To Run: python detectSpriteCollision.py
############################################################################
import pygame
from pygame.locals import *
import random
screen=pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("Collision Detection")
#creating the boxes
class Boxes(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((50,50))
self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.rect=self.image.get_rect()
self.rect.center=(random.randint(0,255),random.randint(0,255))
#creating circle
class Circle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((50,50))
self.image.fill((0,255,0))
pygame.draw.circle(self.image,(255,0,0),(25,25),25,0)
self.rect=self.image.get_rect()
def update(self):
self.rect.center=pygame.mouse.get_pos()
def main():
background=pygame.Surface(screen.get_size())
background=background.convert()
background.fill((0,255,0))
screen.blit(background,(0,0))
boxes=[]
for i in xrange(0,10):
boxes.append(Boxes())
circle=Circle()
allSprites=pygame.sprite.Group(boxes)
circleSprite=pygame.sprite.Group(circle)
while 1:
for i in pygame.event.get():
if i.type==QUIT:
exit()
#checking the collision.check 'pydoc pygame.sprite.spritecollide' for mode details. True is used for sprite killing. It doesn't kill the sprite in actual.It is still present in the computer memory though.It has just removed it from the group so that no further display of that sprite is possible.
if pygame.sprite.spritecollide(circle,allSprites,True):
print "collision"
#following the CUD method
allSprites.clear(screen,background)
circleSprite.clear(screen,background)
allSprites.update()
circleSprite.update()
allSprites.draw(screen)
circleSprite.draw(screen)
pygame.display.flip()
if __name__=='__main__':
main()
"""You can also check the collision about the rect attributes. There are many ways to do that.Example:
1.circle.rect.colliderect(box1) will check the collision between the circle and box1 collision
2. pygame.sprite.collide_rect(sprite1,sprite2) willl also do the same """
#! /usr/bin/env python
############################################################################
# File name : detectSpriteCollsion.py
# Purpose : Demostarting The Killing of Sprite On Collision
# Usages : Logic can be used in real time games
# Start date : 04/01/2012
# End date : 04/01/2012
# Author : Ankur Aggarwal
# License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html
# How To Run: python detectSpriteCollision.py
############################################################################
import pygame
from pygame.locals import *
import random
screen=pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("Collision Detection")
#creating the boxes
class Boxes(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((50,50))
self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.rect=self.image.get_rect()
self.rect.center=(random.randint(0,255),random.randint(0,255))
#creating circle
class Circle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((50,50))
self.image.fill((0,255,0))
pygame.draw.circle(self.image,(255,0,0),(25,25),25,0)
self.rect=self.image.get_rect()
def update(self):
self.rect.center=pygame.mouse.get_pos()
def main():
background=pygame.Surface(screen.get_size())
background=background.convert()
background.fill((0,255,0))
screen.blit(background,(0,0))
boxes=[]
for i in xrange(0,10):
boxes.append(Boxes())
circle=Circle()
allSprites=pygame.sprite.Group(boxes)
circleSprite=pygame.sprite.Group(circle)
while 1:
for i in pygame.event.get():
if i.type==QUIT:
exit()
#checking the collision.check 'pydoc pygame.sprite.spritecollide' for mode details. True is used for sprite killing. It doesn't kill the sprite in actual.It is still present in the computer memory though.It has just removed it from the group so that no further display of that sprite is possible.
if pygame.sprite.spritecollide(circle,allSprites,True):
print "collision"
#following the CUD method
allSprites.clear(screen,background)
circleSprite.clear(screen,background)
allSprites.update()
circleSprite.update()
allSprites.draw(screen)
circleSprite.draw(screen)
pygame.display.flip()
if __name__=='__main__':
main()
"""You can also check the collision about the rect attributes. There are many ways to do that.Example:
1.circle.rect.colliderect(box1) will check the collision between the circle and box1 collision
2. pygame.sprite.collide_rect(sprite1,sprite2) willl also do the same """
Last edited by Adminator on Thu Dec 19, 2013 3:47 pm; edited 1 time in total
Re: Simple pygame for python 2.7 code that works
Hello nerds
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False
font = pygame.font.SysFont("comicsansms", 72)
text = font.render("Hello, Nerds", True, (0, 128, 0))
##in mainloop
screen.fill((255, 255, 255))
screen.blit(text,
(320 - text.get_width() // 2, 240 - text.get_height() // 2))
pygame.display.flip()
clock.tick(60)
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False
font = pygame.font.SysFont("comicsansms", 72)
text = font.render("Hello, Nerds", True, (0, 128, 0))
##in mainloop
screen.fill((255, 255, 255))
screen.blit(text,
(320 - text.get_width() // 2, 240 - text.get_height() // 2))
pygame.display.flip()
clock.tick(60)
It's Not Real :: stuff :: visible
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|