import pygame, sys, time, random, pika, json, uuid from threading import Thread pygame.init() window = pygame.display.set_mode((800, 600)) screen = pygame.Surface((800, 600)) pygame.display.set_caption("Tank") global done done = False global hello hello = False global intl intl = False '''Menu''' class Menu: def __init__(self, lists=[360, 200, u"Write", (50, 250, 30), (250, 30, 250)]): self.lists = lists def render(self, surf, font, num_list): for i in self.lists: if num_list == i[5]: surf.blit(font.render(i[2], 1, i[4]), (i[0], i[1])) else: surf.blit(font.render(i[2], 1, i[3]), (i[0], i[1])) def menu(self): font_menu = pygame.font.SysFont("algerian", 50)
import sys, pygame #pygame.font.init() pygame.init() size = 800, 480 screen = pygame.display.set_mode(size) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((255, 255, 255)) surface = pygame.Surface((100, 100)) image = pygame.image.load('main.png') button = pygame.image.load('button.png') circle = pygame.image.load('circle.png') font = pygame.font.Font(None, 36) text = font.render("Hello, World", 1, (255, 0, 0)) textPos = text.get_rect(centerx = background.get_width()//2, centery = background.get_height()//2) background.blit(text, textPos) screen.blit(background, background.get_rect()) screen.blit(image, (0, 0)) screen.blit(button, (10, 10)) screen.blit(circle, (30, 30)) pygame.display.flip() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
all_sprites.add(new_enemy) # why is this needed? elif event.type == add_cloud: new_cloud = Cloud() clouds.add(new_cloud) all_sprites.add(new_cloud) screen.fill((135, 206, 250)) pressed_keys = pygame.key.get_pressed() player.update(pressed_keys) enemies.update() # update enemy position clouds.update() # update cloud every loop # create a surf object surf = pygame.Surface((50, 50)) # a surface object is 50 pixel by 50 pixel surf.fill((125, 125, 125)) # fill it black rect = surf.get_rect() surf_center = ((SCREEN_WIDTH - surf.get_width()) / 2, (SCREEN_HEIGHT - surf.get_height()) / 2) player_location = (0, (SCREEN_HEIGHT - player.surf.get_height()) / 2) # block transfer putting surface onto another surface # screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)) # this only puts the top corner of the surface object # screen.blit(surf, surf_center) # this copies surface onto the screen, at exactly the center of the screen # screen.blit(player.surf, player.rect) for entity in all_sprites: screen.blit(entity.surf, entity.rect) if pygame.sprite.spritecollideany(player, enemies): player.kill()
# Listing_18-1.py # Copyright Warren & Carter Sande, 2013 # Released under MIT license http://www.opensource.org/licenses/mit-license.php # Version $version ---------------------------- # Program z odbijającą się piłką, sprajtami i funkcją clock.tick() import pygame, sys pygame.init() ekran = pygame.display.set_mode([640, 480]) tlo = pygame.Surface(ekran.get_size()) tlo.fill([255, 255, 255]) zegar = pygame.time.Clock() # Klasa Pilka zawierająca metodę przesun() class Pilka(pygame.sprite.Sprite): def __init__(self, plik_obrazka, predkosc, polozenie): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(plik_obrazka) self.rect = self.image.get_rect() self.rect.left, self.rect.top = polozenie self.predkosc = predkosc def przesun(self): if self.rect.left <= ekran.get_rect().left or \ self.rect.right >= ekran.get_rect().right: self.predkosc[0] = -self.predkosc[0] nowaPozycja = self.rect.move(self.predkosc) self.rect = nowaPozycja
class Block(pygame.sprite.Sprite): def __init__(self, color, (width, height)): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([width, height]) self.image.fill(color) self.rect = self.image.get_rect()
display.set_caption('Giperborea') text_intro_screen = Surface ((600, 150)) start_screen = Surface((840, 630)) #background = image.load ('images/back.png') adventure_screen = Surface ((840, 420)) instrumental_screen = Surface ((880,190)) hero_screen = Surface ((180,190)) monster_screen = Surface ((190,200)) information_screen = Surface ((680,190)) roll_screen = Surface ((600, 170)) high_screen = Surface ((810, 22)) inventory_screen = Surface ((380,380)) char_screen = Surface ((380,380)) journal_screen = Surface ((380,380)) start_screen_text_surface = pygame.Surface ((300, 100)) start_screen_version_surface = pygame.Surface ((300, 100)) def main_interface (level): window.blit(start_screen, (10, 10)) start_screen.fill ((black)) start_screen.blit (high_screen, (0,2)) high_screen.fill ((black)) start_screen.blit (adventure_screen, (0,25)) adventure_screen.fill ((black)) start_screen.blit (instrumental_screen, (0,430)) instrumental_screen.fill ((black)) instrumental_screen.blit (hero_screen, (0,2)) hero_screen.fill ((black))
def __init__(self, font, dim, pos): self.font = font self.dim = dim self.pos = pos self.seconds_left = 0 self.surface = pygame.Surface(self.dim)
def test_subsurface_lock(self): sf = pygame.Surface((5, 5)) subsf = sf.subsurface((1, 1, 2, 2)) sf2 = pygame.Surface((5, 5)) # Simple blits, nothing should happen here. sf2.blit(subsf, (0, 0)) sf2.blit(sf, (0, 0)) # Test blitting on self: self.assertRaises(pygame.error, sf.blit, subsf, (0, 0)) #self.assertRaises(pygame.error, subsf.blit, sf, (0, 0)) # ^ Fails although it should not in my opinion. If I cannot # blit the subsurface to the surface, it should not be allowed # the other way around as well. # Test additional locks. sf.lock() sf2.blit(subsf, (0, 0)) self.assertRaises(pygame.error, sf2.blit, sf, (0, 0)) subsf.lock() self.assertRaises(pygame.error, sf2.blit, subsf, (0, 0)) self.assertRaises(pygame.error, sf2.blit, sf, (0, 0)) # sf and subsf are now explicitly locked. Unlock sf, so we can # (assume) to blit it. # It will fail though as the subsurface still has a lock around, # which is okay and correct behaviour. sf.unlock() self.assertRaises(pygame.error, sf2.blit, subsf, (0, 0)) self.assertRaises(pygame.error, sf2.blit, sf, (0, 0)) # Run a second unlock on the surface. This should ideally have # no effect as the subsurface is the locking reason! sf.unlock() self.assertRaises(pygame.error, sf2.blit, sf, (0, 0)) self.assertRaises(pygame.error, sf2.blit, subsf, (0, 0)) subsf.unlock() sf.lock() self.assertEqual(sf.get_locked(), True) self.assertEqual(sf.get_locks(), (sf,)) self.assertEqual(subsf.get_locked(), False) self.assertEqual(subsf.get_locks(), ()) subsf.lock() self.assertEqual(sf.get_locked(), True) self.assertEqual(sf.get_locks(), (sf, subsf)) self.assertEqual(subsf.get_locked(), True) self.assertEqual(subsf.get_locks(), (subsf,)) sf.unlock() self.assertEqual(sf.get_locked(), True) self.assertEqual(sf.get_locks(), (subsf,)) self.assertEqual(subsf.get_locked(), True) self.assertEqual(subsf.get_locks(), (subsf,)) subsf.unlock() self.assertEqual(sf.get_locked(), False) self.assertEqual(sf.get_locks(), ()) self.assertEqual(subsf.get_locked(), False) self.assertEqual(subsf.get_locks(), ()) subsf.lock() self.assertEqual(sf.get_locked(), True) self.assertEqual(sf.get_locks(), (subsf,)) self.assertEqual(subsf.get_locked(), True) self.assertEqual(subsf.get_locks(), (subsf,)) subsf.lock() self.assertEqual(sf.get_locked(), True) self.assertEqual(sf.get_locks(), (subsf, subsf)) self.assertEqual(subsf.get_locked(), True) self.assertEqual(subsf.get_locks(), (subsf, subsf))
pygame.init() window = pygame.display.set_mode((600,600)) pygame.display.set_caption("MY OWN PAINT") color = pygame.Color(255,255,255) window.fill(color) color2 = pygame.Color(156,156,156) for x in range(30): posx = x*20 pygame.draw.line(window,color2,(posx,0),(posx,600)) for y in range(30): posy = y*20 pygame.draw.line(window,color2,(0,posy),(600,posy)) rectan = pygame.Surface((20, 20)) #Tamano del Rectangulo for x in range(len(colores)): posx = x*20 rectan.fill(colores[x]) window.blit(rectan, (posx, 0)) while True: evento = pygame.event.wait() if evento.type == pygame.QUIT: break ## Dibujando el mapa if evento.type == pygame.MOUSEBUTTONDOWN: if evento.button == 1: mouse_x,mouse_y = pygame.mouse.get_pos()
#e7-3-11彈跳球遊戲 import pygame, sys pygame.init() #load pygame modules size = width, height = 400, 300 #size of window speed = [2, 2] #speed and direction screen = pygame.display.set_mode(size) #make window s = pygame.Surface((100, 50)) #create surface 100px by 50px s.fill((33, 66, 99)) #color the surface blue r = s.get_rect() #get the rectangle bounds for the surface clock = pygame.time.Clock() #make a clock while 1: #infinite loop clock.tick(30) #limit framerate to 30 FPS for event in pygame.event.get(): #if something clicked if event.type == pygame.QUIT: #if EXIT clicked sys.exit() #close cleanly r = r.move(speed) #move the box by the "speed" coordinates #if we hit a wall, change direction if r.left < 0 or r.right > width: speed[0] = -speed[0] if r.top < 0 or r.bottom > height: speed[1] = -speed[1] screen.fill((0, 0, 0)) #make redraw background black screen.blit(s, r) #render the surface into the rectangle pygame.display.flip() #update the screen
main_theme = (0, 100, 100) red = (100, 0, 0) white = (255, 255, 255) # game settings settings = Settings() show_notes = True star_chance = 10 # chance of a star is 1 in 10 every update initial_stars = 100 # create 100 stars to fill the screen package_chance = 10 # chance of a package drop is 1 in 10 every enemy kill # if your game runs slow, set particle count low and star chance higher # all particles, stars, enemies and bullets are removed when they leave the screen width = 800 height = 500 main_s = pygame.display.set_mode((width, height)) main_s.fill((255, 255, 255)) pygame.display.set_caption("SPACE SHIPS") pygame.display.set_icon(pygame.image.load("Images/Icon.png")) font = pygame.font.Font("./Font/tall bolder.ttf", 15) menu_font = pygame.font.Font("./Font/tall bolder.ttf", 25) title_font = pygame.font.Font("./Font/tall bolder.ttf", 45) screen_rect = pygame.sprite.Sprite() # screen_rect is slightly bigger than the screen so that objects do not get removed # when created off screen. many objects are checked as still on screen to remove it # from a list that it is in screen_rect.rect = pygame.Surface((width + 30, height + 30)).get_rect() screen_rect.rect.x = 0 screen_rect.rect.y = 0
def initializeTextureBoard(self, size): for y in range(0, size - 1): new = [] for x in range(0, size - 1): new.append(pygame.Surface((32, 32), pygame.SRCALPHA)) self.boardTextures.append(new)
def insertTexture(self, x, y, image): self.boardTextures[x][y] = pygame.Surface((32, 32), pygame.SRCALPHA) self.boardTextures[x][y].blit(self.imageDictionary[image], (0, 0))
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, dokill=1): boom_sound.play() Explosion(alien) Explosion(player) SCORE = SCORE + 1 player.kill() for alien in pygame.sprite.groupcollide(shots, aliens, dokilla=1, dokillb=1).keys(): boom_sound.play() Explosion(alien) SCORE = SCORE + 1 for bomb in pygame.sprite.spritecollide(player, bombs, dokill=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()
import pygame, sys from pygame.locals import * pygame.init() WINDOW_SIZE = (1000, 600) display = pygame.Surface((1000, 600)) screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32) pygame.display.set_caption('Platformer_v01') # Window Name WHITE = (255, 255, 255) BLACK = (0, 0, 0) tile_rects = [] global player_movement max_fall_speed = 8 fall_speed = 0.5 clock = pygame.time.Clock() background = pygame.image.load("C:/code/Pygame/RPG/Images/sky.jpg") character = pygame.image.load('C:/code/Pygame/RPG/Images/blue.png') # character.set_colorkey((255, 255, 255)) grass_tile = pygame.image.load('C:/code/Pygame/RPG/Images/grass_tile.png') dirt_tile = pygame.image.load('C:/code/Pygame/RPG/Images/dirt_tile.png') TILE_SIZE = grass_tile.get_width()
# Main file for game environment # Credit to: https://pygame-gui.readthedocs.io/en/latest/quick_start.html # for pygame environment import pygame pygame.init() pygame.display.set_caption('Quick Start') window_surface = pygame.display.set_mode((800, 600)) background = pygame.Surface((800, 600)) background.fill(pygame.Color('#000000')) is_running = True while is_running: for event in pygame.event.get(): if event.type == pygame.QUIT: is_running = False window_surface.blit(background, (0, 0)) pygame.display.update()
from pygame.locals import * pygame.init() OKNOGRY_SZER = 800 OKNOGRY_WYS = 400 LT_BLUE = (230, 255, 255) oknogry = pygame.display.set_mode((OKNOGRY_SZER, OKNOGRY_WYS), 0, 32) pygame.display.set_caption('Prosty Pong') PALETKA_SZER = 100 PALETKA_WYS = 20 BLUE = (0, 0, 255) PALETKA_1_POZ = (350, 360) paletka1 = pygame.Surface([PALETKA_SZER, PALETKA_WYS]) paletka1.fill(BLUE) paletka1_prost = paletka1.get_rect() paletka1_prost.x = PALETKA_1_POZ[0] paletka1_prost.y = PALETKA_1_POZ[1] P_SZER = 20 P_WYS = 20 P_PREDKOSC_X = 4 P_PREDKOSC_Y = 4 GREEN = (0, 255, 0) pilka = pygame.Surface([P_SZER, P_WYS], pygame.SRCALPHA, 32).convert_alpha() pygame.draw.ellipse(pilka, GREEN, [0, 0, P_SZER, P_WYS]) pilka_prost = pilka.get_rect() pilka_prost.x = OKNOGRY_SZER / 2 pilka_prost.y = OKNOGRY_WYS / 2
import pygame from pygame.math import Vector2 import os pygame.init() gameDisplay = pygame.display.set_mode((800, 600)) pygame.display.set_caption('A bit Racey') clock = pygame.time.Clock() loop = True pos = (120, 120) filename = "Snake\\Assets\Images\\car0.png" if filename == None or not os.path.exists(filename): image_base = pygame.Surface((70, 50), pygame.SRCALPHA) pygame.draw.polygon(image_base, (50, 120, 180), ((0, 0), (0, 50), (70, 25))) car_rect = image_base.get_rect(center=pos) else: image_base = pygame.image.load(filename).convert_alpha() car_rect = image_base.get_rect(center=pos) #car_rect.centerx = car_rect.width / 2 #car_rect.centery = car_rect.height / 2 def rot_center(image, angle): center = image.get_rect().center rotated_image = pygame.transform.rotate(image, angle) new_rect = rotated_image.get_rect(center=center) return rotated_image, new_rect
def main(): agent = dqn() agent.restore('save/ckpt') """Mainline logic for Getaway""" # D - Display configuration screen = pygame.display.set_mode((300, 700), 0, 32) pygame.display.set_caption("DISPLAY!") # E - Entities background = pygame.Surface(screen.get_size()) background = background.convert() #Sounds pygame.mixer.music.load("Sounds/bgm.wav") pygame.mixer.music.set_volume(0.2) pygame.mixer.music.play(-1) crash = pygame.mixer.Sound("Sounds/crash.wav") crash.set_volume(0.3) coin = pygame.mixer.Sound("Sounds/coin.wav") coin.set_volume(0.3) power_up = pygame.mixer.Sound("Sounds/power_up.wav") power_up.set_volume(0.3) #Road Background road = pySprites.road(screen) #Scorekeeper #Car sprite car = pySprites.player_car() #Obstacles opposing_cars_group = [] car1 = pySprites.other_cars() opposing_cars_group.append(car1) Scorekeeper = pySprites.ScoreKeeper(car, 100, 'speed') otherspeed = pySprites.ScoreKeeper(car1, 150, 'other_speed') #car2 = pySprites.other_cars() #opposing_cars_group.append(car2) obstacles = pygame.sprite.Group(opposing_cars_group, []) coin_group = [] road_block_group = [] speed_boost = [] life_boost = [] #allSprites Group # A - Action # A - Assign values clock = pygame.time.Clock() keepGoing = True move_up = False move_down = False move_left = False move_right = False counter = 0 speedboost = False boostcounter = 0 invincibility_flag = False inv_counter = 0 vfree = 90 current_state = pySprites.neirong() current_state.bian = "side road" chedao = pySprites.showbian(current_state, 200, 'Road') Vtree = pySprites.neirong() Vtree.bian = vfree zhuangtai = pySprites.showbian(Vtree, 50, 'expect') allSprites = pygame.sprite.OrderedUpdates(chedao, zhuangtai, road, car, otherspeed, opposing_cars_group, road_block_group, Scorekeeper) # L - Loop h = 0 vdf = 0 crashh = 0 distance = 0 total_time = 0 state = [ vfree, car.road, car1.road, 10 * round(car.speed / 36), round(car1.speed / 3.6), 10 * round( (car.rect.top - car1.rect.bottom) / 30) ] while keepGoing: total_time += 1 if total_time % 1000 == 0: agent.restore('save/ckpt') print(distance) print(h) h = 0 action = agent.action(state) # T - Timer clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer.music.fadeout(2000) keepGoing = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: vfree -= 5 Vtree.bian = vfree if event.key == pygame.K_UP: vfree += 5 Vtree.bian = vfree #Ingame clock counter if action == 1: if car.road == 1: vdf = keepgo(car, car1, vfree) else: current_state.bian = "changing" crashh = change(car, allSprites, screen, background, clock, obstacles, Scorekeeper) current_state.bian = "main road" else: if car.road == 2: vdf = keepgo(car, car1, vfree) else: current_state.bian = "changing" crashh = change(car, allSprites, screen, background, clock, obstacles, Scorekeeper) current_state.bian = "over road" #Collision detection for obstacle in pygame.sprite.spritecollide(car, obstacles, False): obstacle.reset() Scorekeeper.death() crashh = 1 h += 1 crash.play() if crashh == 1: reward = -1000 else: if vdf > 3: reward = -150 else: if car.road == 2: reward = car.speed / 3.6 - vfree / 3.6 - 0.1 * vdf - 2 else: reward = car.speed / 3.6 - vfree / 3.6 - 0.1 * vdf # R - Refresh display allSprites.clear(screen, background) allSprites.update(car.speed) allSprites.draw(screen) if crashh == 1: reward = -1000 else: if vdf > 3: reward = -150 else: if car.road == 2: reward = car.speed / 3.6 - vfree / 3.6 - 0.1 * vdf - 2 else: reward = car.speed / 3.6 - vfree / 3.6 - 0.1 * vdf next_state = [ vfree, car.road, car1.road, 10 * round(car.speed / 36), round(car1.speed / 3.6), 10 * round( (car.rect.top - car1.rect.bottom) / 30) ] #agent.preceive(state, action, reward, next_state, 0) state = next_state distance += car.speed pygame.display.flip() # Close the game window pygame.quit()
def get_Rectangulo(color, tamanio): ''' Devuelve una superficie según color y tamaño. ''' superficie = pygame.Surface(tamanio, flags=HWSURFACE) superficie.fill(color) return superficie
def set_text(self, text, color=(255, 255, 255), seconds=2.0): text_texture = self.font.render(text, True, color) self.surface = pygame.Surface(self.dim) self.seconds_left = seconds self.surface.fill((0, 0, 0, 0)) self.surface.blit(text_texture, (10, 11))
def get_Rectangulo_Transparente(tamanio): ''' Devuelve una superficie según color y tamaño. ''' superficie = pygame.Surface(tamanio, flags=HWSURFACE) superficie.fill(get_magenta()) superficie.set_colorkey(get_magenta(), pygame.RLEACCEL) return superficie
def preRender(self): self.surface = pygame.Surface((self.width, self.height)) self.surface.blit(self.tableImg, (0, 0))
def __init__(self, image_path, perso_img=perso["standard"]): self.image = pygame.image.load(image_path).convert().convert_alpha() self.surface = pygame.Surface((1200, 700)) self.color = get_pixel_color(image_path) self.perso = pygame.image.load(perso_img)
''' * @author anlzou * @date 2020/5/1 19:24 * @version xx * @description xx ''' import random, pygame PANEL_width = 400 PANEL_highly = 500 FONT_PX = 15 pygame.init() # 创建一个窗口 winSur = pygame.display.set_mode((PANEL_width, PANEL_highly)) font = pygame.font.SysFont('123.ttf', 22) bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA) pygame.Surface.convert(bg_suface) bg_suface.fill(pygame.Color(0, 0, 0, 28)) winSur.fill((0, 0, 0)) letter = [ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' ] texts = [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)] # 按窗口的宽度来计算可以在画板上放几列坐标并生成一个列表 column = int(PANEL_width / FONT_PX) drops = [0 for i in range(column)] while True: # 从队列中获取事件 for event in pygame.event.get(): if event.type == pygame.QUIT:
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) win = (800, 600) pygame.init() screen = pygame.display.set_mode(win) pygame.display.set_caption('Snake') screen.fill(BLACK) pygame.display.flip() snake = [[200, 200]] surface = pygame.Surface(win, 0, 24) clock = pygame.time.Clock() clock.tick() direction = '' started = False point = newpoint() screen.fill(WHITE) while True: for e in pygame.event.get(): if e.type == KEYDOWN: if not started: started = True
def create_sprite(img, sprite_size): icon = pygame.image.load(img).convert_alpha() icon = pygame.transform.scale(icon, (sprite_size, sprite_size)) sprite = pygame.Surface((sprite_size, sprite_size), pygame.HWSURFACE) sprite.blit(icon, (0, 0)) return sprite
import pygame import sys pygame.init() size = width, height = 800, 600 black = 0, 0, 0 screen = pygame.display.set_mode(size).convert() pygame.display.set_caption("Vertical Line") vertical_rect = pygame.Rect(40, 40, 10, 30) vertical_line = pygame.Surface(vertical_rect.size) vertical_line.fill((1, 255, 1)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #screen.fill(black) screen.blit(vertical_line, (400, 0)) pygame.display.flip()
# Listing_18-2.py # Copyright Warren Sande, 2009 # Released under MIT license http://www.opensource.org/licenses/mit-license.php # Version 61 ---------------------------- # Bouncing ball with up and down arrow keys import pygame, sys pygame.init() screen = pygame.display.set_mode([640, 480]) background = pygame.Surface(screen.get_size()) background.fill([255, 255, 255]) clock = pygame.time.Clock() class Ball(pygame.sprite.Sprite): def __init__(self, image_file, speed, location): pygame.sprite.Sprite.__init__(self) #call Sprite initializer self.image = pygame.image.load(image_file) self.rect = self.image.get_rect() self.rect.left, self.rect.top = location self.speed = speed def move(self): if self.rect.left <= screen.get_rect().left or \ self.rect.right >= screen.get_rect().right: self.speed[0] = -self.speed[0] newpos = self.rect.move(self.speed) self.rect = newpos
def add(self, widget): self.widgets.append(widget) w = int(self.screen.get_width()*widget.width) h = int(self.screen.get_height()*widget.height) self.surfaces[widget] = pygame.Surface((w,h),HWSURFACE|DOUBLEBUF)